70b2e3277f84caaec50de7d7b2a5bbe5ba60659c
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / RewriteCustomizerTest.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.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.translate.vpp.util.TagRewriteOperation;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceStateAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfaces;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.L2Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.tag.rewrite.PushTags;
46 import org.opendaylight.yangtools.yang.binding.ChildOf;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
49
50 public class RewriteCustomizerTest extends ReaderCustomizerTest<Rewrite, RewriteBuilder> {
51
52     private static final String IFC_CTX_NAME = "ifc-test-instance";
53     private static final String IF_NAME = "local0";
54     private static final String VLAN_IF_NAME = "local0.1";
55     private static final int VLAN_IF_INDEX = 11;
56     private static final int VLAN_ID = 1;
57
58     private NamingContext interfacesContext;
59
60     @Captor
61     private ArgumentCaptor<List<PushTags>> captor;
62
63     public RewriteCustomizerTest() {
64         super(Rewrite.class, L2Builder.class);
65     }
66
67     @Override
68     public void setUp() {
69         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
70         defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
71     }
72
73     @Override
74     protected ReaderCustomizer<Rewrite, RewriteBuilder> initCustomizer() {
75         return new RewriteCustomizer(api, interfacesContext);
76     }
77
78     private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
79         final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
80         final InstanceIdentifier id =
81                 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
82                         SubinterfaceStateAugmentation.class).child(SubInterfaces.class)
83                         .child(SubInterface.class, new SubInterfaceKey(index))
84                         .child(child);
85         return id;
86     }
87
88     @Test
89     public void testRead() throws ReadFailedException {
90         final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
91
92         final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
93         ifaceDetails.subId = VLAN_ID;
94         ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
95         ifaceDetails.vtrOp = TagRewriteOperation.translate_2_to_2.ordinal();
96         ifaceDetails.subNumberOfTags = 2;
97         ifaceDetails.vtrTag1 = 123;
98         ifaceDetails.vtrTag2 = 321;
99         ifaceDetails.vtrPushDot1Q = 1;
100         cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
101         cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
102
103         final RewriteBuilder builder = mock(RewriteBuilder.class);
104
105         getCustomizer().readCurrentAttributes(getVlanTagRewriteId(IF_NAME, VLAN_ID), builder, ctx);
106
107         verify(builder).setVlanType(_802dot1q.class);
108         verify(builder).setPopTags((short) 2);
109
110         verify(builder).setPushTags(captor.capture());
111         final List<PushTags> tags = captor.getValue();
112         assertEquals(ifaceDetails.subNumberOfTags, tags.size());
113     }
114 }