d1a83bcfec7480a1ae4ccc01e482b1dd4e996e95
[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 io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
24 import io.fd.hc2vpp.common.translate.util.NamingContext;
25 import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
28 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.acl.base.attributes.Ip4Acl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.acl.base.attributes.Ip4AclBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.acl.base.attributes.Ip6Acl;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.acl.base.attributes.Ip6AclBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.Acl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.acl.Ingress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.acl.IngressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.subinterface.acl.rev170315.VppSubinterfaceAclAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170607.SubinterfaceAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170607.interfaces._interface.SubInterfaces;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170607.interfaces._interface.sub.interfaces.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170607.interfaces._interface.sub.interfaces.SubInterfaceKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47
48 public class SubInterfaceAclCustomizerTest extends WriterCustomizerTest {
49     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
50     private static final String IF_NAME = "local0";
51     private static final int IF_INDEX = 1;
52     private static final String SUBIF_NAME = "local0.0";
53     private static final int SUBIF_INDEX = 11;
54     private static final long SUBIF_ID = 0;
55     private static final String TABLE_NAME = "table0";
56     private static final int TABLE_INDEX = 123;
57
58     private static final InstanceIdentifier<Ingress> IID =
59             InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
60                     SubinterfaceAugmentation.class).child(SubInterfaces.class)
61                     .child(SubInterface.class, new SubInterfaceKey(SUBIF_ID))
62                     .augmentation(VppSubinterfaceAclAugmentation.class)
63                     .child(Acl.class).child(Ingress.class);
64
65     @Mock
66     private VppClassifierContextManager classifyTableContext;
67
68     private SubInterfaceAclCustomizer customizer;
69
70     @Override
71     protected void setUpTest() throws Exception {
72         customizer = new SubInterfaceAclCustomizer(api, new NamingContext("prefix", IFC_TEST_INSTANCE),
73                 classifyTableContext);
74         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
75         defineMapping(mappingContext, SUBIF_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
76         when(classifyTableContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
77     }
78
79     @Test
80     public void testCreate() throws WriteFailedException {
81         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
82         customizer.writeCurrentAttributes(IID, ip4Acl(), writeContext);
83         verify(api).inputAclSetInterface(expectedIp4AclRequest());
84     }
85
86     @Test(expected = WriteFailedException.class)
87     public void testCreateFailed() throws WriteFailedException {
88         when(api.inputAclSetInterface(any())).thenReturn(failedFuture());
89         customizer.writeCurrentAttributes(IID, ip4Acl(), writeContext);
90     }
91
92     @Test(expected = UnsupportedOperationException.class)
93     public void testUpdate() throws WriteFailedException {
94         customizer.updateCurrentAttributes(IID, ip4Acl(), ip6Acl(), writeContext);
95     }
96
97     @Test
98     public void testDelete() throws Exception {
99         when(api.inputAclSetInterface(any())).thenReturn(future(new InputAclSetInterfaceReply()));
100         customizer.deleteCurrentAttributes(IID, ip6Acl(), writeContext);
101         verify(api).inputAclSetInterface(expectedIp6AclRequest());
102     }
103
104     @Test(expected = WriteFailedException.class)
105     public void testDeleteFailed() throws WriteFailedException {
106         when(api.inputAclSetInterface(any())).thenReturn(failedFuture());
107         customizer.deleteCurrentAttributes(IID, ip4Acl(), writeContext);
108     }
109
110     private Ingress ip4Acl() {
111         final IngressBuilder builder = new IngressBuilder();
112         final Ip4Acl acl = new Ip4AclBuilder().setClassifyTable(TABLE_NAME).build();
113         builder.setIp4Acl(acl);
114         return builder.build();
115     }
116
117     private InputAclSetInterface expectedIp4AclRequest() {
118         final InputAclSetInterface request = new InputAclSetInterface();
119         request.isAdd = 1;
120         request.l2TableIndex = -1;
121         request.ip4TableIndex = TABLE_INDEX;
122         request.ip6TableIndex = -1;
123         request.swIfIndex = SUBIF_INDEX;
124         return request;
125     }
126
127     private Ingress ip6Acl() {
128         final IngressBuilder builder = new IngressBuilder();
129         final Ip6Acl acl = new Ip6AclBuilder().setClassifyTable(TABLE_NAME).build();
130         builder.setIp6Acl(acl);
131         return builder.build();
132     }
133
134     private InputAclSetInterface expectedIp6AclRequest() {
135         final InputAclSetInterface request = new InputAclSetInterface();
136         request.isAdd = 0;
137         request.l2TableIndex = -1;
138         request.ip4TableIndex = -1;
139         request.ip6TableIndex = TABLE_INDEX;
140         request.swIfIndex = SUBIF_INDEX;
141         return request;
142     }
143 }