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.translate.v3po.interfacesstate;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyZeroInteractions;
27 import com.google.common.collect.Lists;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.util.HashMap;
35 import org.junit.Test;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Vxlan;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.VxlanBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import io.fd.vpp.jvpp.VppInvocationException;
45 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
46 import io.fd.vpp.jvpp.core.dto.VxlanTunnelDetails;
47 import io.fd.vpp.jvpp.core.dto.VxlanTunnelDetailsReplyDump;
48 import io.fd.vpp.jvpp.core.dto.VxlanTunnelDump;
50 public class VxlanCustomizerTest extends ReaderCustomizerTest<Vxlan, VxlanBuilder> {
52 private static final String IFC_CTX_NAME = "ifc-test-instance";
53 private static final String IF_NAME = "ifc1";
54 private static final int IF_INDEX = 0;
56 private NamingContext interfacesContext;
57 static final InstanceIdentifier<Vxlan> IID =
58 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
59 .augmentation(VppInterfaceStateAugmentation.class).child(Vxlan.class);
61 public VxlanCustomizerTest() {
62 super(Vxlan.class, VppInterfaceStateAugmentationBuilder.class);
66 public void setUp() throws UnknownHostException, VppInvocationException {
67 interfacesContext = new NamingContext("vxlan-tunnel", IFC_CTX_NAME);
68 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
70 final SwInterfaceDetails v = new SwInterfaceDetails();
71 v.interfaceName = "vxlan-tunnel4".getBytes();
72 final Map<Integer, SwInterfaceDetails> map = new HashMap<>();
74 cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, map);
76 final VxlanTunnelDetailsReplyDump value = new VxlanTunnelDetailsReplyDump();
77 final VxlanTunnelDetails vxlanTunnelDetails = new VxlanTunnelDetails();
78 vxlanTunnelDetails.isIpv6 = 0;
79 vxlanTunnelDetails.decapNextIndex = 1;
80 vxlanTunnelDetails.dstAddress = InetAddress.getByName("1.2.3.4").getAddress();
81 vxlanTunnelDetails.srcAddress = InetAddress.getByName("1.2.3.5").getAddress();
82 vxlanTunnelDetails.encapVrfId = 55;
83 vxlanTunnelDetails.swIfIndex = 0;
84 vxlanTunnelDetails.vni = 9;
85 value.vxlanTunnelDetails = Lists.newArrayList(vxlanTunnelDetails);
86 doReturn(future(value)).when(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
90 public void testReadCurrentAttributes() throws Exception {
91 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
92 getCustomizer().readCurrentAttributes(IID, builder, ctx);
94 assertEquals(9, builder.getVni().getValue().intValue());
95 assertEquals(55, builder.getEncapVrfId().intValue());
97 assertNull(builder.getSrc().getIpv6Address());
98 assertNotNull(builder.getSrc().getIpv4Address());
99 assertEquals("1.2.3.5", builder.getSrc().getIpv4Address().getValue());
101 assertNull(builder.getDst().getIpv6Address());
102 assertNotNull(builder.getDst().getIpv4Address());
103 assertEquals("1.2.3.4", builder.getDst().getIpv4Address().getValue());
105 verify(api).vxlanTunnelDump(any(VxlanTunnelDump.class));
108 @Test(expected = NullPointerException.class)
109 public void testReadCurrentAttributesVppNameNotCached() throws Exception {
110 InterfaceCustomizer.getCachedInterfaceDump(cache).remove(0);
112 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
113 getCustomizer().readCurrentAttributes(IID, builder, ctx);
117 public void testReadCurrentAttributesWrongType() throws Exception {
118 final SwInterfaceDetails v = new SwInterfaceDetails();
119 v.interfaceName = "tap-2".getBytes();
120 InterfaceCustomizer.getCachedInterfaceDump(cache).put(0, v);
122 final VxlanBuilder builder = getCustomizer().getBuilder(IID);
123 getCustomizer().readCurrentAttributes(IID, builder, ctx);
126 verifyZeroInteractions(api);
130 protected ReaderCustomizer<Vxlan, VxlanBuilder> initCustomizer() {
131 return new VxlanCustomizer(api, interfacesContext);