Move interface acls to separate yang module
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / AclWriterTest.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;
18
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.translate.write.WriteFailedException;
24 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
25 import java.util.Collections;
26 import java.util.List;
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.AccessLists;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentationBuilder;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.IetfAclBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ietf.acl.IngressBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 public class AclWriterTest extends WriterCustomizerTest {
47
48     private static final String ACL_NAME = "acl1";
49     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
50     private static final InstanceIdentifier<Acl> IID =
51         InstanceIdentifier.create(AccessLists.class).child(Acl.class, new AclKey(ACL_NAME, ACL_TYPE));
52
53     @Mock
54     private Acl acl;
55     private IetfAclWriter customizer;
56
57     @Override
58     public void setUp() {
59         customizer = new IetfAclWriter();
60         when(acl.getAclName()).thenReturn(ACL_NAME);
61         doReturn(ACL_TYPE).when(acl).getAclType();
62     }
63
64     private void defineInterfacesContext(final List<Interface> interfaces) {
65         when(writeContext.readAfter(InstanceIdentifier.create(Interfaces.class))).thenReturn(Optional.of(
66             new InterfacesBuilder().setInterface(interfaces).build()
67         ));
68     }
69
70     @Test
71     public void testWrite() throws Exception {
72         customizer.writeCurrentAttributes(IID, acl, writeContext);
73     }
74
75     @Test
76     public void testUpdate() throws WriteFailedException {
77         defineInterfacesContext(Collections.emptyList());
78         customizer.updateCurrentAttributes(IID, acl, acl, writeContext);
79     }
80
81     @Test
82     public void testDelete() throws WriteFailedException {
83         defineInterfacesContext(Collections.emptyList());
84         customizer.deleteCurrentAttributes(IID, acl, writeContext);
85     }
86
87     @Test(expected = WriteFailedException.class)
88     public void testDeleteFailed() throws WriteFailedException {
89         final Interface iface = new InterfaceBuilder().addAugmentation(VppInterfaceAugmentation.class,
90             new VppInterfaceAugmentationBuilder().setIetfAcl(
91                 new IetfAclBuilder().setIngress(
92                     new IngressBuilder().setAccessLists(
93                         new AccessListsBuilder().setAcl(
94                             Collections.singletonList(new AclBuilder().setName(ACL_NAME).setType(ACL_TYPE).build())
95                         ).build()
96                     ).build()
97                 ).build()
98             ).build()
99         ).build();
100         defineInterfacesContext(Collections.singletonList(iface));
101         customizer.deleteCurrentAttributes(IID, acl, writeContext);
102     }
103 }