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.v3po.interfaces.acl.ingress.AclCustomizer;
26 import io.fd.honeycomb.translate.v3po.vppclassifier.VppClassifierContextManager;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import io.fd.vpp.jvpp.VppBaseCallException;
31 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
32 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.acl.base.attributes.L2Acl;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.acl.base.attributes.L2AclBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Acl;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.acl.Ingress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.acl.IngressBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 public class AclCustomizerTest extends WriterCustomizerTest {
49 private VppClassifierContextManager classifyTableContext;
51 private AclCustomizer customizer;
53 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
54 private static final String IF_NAME = "local0";
55 private static final int IF_INDEX = 1;
57 private static final int ACL_TABLE_INDEX = 0;
58 private static final String ACL_TABLE_NAME = "table0";
62 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
63 customizer = new AclCustomizer(api, new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE),
64 classifyTableContext);
67 private InstanceIdentifier<Ingress> getAclId(final String name) {
68 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
69 VppInterfaceAugmentation.class).child(Acl.class).child(Ingress.class);
72 private Ingress generateAcl(final String tableName) {
73 final IngressBuilder builder = new IngressBuilder();
74 final L2Acl l2Acl = new L2AclBuilder().setClassifyTable(tableName).build();
75 builder.setL2Acl(l2Acl);
76 return builder.build();
79 private void whenInputAclSetInterfaceThenSuccess() {
80 doReturn(future(new InputAclSetInterfaceReply())).when(api)
81 .inputAclSetInterface(any(InputAclSetInterface.class));
84 private void whenInputAclSetInterfaceThenFailure() {
85 doReturn(failedFuture()).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
88 private static InputAclSetInterface generateInputAclSetInterface(final byte isAdd, final int ifIndex,
89 final int l2TableIndex) {
90 final InputAclSetInterface request = new InputAclSetInterface();
91 request.isAdd = isAdd;
92 request.l2TableIndex = l2TableIndex;
93 request.ip4TableIndex = ~0;
94 request.ip6TableIndex = ~0;
95 request.swIfIndex = ifIndex;
100 public void testCreate() throws Exception {
101 final Ingress acl = generateAcl(ACL_TABLE_NAME);
102 final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
104 whenInputAclSetInterfaceThenSuccess();
106 customizer.writeCurrentAttributes(id, acl, writeContext);
108 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
112 public void testCreateFailed() throws Exception {
113 final Ingress acl = generateAcl(ACL_TABLE_NAME);
114 final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
116 whenInputAclSetInterfaceThenFailure();
119 customizer.writeCurrentAttributes(id, acl, writeContext);
120 } catch (WriteFailedException e) {
121 assertTrue(e.getCause() instanceof VppBaseCallException);
122 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
125 fail("WriteFailedException.CreateFailedException was expected");
129 public void testDelete() throws Exception {
130 final Ingress acl = generateAcl(ACL_TABLE_NAME);
131 final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
133 whenInputAclSetInterfaceThenSuccess();
135 customizer.deleteCurrentAttributes(id, acl, writeContext);
137 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
141 public void testDeleteFailed() throws Exception {
142 final Ingress acl = generateAcl(ACL_TABLE_NAME);
143 final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
145 whenInputAclSetInterfaceThenFailure();
148 customizer.deleteCurrentAttributes(id, acl, writeContext);
149 } catch (WriteFailedException e) {
150 assertTrue(e.getCause() instanceof VppBaseCallException);
151 verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
154 fail("WriteFailedException.DeleteFailedException was expected");