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