58f7ea1a24fd27164243ab85b0565b60e2c9c0de
[hc2vpp.git] /
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.hc2vpp.vpp.classifier.write.acl.ingress;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.argThat;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import com.google.common.base.Optional;
25 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
26 import io.fd.hc2vpp.common.translate.util.NamingContext;
27 import io.fd.hc2vpp.vpp.classifier.write.acl.common.AclTableContextManager;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
30 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
31 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
32 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
33 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
34 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
35 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import org.junit.Test;
39 import org.mockito.ArgumentMatcher;
40 import org.mockito.InOrder;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.AceBuilder;
48 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;
49 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;
50 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.PacketHandling;
51 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.Deny;
52 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;
53 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.Permit;
54 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.PermitBuilder;
55 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;
56 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;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp._interface.acl.rev161214.VppInterfaceAclAugmentation;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntryBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.AccessListsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.access.lists.AclBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.IetfAcl;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.ietf.acl.Ingress;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.ietf.acl.IngressBuilder;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68
69 public class IetfAclCustomizerTest extends WriterCustomizerTest {
70
71     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
72     private static final String IF_NAME = "local0";
73     private static final int IF_INDEX = 1;
74     private static final InstanceIdentifier<Ingress> IID =
75         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
76             VppInterfaceAclAugmentation.class).child(IetfAcl.class).child(Ingress.class);
77     private static final String ACL_NAME = "acl1";
78     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
79
80     @Mock
81     private AclTableContextManager aclCtx;
82
83     private IetfAclCustomizer customizer;
84     private Ingress acl;
85     private int DENY = 0;
86     private int PERMIT = -1;
87
88     private static Ace ace(final PacketHandling action) {
89         return new AceBuilder()
90                 .setMatches(new MatchesBuilder().setAceType(
91                         new AceIpBuilder()
92                                 .setAceIpVersion(new AceIpv6Builder().build())
93                                 .setProtocol((short) 1)
94                                 .build()
95                 ).build())
96                 .setActions(new ActionsBuilder().setPacketHandling(action).build())
97                 .build();
98     }
99
100     private static InputAclSetInterface inputAclSetInterfaceDeleteRequest() {
101         final InputAclSetInterface request = new InputAclSetInterface();
102         request.swIfIndex = IF_INDEX;
103         request.l2TableIndex = 1;
104         request.ip4TableIndex = 2;
105         request.ip6TableIndex = 3;
106         return request;
107     }
108
109     private static ClassifyAddDelTable classifyAddDelTable(final int tableIndex) {
110         final ClassifyAddDelTable reply = new ClassifyAddDelTable();
111         reply.tableIndex = tableIndex;
112         reply.delChain = 1;
113         return reply;
114     }
115
116     private static InputAclSetInterface inputAclSetInterfaceWriteRequest() {
117         final InputAclSetInterface request = new InputAclSetInterface();
118         request.swIfIndex = IF_INDEX;
119         request.isAdd = 1;
120         request.l2TableIndex = -1;
121         request.ip4TableIndex = -1;
122         request.ip6TableIndex = 0;
123         return request;
124     }
125
126     @Override
127     protected void setUpTest() {
128         customizer = new IetfAclCustomizer(new IngressIetfAclWriter(api, aclCtx), new NamingContext("prefix", IFC_TEST_INSTANCE));
129         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
130         acl = new IngressBuilder().setAccessLists(
131             new AccessListsBuilder().setAcl(
132                 Collections.singletonList(new AclBuilder()
133                     .setName(ACL_NAME)
134                     .setType(ACL_TYPE)
135                     .build())
136             ).build()
137         ).build();
138     }
139
140     @Test
141     public void testWrite() throws WriteFailedException {
142         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
143         when(api.classifyAddDelSession(any())).thenReturn(future(new ClassifyAddDelSessionReply()));
144
145         when(writeContext.readAfter(any())).thenReturn(Optional.of(
146             new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder()
147                 .setAccessListEntries(
148                     new AccessListEntriesBuilder().setAce(Arrays.asList(ace(permit()), ace(permit()), ace(deny())
149                     )).build()
150                 ).build()
151
152         ));
153         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
154
155         customizer.writeCurrentAttributes(IID, acl, writeContext);
156
157         final InOrder inOrder = Mockito.inOrder(api);
158         inOrder.verify(api).classifyAddDelTable(argThat(actionOnMissEquals(DENY))); // default action
159         inOrder.verify(api).classifyAddDelTable(any());
160         inOrder.verify(api).classifyAddDelSession(argThat(actionOnHitEquals(DENY))); // last deny ACE
161         inOrder.verify(api).classifyAddDelTable(any());
162         inOrder.verify(api).classifyAddDelSession(argThat(actionOnHitEquals(PERMIT)));
163         inOrder.verify(api).classifyAddDelTable(any());
164         inOrder.verify(api).classifyAddDelSession(argThat(actionOnHitEquals(PERMIT)));
165         inOrder.verify(api).inputAclSetInterface(inputAclSetInterfaceWriteRequest()); // assignment
166     }
167
168     private ArgumentMatcher<ClassifyAddDelTable> actionOnMissEquals(final int action) {
169         return table -> table.missNextIndex == action;
170     }
171
172     private ArgumentMatcher<ClassifyAddDelSession> actionOnHitEquals(final int action) {
173         return session -> session.hitNextIndex == action;
174     }
175
176     private Deny deny() {
177         return new DenyBuilder().build();
178     }
179
180     private Permit permit() {
181         return new PermitBuilder().build();
182     }
183
184     @Test
185     public void testDelete() throws WriteFailedException {
186         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
187         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
188         when(aclCtx.getEntry(IF_INDEX, mappingContext)).thenReturn(Optional.of(
189             new MappingEntryBuilder()
190                 .setIndex(IF_INDEX)
191                 .setL2TableId(1)
192                 .setIp4TableId(2)
193                 .setIp6TableId(3)
194                 .build()));
195
196         customizer.deleteCurrentAttributes(IID, acl, writeContext);
197
198         final ClassifyTableByInterface expectedRequest = new ClassifyTableByInterface();
199         expectedRequest.swIfIndex = IF_INDEX;
200         verify(api).inputAclSetInterface(inputAclSetInterfaceDeleteRequest());
201         verify(api).classifyAddDelTable(classifyAddDelTable(1));
202         verify(api).classifyAddDelTable(classifyAddDelTable(2));
203         verify(api).classifyAddDelTable(classifyAddDelTable(3));
204     }
205 }