b93c9f42af359eff6710fcfd70ecc74d77f6078b
[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.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25
26 import com.google.common.base.Optional;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
30 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
31 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
32 import io.fd.honeycomb.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
56         ListReaderCustomizerTest<SubInterface, SubInterfaceKey, SubInterfaceBuilder> {
57
58     public static final String SUPER_IF_NAME = "local0";
59     public static final int SUPER_IF_INDEX = 1;
60     public static final String VLAN_IF_NAME = "local0.1";
61     public static final int VLAN_IF_ID = 1;
62     public 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 setUpBefore() {
72         interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
73     }
74
75     @Override
76     protected ReaderCustomizer<SubInterface, SubInterfaceBuilder> initCustomizer() {
77         return new SubInterfaceCustomizer(api, interfacesContext);
78     }
79
80     private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long id) {
81         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
82                 SubinterfaceStateAugmentation.class).child(
83                 SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(id));
84     }
85
86     @Test
87     public void testMerge() {
88         final SubInterfacesBuilder builder = mock(SubInterfacesBuilder.class);
89         final  List<SubInterface> value = mock(List.class);
90         getCustomizer().merge(builder, value);
91         verify(builder).setSubInterface(value);
92     }
93
94     @Test
95     public void testRead() throws ReadFailedException {
96         final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(VLAN_IF_NAME, VLAN_IF_INDEX);
97         doReturn(ifcMapping).when(mappingContext).read(any());
98
99         final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
100
101         final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
102         ifaceDetails.subId = VLAN_IF_ID;
103         ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
104         ifaceDetails.subDot1Ad = 1;
105         ifaceDetails.subNumberOfTags = 2;
106         ifaceDetails.subOuterVlanIdAny = 1;
107         ifaceDetails.subInnerVlanIdAny = 1;
108         ifaceDetails.subExactMatch = 1;
109         cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
110         cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
111
112         final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class);
113         getCustomizer().readCurrentAttributes(getSubInterfaceId(VLAN_IF_NAME, VLAN_IF_ID), builder, ctx);
114
115         verify(builder).setIdentifier((long) VLAN_IF_ID);
116
117         ArgumentCaptor<Tags> tagCaptor = ArgumentCaptor.forClass(Tags.class);
118         verify(builder).setTags(tagCaptor.capture());
119         assertEquals(ifaceDetails.subNumberOfTags, tagCaptor.getValue().getTag().size());
120
121         ArgumentCaptor<Match> matchCaptor = ArgumentCaptor.forClass(Match.class);
122         verify(builder).setMatch(matchCaptor.capture());
123         final VlanTagged matchType = (VlanTagged)matchCaptor.getValue().getMatchType();
124         assertTrue(matchType.getVlanTagged().isMatchExactTags());
125     }
126
127     @Test
128     public void testGetAllIds() throws Exception {
129         final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(SUPER_IF_NAME, SUPER_IF_INDEX);
130         doReturn(ifcMapping).when(mappingContext).read(any());
131
132         final SwInterfaceDetails iface = new SwInterfaceDetails();
133         iface.interfaceName = VLAN_IF_NAME.getBytes();
134         iface.swIfIndex = VLAN_IF_INDEX;
135         iface.subId = VLAN_IF_ID;
136         iface.supSwIfIndex = SUPER_IF_INDEX;
137         final List<SwInterfaceDetails> ifaces = Collections.singletonList(iface);
138         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, ifaces);
139
140         final List<SubInterfaceKey> allIds =
141                 getCustomizer().getAllIds(getSubInterfaceId(VLAN_IF_NAME, VLAN_IF_ID), ctx);
142
143         assertEquals(ifaces.size(), allIds.size());
144
145     }
146 }