HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / GreCustomizerTest.java
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.translate.v3po.interfacesstate;
18
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;
26
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;
34 import java.util.Map;
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.Gre;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.GreBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import io.fd.vpp.jvpp.VppInvocationException;
45 import io.fd.vpp.jvpp.core.dto.GreTunnelDetails;
46 import io.fd.vpp.jvpp.core.dto.GreTunnelDetailsReplyDump;
47 import io.fd.vpp.jvpp.core.dto.GreTunnelDump;
48 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
49
50 public class GreCustomizerTest extends ReaderCustomizerTest<Gre, GreBuilder> {
51
52     private static final String IFACE_NAME = "ifc1";
53     private static final int IFACE_ID = 0;
54     private static final String IFC_CTX_NAME = "ifc-test-instance";
55
56     private NamingContext interfacesContext;
57     static final InstanceIdentifier<Gre> IID =
58         InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
59             .augmentation(VppInterfaceStateAugmentation.class).child(Gre.class);
60
61     public GreCustomizerTest() {
62         super(Gre.class, VppInterfaceStateAugmentationBuilder.class);
63     }
64
65     @Override
66     public void setUp() throws UnknownHostException, VppInvocationException {
67         interfacesContext = new NamingContext("gre-tunnel", IFC_CTX_NAME);
68         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
69
70         final SwInterfaceDetails v = new SwInterfaceDetails();
71         v.interfaceName = "gre-tunnel4".getBytes();
72         final Map<Integer, SwInterfaceDetails> map = new HashMap<>();
73         map.put(0, v);
74         cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, map);
75
76         final GreTunnelDetailsReplyDump value = new GreTunnelDetailsReplyDump();
77         final GreTunnelDetails greTunnelDetails = new GreTunnelDetails();
78         greTunnelDetails.isIpv6 = 0;
79         greTunnelDetails.dstAddress = InetAddress.getByName("1.2.3.4").getAddress();
80         greTunnelDetails.srcAddress = InetAddress.getByName("1.2.3.5").getAddress();
81         greTunnelDetails.outerFibId = 55;
82         greTunnelDetails.swIfIndex = 0;
83         value.greTunnelDetails = Lists.newArrayList(greTunnelDetails);
84
85         doReturn(future(value)).when(api).greTunnelDump(any(GreTunnelDump.class));
86     }
87
88     @Test
89     public void testReadCurrentAttributes() throws Exception {
90         final GreBuilder builder = getCustomizer().getBuilder(IID);
91         getCustomizer().readCurrentAttributes(IID, builder, ctx);
92
93         assertEquals(55, builder.getOuterFibId().intValue());
94
95         assertNull(builder.getSrc().getIpv6Address());
96         assertNotNull(builder.getSrc().getIpv4Address());
97         assertEquals("1.2.3.5", builder.getSrc().getIpv4Address().getValue());
98
99         assertNull(builder.getDst().getIpv6Address());
100         assertNotNull(builder.getDst().getIpv4Address());
101         assertEquals("1.2.3.4", builder.getDst().getIpv4Address().getValue());
102
103         verify(api).greTunnelDump(any(GreTunnelDump.class));
104     }
105
106     @Test(expected = NullPointerException.class)
107     public void testReadCurrentAttributesVppNameNotCached() throws Exception {
108         InterfaceCustomizer.getCachedInterfaceDump(cache).remove(0);
109
110         final GreBuilder builder = getCustomizer().getBuilder(IID);
111         getCustomizer().readCurrentAttributes(IID, builder, ctx);
112     }
113
114     @Test
115     public void testReadCurrentAttributesWrongType() throws Exception {
116         final SwInterfaceDetails v = new SwInterfaceDetails();
117         v.interfaceName = "tap-2".getBytes();
118         InterfaceCustomizer.getCachedInterfaceDump(cache).put(0, v);
119
120         final GreBuilder builder = getCustomizer().getBuilder(IID);
121         getCustomizer().readCurrentAttributes(IID, builder, ctx);
122
123         // Should be ignored
124         verifyZeroInteractions(api);
125     }
126
127     @Override
128     protected ReaderCustomizer<Gre, GreBuilder> initCustomizer() {
129         return new GreCustomizer(api, interfacesContext);
130     }
131 }