d70654655af5a4f1a98e897ec04b54419cc2afca
[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.interfaces;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
30 import io.fd.honeycomb.v3po.translate.write.WriteContext;
31 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.ExecutionException;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterfaceBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.dto.CreateSubif;
48 import org.openvpp.jvpp.dto.CreateSubifReply;
49 import org.openvpp.jvpp.future.FutureJVpp;
50
51 public class SubInterfaceCustomizerTest {
52
53     @Mock
54     private FutureJVpp api;
55     @Mock
56     private WriteContext writeContext;
57
58     private NamingContext namingContext;
59     private SubInterfaceCustomizer customizer;
60     public static final String SUPER_IF_NAME = "local0";
61     public static final int SUPER_IF_ID = 1;
62
63     @Before
64     public void setUp() throws Exception {
65         initMocks(this);
66         InterfaceTypeTestUtils.setupWriteContext(writeContext,
67                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class);
68         namingContext = new NamingContext("generatedSubInterfaceName");
69         // TODO create base class for tests using vppApi
70         customizer = new SubInterfaceCustomizer(api, namingContext);
71         namingContext.addName(SUPER_IF_ID, SUPER_IF_NAME);
72     }
73
74     private SubInterface generateSubInterface(final String superIfName) {
75         SubInterfaceBuilder builder = new SubInterfaceBuilder();
76         builder.setVlanType(VlanType._802dot1q);
77         builder.setIdentifier(11L);
78         builder.setNumberOfTags((short)1);
79         builder.setOuterId(new VlanTag(100));
80         builder.setInnerId(new VlanTag(200));
81         builder.setSuperInterface(superIfName);
82         return builder.build();
83     }
84
85     private CreateSubif generateSubInterfaceRequest(final int superIfId) {
86         CreateSubif request = new CreateSubif();
87         request.subId = 11;
88         request.swIfIndex = superIfId;
89         request.oneTag = 1;
90         request.dot1Ad = 1;
91         request.outerVlanId = 100;
92         request.innerVlanId = 200;
93         return request;
94     }
95
96     private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name) {
97         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
98                 VppInterfaceAugmentation.class).child(SubInterface.class);
99     }
100
101     private void whenCreateSubifThen(final int retval) throws ExecutionException, InterruptedException {
102         final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
103         final CreateSubifReply reply = new CreateSubifReply();
104         reply.retval = retval;
105         replyFuture.complete(reply);
106         doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
107     }
108
109     private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException {
110         whenCreateSubifThen(0);
111     }
112
113     private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException {
114         whenCreateSubifThen(-1);
115     }
116
117     private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) {
118         ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
119         verify(api).createSubif(argumentCaptor.capture());
120         final CreateSubif actual = argumentCaptor.getValue();
121
122         assertEquals(expected.swIfIndex, actual.swIfIndex);
123         assertEquals(expected.subId, actual.subId);
124         assertEquals(expected.noTags, actual.noTags);
125         assertEquals(expected.oneTag, actual.oneTag);
126         assertEquals(expected.twoTags, actual.twoTags);
127         assertEquals(expected.dot1Ad, actual.dot1Ad);
128         assertEquals(expected.exactMatch, actual.exactMatch);
129         assertEquals(expected.defaultSub, actual.defaultSub);
130         assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
131         assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
132         assertEquals(expected.outerVlanId, actual.outerVlanId);
133         assertEquals(expected.innerVlanId, actual.innerVlanId);
134         return actual;
135     }
136
137     @Test
138     public void testCreate() throws Exception {
139         final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
140         final String subIfaceName = "local0.sub1";
141         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
142
143         whenCreateSubifThenSuccess();
144
145         customizer.writeCurrentAttributes(id, subInterface, writeContext);
146
147         verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
148         assertTrue(namingContext.containsIndex(subIfaceName));
149     }
150
151     @Test
152     public void testCreateFailed() throws Exception {
153         final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
154         final String subIfaceName = "local0.sub1";
155         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
156
157         whenCreateSubifThenFailure();
158
159         try {
160             customizer.writeCurrentAttributes(id, subInterface, writeContext);
161         } catch (WriteFailedException.CreateFailedException e) {
162             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
163             verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
164             assertFalse(namingContext.containsIndex(subIfaceName));
165             return;
166         }
167         fail("WriteFailedException.CreateFailedException was expected");
168     }
169
170     @Test
171     public void testUpdateNoChange() throws Exception {
172         final SubInterface before = generateSubInterface(SUPER_IF_NAME);
173         final SubInterface after = generateSubInterface(SUPER_IF_NAME);
174         customizer.updateCurrentAttributes(null, before, after, writeContext);
175     }
176
177     @Test(expected = UnsupportedOperationException.class)
178     public void testUpdate() throws Exception {
179         final SubInterface before = generateSubInterface("eth0");
180         final SubInterface after = generateSubInterface("eth1");
181         customizer.updateCurrentAttributes(null, before, after, writeContext);
182     }
183
184     @Test(expected = UnsupportedOperationException.class)
185     public void testDelete() throws Exception {
186         final SubInterface subInterface = generateSubInterface("eth0");
187         customizer.deleteCurrentAttributes(null, subInterface, writeContext);
188     }
189 }