76c681ca94f5ece921a539a0e1846f0bbdc0c08a
[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.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.ClassifyTableByInterface;
28 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
29 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
30 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
31 import java.util.Collections;
32 import org.junit.Test;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclKey;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessListsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.access.lists.AclBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.SubInterfaces;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.IetfAcl;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.ietf.acl.Ingress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.ietf.acl.IngressBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51
52 public class SubInterfaceIetfAclCustomizerTest extends WriterCustomizerTest {
53
54     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
55     private static final String IF_NAME = "local0";
56     private static final int IF_INDEX = 1;
57     private static final String SUBIF_NAME = "local0.123";
58     private static final int SUBIF_INDEX = 2;
59     private static final long SUB_IF_ID = 123;
60     private static final InstanceIdentifier<Ingress> IID =
61         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
62             SubinterfaceAugmentation.class).child(SubInterfaces.class)
63             .child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID)).child(IetfAcl.class).child(Ingress.class);
64     private static final String ACL_NAME = "acl1";
65     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
66
67     private SubInterfaceIetfAclCustomizer customizer;
68     private Ingress acl;
69
70     @Override
71     protected void setUp() {
72         customizer =
73             new SubInterfaceIetfAclCustomizer(new IetfAclWriter(api), new NamingContext("prefix", IFC_TEST_INSTANCE));
74         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
75
76         acl = new IngressBuilder().setAccessLists(
77             new AccessListsBuilder().setAcl(
78                 Collections.singletonList(new AclBuilder()
79                     .setName(ACL_NAME)
80                     .setType(ACL_TYPE)
81                     .build())
82             ).build()
83         ).build();
84     }
85
86     private static InputAclSetInterface inputAclSetInterfaceWriteRequest() {
87         final InputAclSetInterface request = new InputAclSetInterface();
88         request.swIfIndex = SUBIF_INDEX;
89         request.isAdd = 1;
90         request.l2TableIndex = -1;
91         request.ip4TableIndex = -1;
92         request.ip6TableIndex = -1;
93         return request;
94     }
95
96     @Test
97     public void testDelete() throws WriteFailedException {
98         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
99         defineMapping(mappingContext, SUBIF_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
100         when(api.classifyTableByInterface(any())).thenReturn(future(noClassifyTablesAssigned()));
101         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
102
103         customizer.deleteCurrentAttributes(IID, acl, writeContext);
104
105         final ClassifyTableByInterface expectedRequest = new ClassifyTableByInterface();
106         expectedRequest.swIfIndex = SUBIF_INDEX;
107         verify(api).classifyTableByInterface(expectedRequest);
108         verify(api).inputAclSetInterface(inputAclSetInterfaceDeleteRequest());
109     }
110
111     private static ClassifyTableByInterfaceReply noClassifyTablesAssigned() {
112         final ClassifyTableByInterfaceReply reply = new ClassifyTableByInterfaceReply();
113         reply.l2TableId = -1;
114         reply.ip4TableId = -1;
115         reply.ip6TableId = -1;
116         return reply;
117     }
118
119     private static InputAclSetInterface inputAclSetInterfaceDeleteRequest() {
120         final InputAclSetInterface request = new InputAclSetInterface();
121         request.l2TableIndex = -1;
122         request.ip4TableIndex = -1;
123         request.ip6TableIndex = -1;
124         return request;
125     }
126
127     @Test
128     public void testWrite() throws WriteFailedException {
129         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
130         defineMapping(mappingContext, SUBIF_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
131
132         when(writeContext.readAfter(IID.firstIdentifierOf(SubInterface.class))).thenReturn(Optional.of(
133             new SubInterfaceBuilder().build()
134         ));
135
136         when(writeContext.readAfter(io.fd.honeycomb.translate.v3po.interfaces.acl.IetfAclWriter.ACL_ID.child(
137             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl.class,
138             new AclKey(ACL_NAME, ACL_TYPE)))).thenReturn(Optional.of(
139             new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder()
140                 .setAccessListEntries(
141                     new AccessListEntriesBuilder().setAce(Collections.emptyList()).build()
142                 ).build()
143         ));
144
145         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
146
147         customizer.writeCurrentAttributes(IID, acl, writeContext);
148
149         verify(api).inputAclSetInterface(inputAclSetInterfaceWriteRequest());
150     }
151 }