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