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.v3po.test.ContextTestUtils;
27 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
28 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
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;
51 public class SubInterfaceCustomizerTest extends
52 ListReaderCustomizerTest<SubInterface, SubInterfaceKey, SubInterfaceBuilder> {
54 private static final String IFC_CTX_NAME = "ifc-test-instance";
55 private static final String SUPER_IF_NAME = "local0";
56 private static final int SUPER_IF_INDEX = 1;
57 private static final String VLAN_IF_NAME = "local0.1";
58 private static final int VLAN_IF_ID = 1;
59 private static final int VLAN_IF_INDEX = 11;
61 private NamingContext interfacesContext;
63 public SubInterfaceCustomizerTest() {
64 super(SubInterface.class);
69 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
70 ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
71 ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
75 protected ReaderCustomizer<SubInterface, SubInterfaceBuilder> initCustomizer() {
76 return new SubInterfaceCustomizer(api, interfacesContext);
79 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long id) {
80 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
81 SubinterfaceStateAugmentation.class).child(
82 SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(id));
86 public void testMerge() {
87 final SubInterfacesBuilder builder = mock(SubInterfacesBuilder.class);
88 final List<SubInterface> value = mock(List.class);
89 getCustomizer().merge(builder, value);
90 verify(builder).setSubInterface(value);
94 public void testRead() throws ReadFailedException {
95 final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
97 final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
98 ifaceDetails.subId = VLAN_IF_ID;
99 ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
100 ifaceDetails.subDot1Ad = 1;
101 ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME);
102 ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
103 ifaceDetails.subNumberOfTags = 2;
104 ifaceDetails.subOuterVlanIdAny = 1;
105 ifaceDetails.subInnerVlanIdAny = 1;
106 ifaceDetails.subExactMatch = 1;
107 cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
108 cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
110 final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class);
111 getCustomizer().readCurrentAttributes(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), builder, ctx);
113 verify(builder).setIdentifier((long) VLAN_IF_ID);
115 ArgumentCaptor<Tags> tagCaptor = ArgumentCaptor.forClass(Tags.class);
116 verify(builder).setTags(tagCaptor.capture());
117 assertEquals(ifaceDetails.subNumberOfTags, tagCaptor.getValue().getTag().size());
119 ArgumentCaptor<Match> matchCaptor = ArgumentCaptor.forClass(Match.class);
120 verify(builder).setMatch(matchCaptor.capture());
121 final VlanTagged matchType = (VlanTagged)matchCaptor.getValue().getMatchType();
122 assertTrue(matchType.getVlanTagged().isMatchExactTags());
126 public void testGetAllIds() throws Exception {
127 final SwInterfaceDetails iface = new SwInterfaceDetails();
128 iface.interfaceName = VLAN_IF_NAME.getBytes();
129 iface.swIfIndex = VLAN_IF_INDEX;
130 iface.subId = VLAN_IF_ID;
131 iface.supSwIfIndex = SUPER_IF_INDEX;
132 final List<SwInterfaceDetails> ifaces = Collections.singletonList(iface);
133 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, ifaces);
135 final List<SubInterfaceKey> allIds =
136 getCustomizer().getAllIds(getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID), ctx);
138 assertEquals(ifaces.size(), allIds.size());