84d2306e6f856bff57221b4ae867d097039120e0
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / AclCustomizerTest.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.interfacesstate;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23
24 import com.google.common.base.Optional;
25 import com.google.common.collect.Lists;
26 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
27 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
28 import io.fd.honeycomb.translate.v3po.test.ReaderCustomizerTest;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import java.util.List;
31 import java.util.concurrent.CompletableFuture;
32 import org.junit.Test;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
34 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
36 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2AclBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Acl;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.AclBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.openvpp.jvpp.core.dto.ClassifyTableByInterface;
48 import org.openvpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
49
50 public class AclCustomizerTest extends ReaderCustomizerTest<Acl, AclBuilder> {
51
52     private static final String IF_NAME = "local0";
53     private static final int IF_INDEX = 1;
54     private static final int TABLE_INDEX = 123;
55     private static final String TABLE_NAME = "table123";
56
57     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
58     private static final String CT_TEST_INSTANCE = "ct-test-instance";
59
60     private NamingContext interfaceContext;
61     private NamingContext classifyTableContext;
62
63     public AclCustomizerTest() {
64         super(Acl.class);
65     }
66
67     @Override
68     public void setUpBefore() {
69         interfaceContext = new NamingContext("generatedIfaceName", IFC_TEST_INSTANCE);
70         classifyTableContext = new NamingContext("generatedTableContext", CT_TEST_INSTANCE);
71
72         final KeyedInstanceIdentifier<Mapping, MappingKey> ifcMappingKey = ContextTestUtils
73                 .getMappingIid(IF_NAME, IFC_TEST_INSTANCE);
74         final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(IF_NAME, IF_INDEX);
75         doReturn(ifcMapping).when(mappingContext).read(ifcMappingKey);
76
77         final KeyedInstanceIdentifier<Mapping, MappingKey> ctMappingKey = ContextTestUtils
78                 .getMappingIid(TABLE_NAME, CT_TEST_INSTANCE);
79         final Optional<Mapping> ctMapping = ContextTestUtils.getMapping(TABLE_NAME, TABLE_INDEX);
80         doReturn(ctMapping).when(mappingContext).read(ctMappingKey);
81
82         final List<Mapping> allCtMappings = Lists.newArrayList(ctMapping.get());
83         final Mappings allCtMappingsBaObject = new MappingsBuilder().setMapping(allCtMappings).build();
84         doReturn(Optional.of(allCtMappingsBaObject)).when(mappingContext)
85             .read(ctMappingKey.firstIdentifierOf(Mappings.class));
86
87         final List<Mapping> allIfcMappings = Lists.newArrayList(ifcMapping.get());
88         final Mappings allIfcMappingsBaObject = new MappingsBuilder().setMapping(allIfcMappings).build();
89         doReturn(Optional.of(allIfcMappingsBaObject)).when(mappingContext)
90             .read(ifcMappingKey.firstIdentifierOf(Mappings.class));
91     }
92
93     @Override
94     protected ReaderCustomizer<Acl, AclBuilder> initCustomizer() {
95         return new AclCustomizer(api, interfaceContext, classifyTableContext);
96     }
97
98     @Test
99     public void testMerge() {
100         final VppInterfaceStateAugmentationBuilder builder = mock(VppInterfaceStateAugmentationBuilder.class);
101         final Acl value = mock(Acl.class);
102         getCustomizer().merge(builder, value);
103         verify(builder).setAcl(value);
104     }
105
106     private InstanceIdentifier<Acl> getAclId(final String name) {
107         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name))
108             .augmentation(
109                 VppInterfaceStateAugmentation.class).child(Acl.class);
110     }
111
112     @Test
113     public void testRead() throws Exception {
114         final InstanceIdentifier<Acl> id = getAclId(IF_NAME);
115         final AclBuilder builder = mock(AclBuilder.class);
116
117         final CompletableFuture<ClassifyTableByInterfaceReply> replyFuture = new CompletableFuture<>();
118         final ClassifyTableByInterfaceReply reply = new ClassifyTableByInterfaceReply();
119         reply.l2TableId = TABLE_INDEX;
120         reply.ip4TableId = ~0;
121         reply.ip6TableId = ~0;
122         replyFuture.complete(reply);
123         doReturn(replyFuture).when(api).classifyTableByInterface(any(ClassifyTableByInterface.class));
124
125         getCustomizer().readCurrentAttributes(id, builder, ctx);
126
127         verify(builder).setL2Acl(new L2AclBuilder().setClassifyTable(TABLE_NAME).build());
128         verify(builder).setIp4Acl(null);
129         verify(builder).setIp6Acl(null);
130     }
131
132 }