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 junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.v3po.vppclassifier.VppClassifierContextManager;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2Acl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2AclBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Acl;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.AclBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.VppBaseCallException;
41 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
42 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
44 public class AclCustomizerTest extends WriterCustomizerTest {
47 private VppClassifierContextManager classifyTableContext;
49 private AclCustomizer customizer;
51 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
52 private static final String IF_NAME = "local0";
53 private static final int IF_INDEX = 1;
55 private static final int ACL_TABLE_INDEX = 0;
56 private static final String ACL_TABLE_NAME = "table0";
60 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
61 customizer = new AclCustomizer(api, new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE),
62 classifyTableContext);
65 private InstanceIdentifier<Acl> getAclId(final String name) {
66 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
67 VppInterfaceAugmentation.class).child(Acl.class);
70 private Acl generateAcl(final String tableName) {
71 final AclBuilder builder = new AclBuilder();
72 final L2Acl l2Acl = new L2AclBuilder().setClassifyTable(tableName).build();
73 builder.setL2Acl(l2Acl);
74 return builder.build();
77 private void whenInputAclSetInterfaceThenSuccess() {
78 doReturn(future(new InputAclSetInterfaceReply())).when(api)
79 .inputAclSetInterface(any(InputAclSetInterface.class));
82 private void whenInputAclSetInterfaceThenFailure() {
83 doReturn(failedFuture()).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
86 private static InputAclSetInterface generateInputAclSetInterface(final byte isAdd, final int ifIndex,
87 final int l2TableIndex) {
88 final InputAclSetInterface request = new InputAclSetInterface();
89 request.isAdd = isAdd;
90 request.l2TableIndex = l2TableIndex;
91 request.ip4TableIndex = ~0;
92 request.ip6TableIndex = ~0;
93 request.swIfIndex = ifIndex;
98 public void testCreate() throws Exception {
99 final Acl acl = generateAcl(ACL_TABLE_NAME);
100 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
102 whenInputAclSetInterfaceThenSuccess();
104 customizer.writeCurrentAttributes(id, acl, writeContext);
106 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
110 public void testCreateFailed() throws Exception {
111 final Acl acl = generateAcl(ACL_TABLE_NAME);
112 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
114 whenInputAclSetInterfaceThenFailure();
117 customizer.writeCurrentAttributes(id, acl, writeContext);
118 } catch (WriteFailedException.CreateFailedException e) {
119 assertTrue(e.getCause() instanceof VppBaseCallException);
120 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
123 fail("WriteFailedException.CreateFailedException was expected");
127 public void testDelete() throws Exception {
128 final Acl acl = generateAcl(ACL_TABLE_NAME);
129 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
131 whenInputAclSetInterfaceThenSuccess();
133 customizer.deleteCurrentAttributes(id, acl, writeContext);
135 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
139 public void testDeleteFailed() throws Exception {
140 final Acl acl = generateAcl(ACL_TABLE_NAME);
141 final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
143 whenInputAclSetInterfaceThenFailure();
146 customizer.deleteCurrentAttributes(id, acl, writeContext);
147 } catch (WriteFailedException.DeleteFailedException e) {
148 assertTrue(e.getCause() instanceof VppBaseCallException);
149 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
152 fail("WriteFailedException.DeleteFailedException was expected");