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