0d0a2cc83e82ae1667f91d42b3663e5ccccef031
[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.translate.v3po.interfacesstate;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceStateAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfaces;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfacesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTagged;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Tags;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
51 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
52 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
53
54 public class SubInterfaceCustomizerTest extends
55         ListReaderCustomizerTest<SubInterface, SubInterfaceKey, SubInterfaceBuilder> {
56
57     private static final String IFC_CTX_NAME = "ifc-test-instance";
58     private static final String SUPER_IF_NAME = "local0";
59     private static final int SUPER_IF_INDEX = 1;
60     private static final String VLAN_IF_NAME = "local0.1";
61     private static final int VLAN_IF_ID = 1;
62     private static final int VLAN_IF_INDEX = 11;
63
64     private NamingContext interfacesContext;
65
66     public SubInterfaceCustomizerTest() {
67         super(SubInterface.class);
68     }
69
70     @Override
71     public void setUp() {
72         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
73         ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
74         ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
75     }
76
77     @Override
78     protected ReaderCustomizer<SubInterface, SubInterfaceBuilder> initCustomizer() {
79         return new SubInterfaceCustomizer(api, interfacesContext);
80     }
81
82     private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long id) {
83         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
84                 SubinterfaceStateAugmentation.class).child(
85                 SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(id));
86     }
87
88     @Test
89     public void testMerge() {
90         final SubInterfacesBuilder builder = mock(SubInterfacesBuilder.class);
91         final  List<SubInterface> value = mock(List.class);
92         getCustomizer().merge(builder, value);
93         verify(builder).setSubInterface(value);
94     }
95
96     @Test
97     public void testRead() throws ReadFailedException {
98         final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
99
100         final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
101         ifaceDetails.subId = VLAN_IF_ID;
102         ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
103         ifaceDetails.subDot1Ad = 1;
104         ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
105         ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
106         ifaceDetails.subNumberOfTags = 2;
107         ifaceDetails.subOuterVlanIdAny = 1;
108         ifaceDetails.subInnerVlanIdAny = 1;
109         ifaceDetails.subExactMatch = 1;
110         cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
111         cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
112
113         final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class);
114         getCustomizer().readCurrentAttributes(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), builder, ctx);
115
116         verify(builder).setIdentifier((long) VLAN_IF_ID);
117
118         ArgumentCaptor<Tags> tagCaptor = ArgumentCaptor.forClass(Tags.class);
119         verify(builder).setTags(tagCaptor.capture());
120         assertEquals(ifaceDetails.subNumberOfTags, tagCaptor.getValue().getTag().size());
121
122         ArgumentCaptor<Match> matchCaptor = ArgumentCaptor.forClass(Match.class);
123         verify(builder).setMatch(matchCaptor.capture());
124         final VlanTagged matchType = (VlanTagged)matchCaptor.getValue().getMatchType();
125         assertTrue(matchType.getVlanTagged().isMatchExactTags());
126     }
127
128     @Test
129     public void testGetAllIds() throws Exception {
130         final SwInterfaceDetails iface = new SwInterfaceDetails();
131         iface.interfaceName = VLAN_IF_NAME.getBytes();
132         iface.swIfIndex = VLAN_IF_INDEX;
133         iface.subId = VLAN_IF_ID;
134         iface.supSwIfIndex = SUPER_IF_INDEX;
135         final List<SwInterfaceDetails> ifaces = Collections.singletonList(iface);
136
137         final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
138         reply.swInterfaceDetails = ifaces;
139         when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(future(reply));
140
141         final List<SubInterfaceKey> allIds =
142                 getCustomizer().getAllIds(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), ctx);
143
144         assertEquals(ifaces.size(), allIds.size());
145     }
146 }