e015a530fdbbf210ffba4c8c3eccad674639c48a
[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.egress;
18
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyZeroInteractions;
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.common.IetfAclWriter;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.util.Collections;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.InterfaceMode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.MixedAcl;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.ietf.acl.base.attributes.AccessLists;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.ietf.acl.base.attributes.AccessListsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.ietf.acl.base.attributes.access.lists.AclBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.vpp.acl.attributes.IetfAcl;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.vpp.acl.attributes.ietf.acl.Egress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.vpp.acl.attributes.ietf.acl.EgressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.subinterface.acl.rev170315.VppSubinterfaceAclAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170315.SubinterfaceAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170315.interfaces._interface.SubInterfaces;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170315.interfaces._interface.sub.interfaces.SubInterface;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170315.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170315.interfaces._interface.sub.interfaces.SubInterfaceKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50
51 public class SubInterfaceIetfAclCustomizerTest extends WriterCustomizerTest {
52     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
53     private static final String IF_NAME = "local0";
54     private static final int IF_INDEX = 1;
55     private static final String SUBIF_NAME = "local0.0";
56     private static final int SUBIF_INDEX = 11;
57     private static final long SUBIF_ID = 0;
58     private static final InstanceIdentifier<Egress> 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(IetfAcl.class).child(Egress.class);
64     private static final String ACL_NAME = "acl1";
65     private static final Class<? extends AclBase> ACL_TYPE = MixedAcl.class;
66
67     @Mock
68     private IetfAclWriter aclWriter;
69     private SubInterfaceIetfAclCustomizer customizer;
70
71     private static Egress acl(final InterfaceMode mode) {
72         return new EgressBuilder().setAccessLists(
73             new AccessListsBuilder().setAcl(
74                 Collections.singletonList(new AclBuilder()
75                     .setName(ACL_NAME)
76                     .setType(ACL_TYPE)
77                     .build())
78             ).setMode(mode)
79                 .build()
80         ).build();
81     }
82
83     @Override
84     protected void setUpTest() {
85         customizer = new SubInterfaceIetfAclCustomizer(aclWriter, new NamingContext("prefix", IFC_TEST_INSTANCE));
86         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
87         defineMapping(mappingContext, SUBIF_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
88
89
90         when(writeContext.readAfter(IID.firstIdentifierOf(SubInterface.class))).thenReturn(Optional.of(
91                 new SubInterfaceBuilder().build()
92         ));
93     }
94
95     private void verifyWrite(final AccessLists accessLists) throws WriteFailedException {
96         verify(aclWriter)
97             .write(IID, SUBIF_INDEX, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
98                 writeContext, 0, mappingContext);
99     }
100
101     private void verifyDelete() throws WriteFailedException {
102         verify(aclWriter).deleteAcl(IID, SUBIF_INDEX, mappingContext);
103     }
104
105     @Test
106     public void testWriteL3() throws Exception {
107         customizer.writeCurrentAttributes(IID, acl(InterfaceMode.L3), writeContext);
108         verifyZeroInteractions(aclWriter);
109     }
110
111     @Test
112     public void testWriteL2() throws Exception {
113         final Egress acl = acl(InterfaceMode.L2);
114         customizer.writeCurrentAttributes(IID, acl, writeContext);
115         verifyWrite(acl.getAccessLists());
116     }
117
118     @Test
119     public void testUpdate() throws Exception {
120         final Egress aclBefore = acl(InterfaceMode.L3);
121         final Egress aclAfter = acl(InterfaceMode.L2);
122         customizer.updateCurrentAttributes(IID, aclBefore, aclAfter, writeContext);
123         verifyDelete();
124         verifyWrite(aclAfter.getAccessLists());
125     }
126
127     @Test
128     public void testDelete() throws Exception {
129         final Egress acl = acl(InterfaceMode.L2);
130         customizer.deleteCurrentAttributes(IID, acl, writeContext);
131         verifyDelete();
132     }
133 }