64c14ebffc1899552120423a9648a4877a0355ae
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 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.honeycomb.v3po.translate.v3po.interfacesstate;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyZeroInteractions;
28
29 import com.google.common.collect.Lists;
30 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
31 import io.fd.honeycomb.v3po.translate.v3po.test.ChildReaderCustomizerTest;
32 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
33 import java.net.InetAddress;
34 import java.net.UnknownHostException;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.concurrent.CompletableFuture;
38 import org.junit.Test;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Vxlan;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.openvpp.jvpp.VppInvocationException;
47 import org.openvpp.jvpp.dto.SwInterfaceDetails;
48 import org.openvpp.jvpp.dto.VxlanTunnelDetails;
49 import org.openvpp.jvpp.dto.VxlanTunnelDetailsReplyDump;
50 import org.openvpp.jvpp.dto.VxlanTunnelDump;
51
52 public class VxlanCustomizerTest extends ChildReaderCustomizerTest<Vxlan, VxlanBuilder> {
53
54     private NamingContext interfacesContext;
55     static final InstanceIdentifier<Vxlan> IID =
56         InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey("ifc1"))
57             .augmentation(VppInterfaceStateAugmentation.class).child(Vxlan.class);
58
59     public VxlanCustomizerTest() {
60         super(Vxlan.class);
61     }
62
63     @Override
64     public void setUpBefore() {
65         interfacesContext = new NamingContext("vxlan-tunnel", "test-instance");
66         doReturn(getMapping("ifc1", 0)).when(mappingContext).read(getMappingIid("ifc1", "test-instance"));
67
68         final SwInterfaceDetails v = new SwInterfaceDetails();
69         v.interfaceName = "vxlan-tunnel4".getBytes();
70         final Map<Integer, SwInterfaceDetails> map = new HashMap<>();
71         map.put(0, v);
72         cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, map);
73     }
74
75     @Override
76     protected void setUpAfter() throws UnknownHostException, VppInvocationException {
77         final CompletableFuture<VxlanTunnelDetailsReplyDump> vxlanTunnelDetailsReplyDumpCompletionStage =
78             new CompletableFuture<>();
79
80         final VxlanTunnelDetailsReplyDump value = new VxlanTunnelDetailsReplyDump();
81         final VxlanTunnelDetails vxlanTunnelDetails = new VxlanTunnelDetails();
82         vxlanTunnelDetails.isIpv6 = 0;
83         vxlanTunnelDetails.decapNextIndex = 1;
84         vxlanTunnelDetails.dstAddress = InetAddress.getByName("1.2.3.4").getAddress();
85         vxlanTunnelDetails.srcAddress = InetAddress.getByName("1.2.3.5").getAddress();
86         vxlanTunnelDetails.encapVrfId = 55;
87         vxlanTunnelDetails.swIfIndex = 0;
88         vxlanTunnelDetails.vni = 9;
89
90         value.vxlanTunnelDetails = Lists.newArrayList(vxlanTunnelDetails);
91         vxlanTunnelDetailsReplyDumpCompletionStage.complete(value);
92
93         doReturn(vxlanTunnelDetailsReplyDumpCompletionStage).when(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
94     }
95
96     @Test
97     public void testReadCurrentAttributes() throws Exception {
98         final VxlanBuilder builder = getCustomizer().getBuilder(IID);
99         getCustomizer().readCurrentAttributes(IID, builder, ctx);
100
101         assertEquals(9, builder.getVni().getValue().intValue());
102         assertEquals(55, builder.getEncapVrfId().intValue());
103
104         assertNull(builder.getSrc().getIpv6Address());
105         assertNotNull(builder.getSrc().getIpv4Address());
106         assertEquals("1.2.3.5", builder.getSrc().getIpv4Address().getValue());
107
108         assertNull(builder.getDst().getIpv6Address());
109         assertNotNull(builder.getDst().getIpv4Address());
110         assertEquals("1.2.3.4", builder.getDst().getIpv4Address().getValue());
111
112         verify(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
113     }
114
115     @Test(expected = NullPointerException.class)
116     public void testReadCurrentAttributesVppNameNotCached() throws Exception {
117         InterfaceCustomizer.getCachedInterfaceDump(cache).remove(0);
118
119         final VxlanBuilder builder = getCustomizer().getBuilder(IID);
120         getCustomizer().readCurrentAttributes(IID, builder, ctx);
121     }
122
123     @Test
124     public void testReadCurrentAttributesWrongType() throws Exception {
125         final SwInterfaceDetails v = new SwInterfaceDetails();
126         v.interfaceName = "tap-2".getBytes();
127         InterfaceCustomizer.getCachedInterfaceDump(cache).put(0, v);
128
129         final VxlanBuilder builder = getCustomizer().getBuilder(IID);
130         getCustomizer().readCurrentAttributes(IID, builder, ctx);
131
132         // Should be ignored
133         verifyZeroInteractions(api);
134     }
135
136     @Override
137     protected RootReaderCustomizer<Vxlan, VxlanBuilder> initCustomizer() {
138         return new VxlanCustomizer(api, interfacesContext);
139     }
140 }