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