HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / egress / IetfAclCustomizerTest.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.egress;
18
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyZeroInteractions;
21
22 import io.fd.honeycomb.translate.v3po.interfaces.acl.common.IetfAclWriter;
23 import io.fd.honeycomb.translate.vpp.util.NamingContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
26 import java.util.Collections;
27 import org.junit.Test;
28 import org.mockito.Mock;
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.interfaces.rev140508.Interfaces;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.IetfAcl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ietf.acl.Egress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ietf.acl.EgressBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.MixedAcl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessListsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.access.lists.AclBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 public class IetfAclCustomizerTest extends WriterCustomizerTest {
45     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
46     private static final String IF_NAME = "local0";
47     private static final int IF_INDEX = 1;
48     private static final InstanceIdentifier<Egress> IID =
49         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
50             VppInterfaceAugmentation.class).child(IetfAcl.class).child(Egress.class);
51     private static final String ACL_NAME = "acl1";
52     private static final Class<? extends AclBase> ACL_TYPE = MixedAcl.class;
53
54     @Mock
55     private IetfAclWriter aclWriter;
56     private IetfAclCustomizer customizer;
57
58     @Override
59     protected void setUp() {
60         customizer = new IetfAclCustomizer(aclWriter, new NamingContext("prefix", IFC_TEST_INSTANCE));
61         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_TEST_INSTANCE);
62     }
63
64     private static Egress acl(final InterfaceMode mode) {
65         return new EgressBuilder().setAccessLists(
66             new AccessListsBuilder().setAcl(
67                 Collections.singletonList(new AclBuilder()
68                     .setName(ACL_NAME)
69                     .setType(ACL_TYPE)
70                     .build())
71             ).setMode(mode)
72                 .build()
73         ).build();
74     }
75
76     private void verifyWrite(final AccessLists accessLists) throws WriteFailedException {
77         verify(aclWriter)
78             .write(IID, IF_INDEX, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
79                 writeContext, mappingContext);
80     }
81
82     private void verifyDelete() throws WriteFailedException {
83         verify(aclWriter).deleteAcl(IID, IF_INDEX, mappingContext);
84     }
85
86     @Test
87     public void testWriteL3() throws Exception {
88         customizer.writeCurrentAttributes(IID, acl(InterfaceMode.L3), writeContext);
89         verifyZeroInteractions(aclWriter);
90     }
91
92     @Test
93     public void testWriteL2() throws Exception {
94         final Egress acl = acl(InterfaceMode.L2);
95         customizer.writeCurrentAttributes(IID, acl, writeContext);
96         verifyWrite(acl.getAccessLists());
97     }
98
99     @Test
100     public void testUpdate() throws Exception {
101         final Egress aclBefore = acl(InterfaceMode.L3);
102         final Egress aclAfter = acl(InterfaceMode.L2);
103         customizer.updateCurrentAttributes(IID, aclBefore, aclAfter, writeContext);
104         verifyDelete();
105         verifyWrite(aclAfter.getAccessLists());
106     }
107
108     @Test
109     public void testDelete() throws Exception {
110         final Egress acl = acl(InterfaceMode.L2);
111         customizer.deleteCurrentAttributes(IID, acl, writeContext);
112         verifyDelete();
113     }
114 }