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