HONEYCOMB-118: extend classifer model to support node names.
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / AclCustomizerTest.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 io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static junit.framework.TestCase.assertTrue;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.MockitoAnnotations.initMocks;
28
29 import com.google.common.base.Optional;
30 import io.fd.honeycomb.translate.MappingContext;
31 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.translate.v3po.vppclassifier.VppClassifierContextManager;
34 import io.fd.honeycomb.translate.write.WriteContext;
35 import io.fd.honeycomb.translate.write.WriteFailedException;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.ExecutionException;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2Acl;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2AclBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Acl;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.AclBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
54 import org.openvpp.jvpp.VppBaseCallException;
55 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
56 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
57 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
58 import org.openvpp.jvpp.core.future.FutureJVppCore;
59
60 public class AclCustomizerTest {
61
62     @Mock
63     private FutureJVppCore api;
64     @Mock
65     private WriteContext writeContext;
66     @Mock
67     private MappingContext mappingContext;
68     @Mock
69     private VppClassifierContextManager classifyTableContext;
70
71     private NamingContext interfaceContext;
72     private AclCustomizer customizer;
73
74     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
75     private static final String IF_NAME = "local0";
76     private static final int IF_INDEX = 1;
77
78     private static final int ACL_TABLE_INDEX = 0;
79     private static final String ACL_TABLE_NAME = "table0";
80
81     @Before
82     public void setUp() throws Exception {
83         initMocks(this);
84         interfaceContext = new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE);
85         doReturn(mappingContext).when(writeContext).getMappingContext();
86         customizer = new AclCustomizer(api, interfaceContext, classifyTableContext);
87
88         final KeyedInstanceIdentifier<Mapping, MappingKey> ifcMappingKey = getMappingIid(IF_NAME, IFC_TEST_INSTANCE);
89         final Optional<Mapping> ifcMapping = getMapping(IF_NAME, IF_INDEX);
90         doReturn(ifcMapping).when(mappingContext).read(ifcMappingKey);
91     }
92
93
94     private InstanceIdentifier<Acl> getAclId(final String name) {
95         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
96             VppInterfaceAugmentation.class).child(Acl.class);
97     }
98
99     private Acl generateAcl(final String tableName) {
100         final AclBuilder builder = new AclBuilder();
101         final L2Acl l2Acl = new L2AclBuilder().setClassifyTable(tableName).build();
102         builder.setL2Acl(l2Acl);
103         return builder.build();
104     }
105
106     private void whenInputAclSetInterfaceThenSuccess() throws ExecutionException, InterruptedException {
107         final CompletableFuture<InputAclSetInterfaceReply> replyFuture = new CompletableFuture<>();
108         final InputAclSetInterfaceReply reply = new InputAclSetInterfaceReply();
109         replyFuture.complete(reply);
110         doReturn(replyFuture).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
111     }
112
113     private void whenInputAclSetInterfaceThenFailure() throws ExecutionException, InterruptedException {
114         doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
115             .inputAclSetInterface(any(InputAclSetInterface.class));
116     }
117
118     private void verifyInputAclSetInterfaceWasInvoked(final InputAclSetInterface expected) {
119         final ArgumentCaptor<InputAclSetInterface> argumentCaptor = ArgumentCaptor.forClass(InputAclSetInterface.class);
120         verify(api).inputAclSetInterface(argumentCaptor.capture());
121         final InputAclSetInterface actual = argumentCaptor.getValue();
122         assertEquals(expected.swIfIndex, actual.swIfIndex);
123         assertEquals(expected.l2TableIndex, actual.l2TableIndex);
124         assertEquals(expected.ip4TableIndex, actual.ip4TableIndex);
125         assertEquals(expected.ip6TableIndex, actual.ip6TableIndex);
126         assertEquals(expected.isAdd, actual.isAdd);
127     }
128
129     private void verifyInputAclSetInterfaceDisableWasInvoked(final InputAclSetInterface expected) {
130         final ArgumentCaptor<InputAclSetInterface> argumentCaptor = ArgumentCaptor.forClass(InputAclSetInterface.class);
131         verify(api).inputAclSetInterface(argumentCaptor.capture());
132         final InputAclSetInterface actual = argumentCaptor.getValue();
133         assertEquals(expected.swIfIndex, actual.swIfIndex);
134         assertEquals(expected.l2TableIndex, actual.l2TableIndex);
135         assertEquals(0, actual.isAdd);
136     }
137
138     private static InputAclSetInterface generateInputAclSetInterface(final byte isAdd, final int ifIndex,
139                                                                      final int l2TableIndex) {
140         final InputAclSetInterface request = new InputAclSetInterface();
141         request.isAdd = isAdd;
142         request.l2TableIndex = l2TableIndex;
143         request.ip4TableIndex = ~0;
144         request.ip6TableIndex = ~0;
145         request.swIfIndex = ifIndex;
146         return request;
147     }
148
149     @Test
150     public void testCreate() throws Exception {
151         final Acl acl = generateAcl(ACL_TABLE_NAME);
152         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
153
154         whenInputAclSetInterfaceThenSuccess();
155
156         customizer.writeCurrentAttributes(id, acl, writeContext);
157
158         verifyInputAclSetInterfaceWasInvoked(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
159     }
160
161     @Test
162     public void testCreateFailed() throws Exception {
163         final Acl acl = generateAcl(ACL_TABLE_NAME);
164         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
165
166         whenInputAclSetInterfaceThenFailure();
167
168         try {
169             customizer.writeCurrentAttributes(id, acl, writeContext);
170         } catch (WriteFailedException.CreateFailedException e) {
171             assertTrue(e.getCause() instanceof VppBaseCallException);
172             verifyInputAclSetInterfaceWasInvoked(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
173             return;
174         }
175         fail("WriteFailedException.CreateFailedException was expected");
176     }
177
178     @Test
179     public void testDelete() throws Exception {
180         final Acl acl = generateAcl(ACL_TABLE_NAME);
181         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
182
183         whenInputAclSetInterfaceThenSuccess();
184
185         customizer.deleteCurrentAttributes(id, acl, writeContext);
186
187         verifyInputAclSetInterfaceDisableWasInvoked(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
188     }
189
190     @Test
191     public void testDeleteFailed() throws Exception {
192         final Acl acl = generateAcl(ACL_TABLE_NAME);
193         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
194
195         whenInputAclSetInterfaceThenFailure();
196
197         try {
198             customizer.deleteCurrentAttributes(id, acl, writeContext);
199         } catch (WriteFailedException.DeleteFailedException e) {
200             assertTrue(e.getCause() instanceof VppBaseCallException);
201             verifyInputAclSetInterfaceDisableWasInvoked(
202                 generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
203             return;
204         }
205         fail("WriteFailedException.DeleteFailedException was expected");
206     }
207 }