bcf53212aace655dae81c25cf2160a8938d12fc4
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.hc2vpp.v3po.interfacesstate;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.when;
23
24 import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest;
25 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
26 import io.fd.hc2vpp.common.translate.util.NamingContext;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceGetTable;
30 import io.fd.vpp.jvpp.core.dto.SwInterfaceGetTableReply;
31 import org.junit.Test;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.SubinterfaceStateAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.SubInterfaces;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.sub.interfaces.SubInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.sub._interface.routing.attributes.Routing;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.sub._interface.routing.attributes.RoutingBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 public class SubInterfaceRoutingCustomizerTest extends ReaderCustomizerTest<Routing, RoutingBuilder> implements
45         ByteDataTranslator {
46
47     private static final String IFC_CTX_NAME = "ifc-test-instance";
48     private static final String IF_NAME = "local0";
49     private static final int IF_ID = 1;
50     private static final String SUBIF_NAME = "local0.4";
51     private static final int SUBIF_ID = 4;
52     private static final InstanceIdentifier<Routing> VALID_ID =
53             InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
54                     .augmentation(SubinterfaceStateAugmentation.class)
55                     .child(SubInterfaces.class)
56                     .child(SubInterface.class, new SubInterfaceKey((long) SUBIF_ID))
57                     .child(Routing.class);
58     private static final int IPV4_VRF = 4;
59     private static final int IPV6_VRF = 6;
60     private static final int NO_VRF = 0;
61
62     private NamingContext interfacesContext;
63
64     public SubInterfaceRoutingCustomizerTest() {
65         super(Routing.class, SubInterfaceBuilder.class);
66     }
67
68     @Override
69     protected void setUp() throws Exception {
70         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
71         defineMapping(mappingContext, IF_NAME, IF_ID, IFC_CTX_NAME);
72         defineMapping(mappingContext, SUBIF_NAME, SUBIF_ID, IFC_CTX_NAME);
73     }
74
75     @Override
76     protected ReaderCustomizer<Routing, RoutingBuilder> initCustomizer() {
77         return new SubInterfaceRoutingCustomizer(api, interfacesContext);
78     }
79
80     @Test
81     public void testReadAttributesAllDefined() throws ReadFailedException {
82         when(api.swInterfaceGetTable(request(false, SUBIF_ID))).thenReturn(future(reply(IPV4_VRF)));
83         when(api.swInterfaceGetTable(request(true, SUBIF_ID))).thenReturn(future(reply(IPV6_VRF)));
84         final RoutingBuilder routingBuilder = new RoutingBuilder();
85         getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx);
86         assertEquals(IPV4_VRF, routingBuilder.getIpv4VrfId().intValue());
87         assertEquals(IPV6_VRF, routingBuilder.getIpv6VrfId().intValue());
88     }
89
90     @Test
91     public void testReadAttributesOneDefined() throws ReadFailedException {
92         when(api.swInterfaceGetTable(request(false, SUBIF_ID))).thenReturn(future(reply(IPV4_VRF)));
93         when(api.swInterfaceGetTable(request(true, SUBIF_ID))).thenReturn(future(reply(NO_VRF)));
94         final RoutingBuilder routingBuilder = new RoutingBuilder();
95         getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx);
96         assertEquals(IPV4_VRF, routingBuilder.getIpv4VrfId().intValue());
97         assertNull(routingBuilder.getIpv6VrfId());
98     }
99
100     @Test
101     public void testReadAttributesNoDefined() throws ReadFailedException {
102         when(api.swInterfaceGetTable(any())).thenReturn(future(reply(NO_VRF)));
103         final RoutingBuilder routingBuilder = new RoutingBuilder();
104         getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx);
105         assertNull(routingBuilder.getIpv4VrfId());
106         assertNull(routingBuilder.getIpv6VrfId());
107     }
108
109     private SwInterfaceGetTable request(final boolean ipv6, final int index) {
110         SwInterfaceGetTable request = new SwInterfaceGetTable();
111         request.isIpv6 = booleanToByte(ipv6);
112         request.swIfIndex = index;
113         return request;
114     }
115
116     private SwInterfaceGetTableReply reply(final int vrf) {
117         SwInterfaceGetTableReply reply = new SwInterfaceGetTableReply();
118         reply.vrfId = vrf;
119         return reply;
120     }
121 }