a0e467a5acdeaacbbb9468a1d57f3566b8e1f791
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / IetfAclCustomizerTest.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.acl.ingress;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
27 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
28 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
29 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
30 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
31 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
32 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
33 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
34 import java.util.Collections;
35 import org.junit.Test;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.AceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.ActionsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.MatchesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.DenyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6Builder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.ietf.acl.base.attributes.AccessListsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.ietf.acl.base.attributes.access.lists.AclBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.IetfAcl;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.ietf.acl.Ingress;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.ietf.acl.IngressBuilder;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55
56 public class IetfAclCustomizerTest extends WriterCustomizerTest {
57
58     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
59     private static final String IF_NAME = "local0";
60     private static final int IF_INDEX = 1;
61     private static final InstanceIdentifier<Ingress> IID = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
62         VppInterfaceAugmentation.class).child(IetfAcl.class).child(Ingress.class);
63     private static final String ACL_NAME = "acl1";
64     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
65
66     private IetfAclCustomizer customizer;
67     private Ingress acl;
68
69     @Override
70     protected void setUp() {
71         customizer = new IetfAclCustomizer(new IetfAClWriter(api), new NamingContext("prefix", IFC_TEST_INSTANCE));
72         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
73         acl = new IngressBuilder().setAccessLists(
74             new AccessListsBuilder().setAcl(
75                 Collections.singletonList(new AclBuilder()
76                     .setName(ACL_NAME)
77                     .setType(ACL_TYPE)
78                     .build())
79             ).build()
80         ).build();
81     }
82
83     @Test
84     public void testWrite() throws WriteFailedException {
85         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
86         when(api.classifyAddDelSession(any())).thenReturn(future(new ClassifyAddDelSessionReply()));
87
88         when(writeContext.readAfter(any())).thenReturn(Optional.of(
89             new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder()
90                 .setAccessListEntries(
91                     new AccessListEntriesBuilder().setAce(Collections.singletonList(
92                         new AceBuilder()
93                             .setMatches(new MatchesBuilder().setAceType(
94                                 new AceIpBuilder()
95                                     .setAceIpVersion(new AceIpv6Builder().build())
96                                     .setProtocol((short)1)
97                                     .build()
98                             ).build())
99                             .setActions(new ActionsBuilder().setPacketHandling(new DenyBuilder().build()).build())
100                             .build()
101                     )).build()
102                 ).build()
103
104         ));
105         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
106
107         customizer.writeCurrentAttributes(IID, acl, writeContext);
108
109         verify(api).classifyAddDelTable(any());
110         verify(api).classifyAddDelSession(any());
111         verify(api).inputAclSetInterface(inputAclSetInterfaceWriteRequest());
112     }
113
114     @Test
115     public void testDelete() throws WriteFailedException {
116         when(api.classifyTableByInterface(any())).thenReturn(future(classifyTableByInterfaceReply()));
117         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
118         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
119
120         customizer.deleteCurrentAttributes(IID, acl, writeContext);
121
122         final ClassifyTableByInterface expectedRequest = new ClassifyTableByInterface();
123         expectedRequest.swIfIndex = IF_INDEX;
124         verify(api).classifyTableByInterface(expectedRequest);
125         verify(api).inputAclSetInterface(inputAclSetInterfaceDeleteRequest());
126         verify(api).classifyAddDelTable(classifyAddDelTable(1));
127         verify(api).classifyAddDelTable(classifyAddDelTable(2));
128         verify(api).classifyAddDelTable(classifyAddDelTable(3));
129     }
130
131     private static InputAclSetInterface inputAclSetInterfaceDeleteRequest() {
132         final InputAclSetInterface request = new InputAclSetInterface();
133         request.l2TableIndex = 1;
134         request.ip4TableIndex = 2;
135         request.ip6TableIndex = 3;
136         return request;
137     }
138
139     private static ClassifyAddDelTable classifyAddDelTable(final int tableIndex) {
140         final ClassifyAddDelTable reply = new ClassifyAddDelTable();
141         reply.tableIndex = tableIndex;
142         return reply;
143     }
144
145     private static ClassifyTableByInterfaceReply classifyTableByInterfaceReply() {
146         final ClassifyTableByInterfaceReply reply = new ClassifyTableByInterfaceReply();
147         reply.l2TableId = 1;
148         reply.ip4TableId = 2;
149         reply.ip6TableId = 3;
150         return reply;
151     }
152
153     private static InputAclSetInterface inputAclSetInterfaceWriteRequest() {
154         final InputAclSetInterface request = new InputAclSetInterface();
155         request.swIfIndex = IF_INDEX;
156         request.isAdd = 1;
157         request.l2TableIndex = -1;
158         request.ip4TableIndex = -1;
159         request.ip6TableIndex = 0;
160         return request;
161     }
162 }