VPP-378: update jvpp package 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 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.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 io.fd.vpp.jvpp.VppBaseCallException;
41 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
42 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
43
44 public class AclCustomizerTest extends WriterCustomizerTest {
45
46     @Mock
47     private VppClassifierContextManager classifyTableContext;
48
49     private AclCustomizer customizer;
50
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;
54
55     private static final int ACL_TABLE_INDEX = 0;
56     private static final String ACL_TABLE_NAME = "table0";
57
58     @Override
59     public void setUp() {
60         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
61         customizer = new AclCustomizer(api, new NamingContext("generatedInterfaceName", IFC_TEST_INSTANCE),
62             classifyTableContext);
63     }
64
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);
68     }
69
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();
75     }
76
77     private void whenInputAclSetInterfaceThenSuccess() {
78         doReturn(future(new InputAclSetInterfaceReply())).when(api)
79             .inputAclSetInterface(any(InputAclSetInterface.class));
80     }
81
82     private void whenInputAclSetInterfaceThenFailure() {
83         doReturn(failedFuture()).when(api).inputAclSetInterface(any(InputAclSetInterface.class));
84     }
85
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;
94         return request;
95     }
96
97     @Test
98     public void testCreate() throws Exception {
99         final Acl acl = generateAcl(ACL_TABLE_NAME);
100         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
101
102         whenInputAclSetInterfaceThenSuccess();
103
104         customizer.writeCurrentAttributes(id, acl, writeContext);
105
106         verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 1, IF_INDEX, ACL_TABLE_INDEX));
107     }
108
109     @Test
110     public void testCreateFailed() throws Exception {
111         final Acl acl = generateAcl(ACL_TABLE_NAME);
112         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
113
114         whenInputAclSetInterfaceThenFailure();
115
116         try {
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));
121             return;
122         }
123         fail("WriteFailedException.CreateFailedException was expected");
124     }
125
126     @Test
127     public void testDelete() throws Exception {
128         final Acl acl = generateAcl(ACL_TABLE_NAME);
129         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
130
131         whenInputAclSetInterfaceThenSuccess();
132
133         customizer.deleteCurrentAttributes(id, acl, writeContext);
134
135         verify(api).inputAclSetInterface(generateInputAclSetInterface((byte) 0, IF_INDEX, ACL_TABLE_INDEX));
136     }
137
138     @Test
139     public void testDeleteFailed() throws Exception {
140         final Acl acl = generateAcl(ACL_TABLE_NAME);
141         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
142
143         whenInputAclSetInterfaceThenFailure();
144
145         try {
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));
150             return;
151         }
152         fail("WriteFailedException.DeleteFailedException was expected");
153     }
154 }