53ff77927062cf35e4b2a6bbda945612f12b7652
[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 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;
24
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.vpp.acl.rev161214.acl.base.attributes.L2Acl;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.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;
45
46 public class AclCustomizerTest extends WriterCustomizerTest {
47
48     @Mock
49     private VppClassifierContextManager classifyTableContext;
50
51     private AclCustomizer customizer;
52
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;
56
57     private static final int ACL_TABLE_INDEX = 0;
58     private static final String ACL_TABLE_NAME = "table0";
59
60     @Override
61     public void setUp() {
62         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
63         customizer = new AclCustomizer(api, new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE),
64             classifyTableContext);
65     }
66
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);
70     }
71
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();
77     }
78
79     private void whenInputAclSetInterfaceThenSuccess() {
80         doReturn(future(new InputAclSetInterfaceReply())).when(api)
81             .inputAclSetInterface(any(InputAclSetInterface.class));
82     }
83
84     private void whenInputAclSetInterfaceThenFailure() {
85         doReturn(failedFuture()).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
86     }
87
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;
96         return request;
97     }
98
99     @Test
100     public void testCreate() throws Exception {
101         final Ingress acl = generateAcl(ACL_TABLE_NAME);
102         final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
103
104         whenInputAclSetInterfaceThenSuccess();
105
106         customizer.writeCurrentAttributes(id, acl, writeContext);
107
108         verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
109     }
110
111     @Test
112     public void testCreateFailed() throws Exception {
113         final Ingress acl = generateAcl(ACL_TABLE_NAME);
114         final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
115
116         whenInputAclSetInterfaceThenFailure();
117
118         try {
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));
123             return;
124         }
125         fail("WriteFailedException.CreateFailedException was expected");
126     }
127
128     @Test
129     public void testDelete() throws Exception {
130         final Ingress acl = generateAcl(ACL_TABLE_NAME);
131         final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
132
133         whenInputAclSetInterfaceThenSuccess();
134
135         customizer.deleteCurrentAttributes(id, acl, writeContext);
136
137         verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
138     }
139
140     @Test
141     public void testDeleteFailed() throws Exception {
142         final Ingress acl = generateAcl(ACL_TABLE_NAME);
143         final InstanceIdentifier<Ingress> id = getAclId(IF_NAME);
144
145         whenInputAclSetInterfaceThenFailure();
146
147         try {
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));
152             return;
153         }
154         fail("WriteFailedException.DeleteFailedException was expected");
155     }
156 }