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