2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate;
19 import static io.fd.honeycomb.v3po.translate.v3po.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.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;
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;
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.dto.SwInterfaceDetails;
47 import org.openvpp.jvpp.dto.VxlanTunnelDetails;
48 import org.openvpp.jvpp.dto.VxlanTunnelDetailsReplyDump;
49 import org.openvpp.jvpp.dto.VxlanTunnelDump;
51 public class VxlanCustomizerTest extends ChildReaderCustomizerTest<Vxlan, VxlanBuilder> {
53 private NamingContext interfacesContext;
54 static final InstanceIdentifier<Vxlan> IID =
55 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey("ifc1"))
56 .augmentation(VppInterfaceStateAugmentation.class).child(Vxlan.class);
58 public VxlanCustomizerTest() {
63 public void setUpBefore() {
64 interfacesContext = new NamingContext("vxlan-tunnel", "test-instance");
65 doReturn(getMapping("ifc1", 0)).when(mappingContext).read(getMappingIid("ifc1", "test-instance"));
67 final SwInterfaceDetails v = new SwInterfaceDetails();
68 v.interfaceName = "vxlan-tunnel4".getBytes();
69 final Map<Integer, SwInterfaceDetails> map = new HashMap<>();
71 cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, map);
75 protected void setUpAfter() throws UnknownHostException {
76 final CompletableFuture<VxlanTunnelDetailsReplyDump> vxlanTunnelDetailsReplyDumpCompletionStage =
77 new CompletableFuture<>();
79 final VxlanTunnelDetailsReplyDump value = new VxlanTunnelDetailsReplyDump();
80 final VxlanTunnelDetails vxlanTunnelDetails = new VxlanTunnelDetails();
81 vxlanTunnelDetails.isIpv6 = 0;
82 vxlanTunnelDetails.decapNextIndex = 1;
83 vxlanTunnelDetails.dstAddress = InetAddress.getByName("1.2.3.4").getAddress();
84 vxlanTunnelDetails.srcAddress = InetAddress.getByName("1.2.3.5").getAddress();
85 vxlanTunnelDetails.encapVrfId = 55;
86 vxlanTunnelDetails.swIfIndex = 0;
87 vxlanTunnelDetails.vni = 9;
89 value.vxlanTunnelDetails = Lists.newArrayList(vxlanTunnelDetails);
90 vxlanTunnelDetailsReplyDumpCompletionStage.complete(value);
92 doReturn(vxlanTunnelDetailsReplyDumpCompletionStage).when(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
96 public void testReadCurrentAttributes() throws Exception {
97 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
98 getCustomizer().readCurrentAttributes(IID, builder, ctx);
100 assertEquals(9, builder.getVni().intValue());
101 assertEquals(55, builder.getEncapVrfId().intValue());
103 assertNull(builder.getSrc().getIpv6Address());
104 assertNotNull(builder.getSrc().getIpv4Address());
105 assertEquals("1.2.3.5", builder.getSrc().getIpv4Address().getValue());
107 assertNull(builder.getDst().getIpv6Address());
108 assertNotNull(builder.getDst().getIpv4Address());
109 assertEquals("1.2.3.4", builder.getDst().getIpv4Address().getValue());
111 verify(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
114 @Test(expected = NullPointerException.class)
115 public void testReadCurrentAttributesVppNameNotCached() throws Exception {
116 InterfaceCustomizer.getCachedInterfaceDump(cache).remove(0);
118 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
119 getCustomizer().readCurrentAttributes(IID, builder, ctx);
123 public void testReadCurrentAttributesWrongType() throws Exception {
124 final SwInterfaceDetails v = new SwInterfaceDetails();
125 v.interfaceName = "tap-2".getBytes();
126 InterfaceCustomizer.getCachedInterfaceDump(cache).put(0, v);
128 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
129 getCustomizer().readCurrentAttributes(IID, builder, ctx);
132 verifyZeroInteractions(api);
136 protected RootReaderCustomizer<Vxlan, VxlanBuilder> initCustomizer() {
137 return new VxlanCustomizer(api, interfacesContext);