HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / SubInterfaceCustomizerTest.java
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.interfaces;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import java.util.Arrays;
32 import java.util.List;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214._802dot1ad;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.SubInterfaces;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.Match;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.MatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.TagsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.Tag;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.TagBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.TagKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import io.fd.vpp.jvpp.VppBaseCallException;
59 import io.fd.vpp.jvpp.core.dto.CreateSubif;
60 import io.fd.vpp.jvpp.core.dto.CreateSubifReply;
61 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
62 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
63
64 public class SubInterfaceCustomizerTest extends WriterCustomizerTest {
65
66     private NamingContext namingContext;
67     private SubInterfaceCustomizer customizer;
68
69     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
70     private static final String SUPER_IF_NAME = "local0";
71     private static final int SUPER_IF_ID = 1;
72     private static final String SUB_IFACE_NAME = "local0.11";
73     private static final int SUBIF_INDEX = 11;
74
75     private static final short STAG_ID = 100;
76     private static final short CTAG_ID = 200;
77     private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
78
79     private final Tag STAG_100;
80     private final Tag CTAG_200;
81     private final Tag CTAG_ANY;
82
83     public SubInterfaceCustomizerTest() {
84         STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
85         CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
86         CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
87     }
88
89     @Override
90     public void setUp() throws Exception {
91         namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
92         customizer = new SubInterfaceCustomizer(api, namingContext);
93         defineMapping(mappingContext, SUB_IFACE_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
94         defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_ID, IFC_TEST_INSTANCE);
95     }
96
97     private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
98         SubInterfaceBuilder builder = new SubInterfaceBuilder();
99         builder.setVlanType(_802dot1ad.class);
100         builder.setIdentifier(11L);
101         final TagsBuilder tags = new TagsBuilder();
102
103         tags.setTag(tagList);
104
105         builder.setTags(tags.build());
106
107         builder.setMatch(generateMatch());
108         builder.setEnabled(enabled);
109         return builder.build();
110     }
111
112     private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
113                                    final Dot1qTag.VlanId vlanId) {
114         TagBuilder tag = new TagBuilder();
115         tag.setIndex(index);
116         tag.setKey(new TagKey(index));
117         final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
118         dtag.setTagType(tagType);
119         dtag.setVlanId(vlanId);
120         tag.setDot1qTag(dtag.build());
121         return tag.build();
122     }
123
124     private static Match generateMatch() {
125         final MatchBuilder match = new MatchBuilder();
126         final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
127         tagged.setMatchExactTags(true);
128         match.setMatchType(
129             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.VlanTaggedBuilder()
130                 .setVlanTagged(tagged.build()).build());
131         return match.build();
132     }
133
134     private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
135                                                     final boolean isInnerAny) {
136         CreateSubif request = new CreateSubif();
137         request.subId = 11;
138         request.swIfIndex = superIfId;
139         request.twoTags = 1;
140         request.innerVlanId = innerVlanId;
141         request.innerVlanIdAny = (byte) (isInnerAny
142             ? 1
143             : 0);
144         request.dot1Ad = 1;
145         request.outerVlanId = STAG_ID;
146         return request;
147     }
148
149     private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
150         SwInterfaceSetFlags request = new SwInterfaceSetFlags();
151         request.swIfIndex = swIfIndex;
152         request.adminUpDown = 1;
153         return request;
154     }
155
156     private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
157         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
158             SubinterfaceAugmentation.class).child(SubInterfaces.class)
159             .child(SubInterface.class, new SubInterfaceKey(index));
160     }
161
162     private void whenCreateSubifThenSuccess() {
163         doReturn(future(new CreateSubifReply())).when(api).createSubif(any(CreateSubif.class));
164     }
165
166     /**
167      * Failure response send
168      */
169     private void whenCreateSubifThenFailure() {
170         doReturn(failedFuture()).when(api).createSubif(any(CreateSubif.class));
171     }
172
173     private void whenSwInterfaceSetFlagsThenSuccess() {
174         doReturn(future(new SwInterfaceSetFlagsReply())).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
175     }
176
177     private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
178         throws VppBaseCallException {
179         ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
180         verify(api).swInterfaceSetFlags(argumentCaptor.capture());
181         final SwInterfaceSetFlags actual = argumentCaptor.getValue();
182
183         assertEquals(expected.swIfIndex, actual.swIfIndex);
184         assertEquals(expected.adminUpDown, actual.adminUpDown);
185         return actual;
186     }
187
188     @Test
189     public void testCreateTwoTags() throws Exception {
190         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
191         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
192
193         whenCreateSubifThenSuccess();
194         whenSwInterfaceSetFlagsThenSuccess();
195
196         customizer.writeCurrentAttributes(id, subInterface, writeContext);
197
198         verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
199         verify(mappingContext)
200             .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
201                 mapping(SUB_IFACE_NAME, 0).get()));
202     }
203
204     @Test
205     public void testCreateDot1qAnyTag() throws Exception {
206         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
207         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
208
209         whenCreateSubifThenSuccess();
210         whenSwInterfaceSetFlagsThenSuccess();
211
212         customizer.writeCurrentAttributes(id, subInterface, writeContext);
213
214         verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
215         verify(mappingContext)
216             .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
217                 mapping(SUB_IFACE_NAME, 0).get()));
218     }
219
220     @Test
221     public void testCreateFailed() throws Exception {
222         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
223         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
224
225         whenCreateSubifThenFailure();
226
227         try {
228             customizer.writeCurrentAttributes(id, subInterface, writeContext);
229         } catch (WriteFailedException.CreateFailedException e) {
230             assertTrue(e.getCause() instanceof VppBaseCallException);
231             verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
232             verify(mappingContext, times(0)).put(
233                 eq(mappingIid(SUPER_IF_NAME, IFC_TEST_INSTANCE)),
234                 eq(mapping(SUPER_IF_NAME, 0).get()));
235             return;
236         }
237         fail("WriteFailedException.CreateFailedException was expected");
238     }
239
240     @Test
241     public void testUpdate() throws Exception {
242         final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
243         final SubInterface before = generateSubInterface(false, tags);
244         final SubInterface after = generateSubInterface(true, tags);
245         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
246
247         whenSwInterfaceSetFlagsThenSuccess();
248         customizer.updateCurrentAttributes(id, before, after, writeContext);
249
250         verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
251     }
252
253     @Test(expected = UnsupportedOperationException.class)
254     public void testDelete() throws Exception {
255         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
256         customizer.deleteCurrentAttributes(null, subInterface, writeContext);
257     }
258 }