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