2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces;
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;
29 import com.google.common.base.Optional;
30 import com.google.common.collect.Lists;
31 import io.fd.honeycomb.translate.MappingContext;
32 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
33 import io.fd.honeycomb.translate.v3po.util.NamingContext;
34 import io.fd.honeycomb.translate.write.WriteContext;
35 import io.fd.honeycomb.translate.write.WriteFailedException;
36 import java.util.List;
37 import java.util.concurrent.CompletableFuture;
38 import java.util.concurrent.ExecutionException;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2Acl;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2AclBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Acl;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.AclBuilder;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57 import org.openvpp.jvpp.VppBaseCallException;
58 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
59 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
60 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
61 import org.openvpp.jvpp.core.future.FutureJVppCore;
63 public class AclCustomizerTest {
66 private FutureJVppCore api;
68 private WriteContext writeContext;
70 private MappingContext mappingContext;
72 private NamingContext interfaceContext;
73 private NamingContext classifyTableContext;
74 private AclCustomizer customizer;
76 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
77 private static final String CT_TEST_INSTANCE = "ct-test-instance";
78 private static final String IF_NAME = "local0";
79 private static final int IF_INDEX = 1;
81 private static final int ACL_TABLE_INDEX = 0;
82 private static final String ACL_TABLE_NAME = "table0";
85 public void setUp() throws Exception {
87 interfaceContext = new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE);
88 classifyTableContext = new NamingContext("generatedClassifyTable", CT_TEST_INSTANCE);
89 doReturn(mappingContext).when(writeContext).getMappingContext();
90 customizer = new AclCustomizer(api, interfaceContext, classifyTableContext);
92 final KeyedInstanceIdentifier<Mapping, MappingKey> ifcMappingKey = getMappingIid(IF_NAME, IFC_TEST_INSTANCE);
93 final Optional<Mapping> ifcMapping = getMapping(IF_NAME, IF_INDEX);
94 doReturn(ifcMapping).when(mappingContext).read(ifcMappingKey);
96 final KeyedInstanceIdentifier<Mapping, MappingKey> ctMappingKey =
97 getMappingIid(ACL_TABLE_NAME, CT_TEST_INSTANCE);
98 final Optional<Mapping> ctMapping = getMapping(ACL_TABLE_NAME, ACL_TABLE_INDEX);
99 doReturn(ctMapping).when(mappingContext).read(ctMappingKey);
101 final List<Mapping> allCtMappings = Lists.newArrayList(ctMapping.get());
102 final Mappings allCtMappingsBaObject = new MappingsBuilder().setMapping(allCtMappings).build();
103 doReturn(Optional.of(allCtMappingsBaObject)).when(mappingContext)
104 .read(ctMappingKey.firstIdentifierOf(Mappings.class));
108 private InstanceIdentifier<Acl> getAclId(final String name) {
109 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
110 VppInterfaceAugmentation.class).child(Acl.class);
113 private Acl generateAcl(final String tableName) {
114 final AclBuilder builder = new AclBuilder();
115 final L2Acl l2Acl = new L2AclBuilder().setClassifyTable(tableName).build();
116 builder.setL2Acl(l2Acl);
117 return builder.build();
120 private void whenInputAclSetInterfaceThenSuccess() throws ExecutionException, InterruptedException {
121 final CompletableFuture<InputAclSetInterfaceReply> replyFuture = new CompletableFuture<>();
122 final InputAclSetInterfaceReply reply = new InputAclSetInterfaceReply();
123 replyFuture.complete(reply);
124 doReturn(replyFuture).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
127 private void whenInputAclSetInterfaceThenFailure() throws ExecutionException, InterruptedException {
128 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
129 .inputAclSetInterface(any(InputAclSetInterface.class));
132 private void verifyInputAclSetInterfaceWasInvoked(final InputAclSetInterface expected) {
133 final ArgumentCaptor<InputAclSetInterface> argumentCaptor = ArgumentCaptor.forClass(InputAclSetInterface.class);
134 verify(api).inputAclSetInterface(argumentCaptor.capture());
135 final InputAclSetInterface actual = argumentCaptor.getValue();
136 assertEquals(expected.swIfIndex, actual.swIfIndex);
137 assertEquals(expected.l2TableIndex, actual.l2TableIndex);
138 assertEquals(expected.ip4TableIndex, actual.ip4TableIndex);
139 assertEquals(expected.ip6TableIndex, actual.ip6TableIndex);
140 assertEquals(expected.isAdd, actual.isAdd);
143 private void verifyInputAclSetInterfaceDisableWasInvoked(final InputAclSetInterface expected) {
144 final ArgumentCaptor<InputAclSetInterface> argumentCaptor = ArgumentCaptor.forClass(InputAclSetInterface.class);
145 verify(api).inputAclSetInterface(argumentCaptor.capture());
146 final InputAclSetInterface actual = argumentCaptor.getValue();
147 assertEquals(expected.swIfIndex, actual.swIfIndex);
148 assertEquals(expected.l2TableIndex, actual.l2TableIndex);
149 assertEquals(0, actual.isAdd);
152 private static InputAclSetInterface generateInputAclSetInterface(final byte isAdd, final int ifIndex,
153 final int l2TableIndex) {
154 final InputAclSetInterface request = new InputAclSetInterface();
155 request.isAdd = isAdd;
156 request.l2TableIndex = l2TableIndex;
157 request.ip4TableIndex = ~0;
158 request.ip6TableIndex = ~0;
159 request.swIfIndex = ifIndex;
164 public void testCreate() throws Exception {
165 final Acl acl = generateAcl(ACL_TABLE_NAME);
166 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
168 whenInputAclSetInterfaceThenSuccess();
170 customizer.writeCurrentAttributes(id, acl, writeContext);
172 verifyInputAclSetInterfaceWasInvoked(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
176 public void testCreateFailed() throws Exception {
177 final Acl acl = generateAcl(ACL_TABLE_NAME);
178 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
180 whenInputAclSetInterfaceThenFailure();
183 customizer.writeCurrentAttributes(id, acl, writeContext);
184 } catch (WriteFailedException.CreateFailedException e) {
185 assertTrue(e.getCause() instanceof VppBaseCallException);
186 verifyInputAclSetInterfaceWasInvoked(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
189 fail("WriteFailedException.CreateFailedException was expected");
193 public void testDelete() throws Exception {
194 final Acl acl = generateAcl(ACL_TABLE_NAME);
195 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
197 whenInputAclSetInterfaceThenSuccess();
199 customizer.deleteCurrentAttributes(id, acl, writeContext);
201 verifyInputAclSetInterfaceDisableWasInvoked(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
205 public void testDeleteFailed() throws Exception {
206 final Acl acl = generateAcl(ACL_TABLE_NAME);
207 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
209 whenInputAclSetInterfaceThenFailure();
212 customizer.deleteCurrentAttributes(id, acl, writeContext);
213 } catch (WriteFailedException.DeleteFailedException e) {
214 assertTrue(e.getCause() instanceof VppBaseCallException);
215 verifyInputAclSetInterfaceDisableWasInvoked(
216 generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
219 fail("WriteFailedException.DeleteFailedException was expected");