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