9f914cc2e4fe945cd3e0c4e710c717b72b8d3f87
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / TapCustomizerTest.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;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.doAnswer;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
28 import org.junit.Test;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Tap;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.TapBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import io.fd.vpp.jvpp.core.dto.TapConnect;
40 import io.fd.vpp.jvpp.core.dto.TapConnectReply;
41 import io.fd.vpp.jvpp.core.dto.TapDelete;
42 import io.fd.vpp.jvpp.core.dto.TapDeleteReply;
43 import io.fd.vpp.jvpp.core.dto.TapModify;
44 import io.fd.vpp.jvpp.core.dto.TapModifyReply;
45
46 public class TapCustomizerTest extends WriterCustomizerTest {
47
48     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
49     private TapCustomizer tapCustomizer;
50
51     @Override
52     public void setUp() throws Exception {
53         InterfaceTypeTestUtils.setupWriteContext(writeContext,
54             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap.class);
55         tapCustomizer = new TapCustomizer(api, new NamingContext("ifcintest", IFC_TEST_INSTANCE));
56     }
57
58     @Test
59     public void testCreate() throws Exception {
60         doAnswer(new Answer() {
61
62             int idx = 0;
63
64             @Override
65             public Object answer(final InvocationOnMock invocation) throws Throwable {
66                 final TapConnectReply t = new TapConnectReply();
67                 t.swIfIndex = idx++;
68                 return future(t);
69             }
70         }).when(api).tapConnect(any(TapConnect.class));
71
72         tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), writeContext);
73         tapCustomizer.writeCurrentAttributes(getTapId("tap2"), getTapData("tap2", "ff:ff:ff:ff:ff:ff"), writeContext);
74
75         verify(api, times(2)).tapConnect(any(TapConnect.class));
76         verify(mappingContext).put(eq(mappingIid("tap", IFC_TEST_INSTANCE)), eq(
77                 mapping("tap", 0).get()));
78         verify(mappingContext).put(eq(mappingIid("tap2", IFC_TEST_INSTANCE)), eq(
79                 mapping("tap2", 1).get()));
80     }
81
82     @Test
83     public void testModify() throws Exception {
84         final TapConnectReply t = new TapConnectReply();
85         t.swIfIndex = 0;
86         doReturn(future(t)).when(api).tapConnect(any(TapConnect.class));
87
88         final TapModifyReply tmodif = new TapModifyReply();
89         tmodif.swIfIndex = 0;
90         doReturn(future(tmodif)).when(api).tapModify(any(TapModify.class));
91
92         tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), writeContext);
93
94         defineMapping(mappingContext, "tap", 1, IFC_TEST_INSTANCE);
95         tapCustomizer.updateCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), getTapData("tap", "ff:ff:ff:ff:ff:f1"), writeContext);
96
97         verify(api).tapConnect(any(TapConnect.class));
98         verify(api).tapModify(any(TapModify.class));
99
100         verify(mappingContext).put(eq(mappingIid("tap", IFC_TEST_INSTANCE)), eq(
101                 mapping("tap", 0).get()));
102     }
103
104     @Test
105     public void testDelete() throws Exception {
106         final TapConnectReply t = new TapConnectReply();
107         t.swIfIndex = 0;
108         doReturn(future(t)).when(api).tapConnect(any(TapConnect.class));
109
110         doReturn(future(new TapDeleteReply())).when(api).tapDelete(any(TapDelete.class));
111
112         tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), writeContext);
113         defineMapping(mappingContext, "tap", 1, IFC_TEST_INSTANCE);
114         tapCustomizer.deleteCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), writeContext);
115
116         verify(api).tapConnect(any(TapConnect.class));
117         verify(api).tapDelete(any(TapDelete.class));
118         verify(mappingContext).delete(eq(mappingIid("tap", IFC_TEST_INSTANCE)));
119     }
120
121     private InstanceIdentifier<Tap> getTapId(final String tap) {
122         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(tap)).augmentation(
123             VppInterfaceAugmentation.class).child(Tap.class);
124     }
125
126     private Tap getTapData(final String tap, final String mac) {
127         return new TapBuilder().setTapName(tap).setMac(new PhysAddress(mac)).build();
128     }
129 }