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.assertTrue;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
28 import io.fd.honeycomb.vpp.test.util.InterfaceDumpHelper;
29 import java.util.HashMap;
30 import java.util.List;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfacesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.VlanTagged;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.Match;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.Tags;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
49 public class SubInterfaceCustomizerTest extends ListReaderCustomizerTest<SubInterface, SubInterfaceKey, SubInterfaceBuilder> implements
52 private static final String IFC_CTX_NAME = "ifc-test-instance";
53 private static final String SUPER_IF_NAME = "local0";
54 private static final int SUPER_IF_INDEX = 1;
55 private static final String VLAN_IF_NAME = "local0.1";
56 private static final int VLAN_IF_ID = 1;
57 private static final int VLAN_IF_INDEX = 11;
59 private NamingContext interfacesContext;
61 public SubInterfaceCustomizerTest() {
62 super(SubInterface.class, SubInterfacesBuilder.class);
67 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
68 defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
69 defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
73 protected ReaderCustomizer<SubInterface, SubInterfaceBuilder> initCustomizer() {
74 return new SubInterfaceCustomizer(api, interfacesContext);
77 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long id) {
78 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
79 SubinterfaceStateAugmentation.class).child(
80 SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(id));
84 public void testRead() throws ReadFailedException {
85 final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
87 final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
88 ifaceDetails.subId = VLAN_IF_ID;
89 ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
90 ifaceDetails.subDot1Ad = 1;
91 defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
92 defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
93 ifaceDetails.subNumberOfTags = 2;
94 ifaceDetails.subOuterVlanIdAny = 1;
95 ifaceDetails.subInnerVlanIdAny = 1;
96 ifaceDetails.subExactMatch = 1;
97 cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
98 cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
100 final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class);
101 getCustomizer().readCurrentAttributes(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), builder, ctx);
103 verify(builder).setIdentifier((long) VLAN_IF_ID);
105 ArgumentCaptor<Tags> tagCaptor = ArgumentCaptor.forClass(Tags.class);
106 verify(builder).setTags(tagCaptor.capture());
107 assertEquals(ifaceDetails.subNumberOfTags, tagCaptor.getValue().getTag().size());
109 ArgumentCaptor<Match> matchCaptor = ArgumentCaptor.forClass(Match.class);
110 verify(builder).setMatch(matchCaptor.capture());
111 final VlanTagged matchType = (VlanTagged)matchCaptor.getValue().getMatchType();
112 assertTrue(matchType.getVlanTagged().isMatchExactTags());
116 public void testGetAllIds() throws Exception {
117 final SwInterfaceDetails iface = new SwInterfaceDetails();
118 iface.interfaceName = VLAN_IF_NAME.getBytes();
119 iface.swIfIndex = VLAN_IF_INDEX;
120 iface.subId = VLAN_IF_ID;
121 iface.supSwIfIndex = SUPER_IF_INDEX;
122 whenSwInterfaceDumpThenReturn(api, iface);
124 final List<SubInterfaceKey> allIds =
125 getCustomizer().getAllIds(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), ctx);
127 assertEquals(1, allIds.size());