2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
26 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.v3po.util.TagRewriteOperation;
29 import java.util.HashMap;
30 import java.util.List;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Captor;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceStateAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfaces;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.L2Builder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.tag.rewrite.PushTags;
47 import org.opendaylight.yangtools.yang.binding.ChildOf;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
51 public class RewriteCustomizerTest extends ReaderCustomizerTest<Rewrite, RewriteBuilder> {
53 private static final String IFC_CTX_NAME = "ifc-test-instance";
54 private static final String IF_NAME = "local0";
55 private static final String VLAN_IF_NAME = "local0.1";
56 private static final int VLAN_IF_INDEX = 11;
57 private static final int VLAN_ID = 1;
59 private NamingContext interfacesContext;
62 private ArgumentCaptor<List<PushTags>> captor;
64 public RewriteCustomizerTest() {
70 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
71 ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME);
75 protected ReaderCustomizer<Rewrite, RewriteBuilder> initCustomizer() {
76 return new RewriteCustomizer(api, interfacesContext);
80 public void testMerge() {
81 final L2Builder builder = mock(L2Builder.class);
82 final Rewrite value = mock(Rewrite.class);
83 getCustomizer().merge(builder, value);
84 verify(builder).setRewrite(value);
87 private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
88 final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
89 final InstanceIdentifier id =
90 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation(
91 SubinterfaceStateAugmentation.class).child(SubInterfaces.class)
92 .child(SubInterface.class, new SubInterfaceKey(index))
98 public void testRead() throws ReadFailedException {
99 final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>();
101 final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails();
102 ifaceDetails.subId = VLAN_ID;
103 ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes();
104 ifaceDetails.vtrOp = TagRewriteOperation.translate_2_to_2.ordinal();
105 ifaceDetails.subNumberOfTags = 2;
106 ifaceDetails.vtrTag1 = 123;
107 ifaceDetails.vtrTag2 = 321;
108 ifaceDetails.vtrPushDot1Q = 1;
109 cachedInterfaceDump.put(VLAN_IF_INDEX, ifaceDetails);
110 cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump);
112 final RewriteBuilder builder = mock(RewriteBuilder.class);
114 getCustomizer().readCurrentAttributes(getVlanTagRewriteId(IF_NAME, VLAN_ID), builder, ctx);
116 verify(builder).setVlanType(_802dot1q.class);
117 verify(builder).setPopTags((short) 2);
119 verify(builder).setPushTags(captor.capture());
120 final List<PushTags> tags = captor.getValue();
121 assertEquals(ifaceDetails.subNumberOfTags, tags.size());