Merge "HwVTEP JUNITs, and fixing show:vxlan CLI"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / test / java / org / opendaylight / vpnservice / interfacemgr / test / HwVTEPConfigurationTest.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.vpnservice.interfacemgr.test;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
26 import org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.confighelpers.HwVTEPConfigRemoveHelper;
27 import org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.confighelpers.HwVTEPInterfaceConfigAddHelper;
28 import org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.statehelpers.HwVTEPInterfaceStateUpdateHelper;
29 import org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.utilities.SouthboundUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParams;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdStatus;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdStatusBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.TunnelInstanceInterfaceMap;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterface;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterfaceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 import java.math.BigInteger;
56 import java.util.ArrayList;
57 import java.util.List;
58
59 import static org.mockito.Matchers.any;
60 import static org.mockito.Mockito.*;
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class HwVTEPConfigurationTest {
64
65     @Mock DataBroker dataBroker;
66     @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
67     @Mock ReadOnlyTransaction mockReadTx;
68     @Mock WriteTransaction mockWriteTx;
69     @Mock InstanceIdentifier<TunnelInstanceInterface> tunnelInterfaceId;
70     @Mock TunnelInstanceInterface tunnelInterface;
71     @Mock Tunnels newTunnel;
72     @Mock Tunnels oldTunnel;
73     HwVTEPInterfaceConfigAddHelper addHelper;
74     HwVTEPConfigRemoveHelper removeHelper;
75     HwVTEPInterfaceStateUpdateHelper updateHelper;
76
77     BigInteger dpId = BigInteger.valueOf(1);
78     Interface hwVTEPInterfaceEnabled;
79     Interface hwVTEPInterfaceDisabled;
80     ParentRefs parentRefs;
81     IfTunnel ifTunnel;
82     InstanceIdentifier<TerminationPoint> tpPath;
83     TerminationPoint terminationPoint;
84     InstanceIdentifier<Node> physicalSwitchId;
85     InstanceIdentifier<Node> globalId;
86     InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier;
87     InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateIdentifier;
88
89     @Before
90     public void setUp() throws Exception {
91         when(dataBroker.registerDataChangeListener(
92                 any(LogicalDatastoreType.class),
93                 any(InstanceIdentifier.class),
94                 any(DataChangeListener.class),
95                 any(DataChangeScope.class)))
96                 .thenReturn(dataChangeListenerRegistration);
97         setupMocks();
98     }
99
100     @After
101     public void cleanUp(){
102     }
103
104     private void setupMocks() {
105         hwVTEPInterfaceEnabled = InterfaceManagerTestUtil.buildTunnelInterface(dpId, InterfaceManagerTestUtil.tunnelInterfaceName ,"Test hwVTEP Interface1", true, TunnelTypeVxlan.class, "192.168.56.101", "192.168.56.102");
106         hwVTEPInterfaceDisabled = InterfaceManagerTestUtil.buildTunnelInterface(dpId, InterfaceManagerTestUtil.tunnelInterfaceName ,"Test hwVTEP Interface1", false, TunnelTypeVxlan.class, "192.168.56.101", "192.168.56.102");
107         interfaceStateIdentifier = IfmUtil.buildStateInterfaceId(hwVTEPInterfaceEnabled.getName());
108         parentRefs = hwVTEPInterfaceEnabled.getAugmentation(ParentRefs.class);
109         ifTunnel = hwVTEPInterfaceEnabled.getAugmentation(IfTunnel.class);
110         physicalSwitchId = SouthboundUtils.createPhysicalSwitchInstanceIdentifier("s1");
111         globalId = SouthboundUtils.createPhysicalSwitchInstanceIdentifier("s1");
112         tunnelsInstanceIdentifier = org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.utilities.SouthboundUtils.
113                 createTunnelsInstanceIdentifier(physicalSwitchId,
114                         ifTunnel.getTunnelSource(), ifTunnel.getTunnelDestination());
115         tunnelInterfaceId = InstanceIdentifier.builder(TunnelInstanceInterfaceMap.class).
116                 child(TunnelInstanceInterface.class, new TunnelInstanceInterfaceKey(tunnelsInstanceIdentifier.toString())).build();
117         tunnelInterface = new TunnelInstanceInterfaceBuilder().
118                 setTunnelInstanceIdentifier(tunnelsInstanceIdentifier.toString()).setKey(new TunnelInstanceInterfaceKey(tunnelsInstanceIdentifier.toString())).setInterfaceName(hwVTEPInterfaceEnabled.getName()).build();
119
120         //Setup termination points
121         TerminationPointKey tpKey = SouthboundUtils.getTerminationPointKey(ifTunnel.getTunnelDestination().getIpv4Address().getValue());
122         tpPath = SouthboundUtils.createInstanceIdentifier(globalId, tpKey);
123         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
124         HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder =
125                 new HwvtepPhysicalLocatorAugmentationBuilder();
126         tpBuilder.setKey(tpKey);
127         tpBuilder.setTpId(tpKey.getTpId());
128         tpAugmentationBuilder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
129         SouthboundUtils.setDstIp(tpAugmentationBuilder, ifTunnel.getTunnelDestination());
130         tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
131         terminationPoint = tpBuilder.build();
132         // Setup mocks
133         when(dataBroker.newReadOnlyTransaction()).thenReturn(mockReadTx);
134         when(dataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTx);
135     }
136
137     @Test
138     public void testAddHwVTEPInterfaceWithGlobalNodeId() {
139         addHelper.addConfiguration(dataBroker, physicalSwitchId, globalId,
140                 hwVTEPInterfaceEnabled, ifTunnel);
141
142         //Verify
143         verify(mockWriteTx).put(LogicalDatastoreType.CONFIGURATION, tpPath, terminationPoint, true);
144         verify(mockWriteTx).put(LogicalDatastoreType.OPERATIONAL, tunnelInterfaceId, tunnelInterface, true);
145     }
146
147     @Test
148     public void testAddHwVTEPInterfaceWithoutGlobalNodeId() {
149
150         addHelper.addConfiguration(dataBroker, physicalSwitchId ,null ,
151                 hwVTEPInterfaceEnabled, ifTunnel);
152
153         //Verification already performed in testAddHwVTEPInterfaceWithGlobalNodeId()
154     }
155
156     @Test
157     public void testAddHwVTEPInterfaceWhenAdminStateDisabled() {
158
159         addHelper.addConfiguration(dataBroker, physicalSwitchId, globalId,
160                 hwVTEPInterfaceDisabled, ifTunnel);
161
162         //Verification already performed in testAddHwVTEPInterfaceWithGlobalNodeId()
163     }
164     @Test
165     public void testDeleteHWVTEPInterface() {
166         removeHelper.removeConfiguration(dataBroker, hwVTEPInterfaceEnabled, physicalSwitchId, globalId);
167         //verification
168         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION, tpPath);
169         verify(mockWriteTx).delete(LogicalDatastoreType.OPERATIONAL, interfaceStateIdentifier);
170         verify(mockWriteTx).delete(LogicalDatastoreType.OPERATIONAL, tunnelsInstanceIdentifier);
171     }
172
173     @Test
174     public void testUpdatePhysicalSwitch(){
175
176         Optional<TunnelInstanceInterface> tunnelInterfaceOptional = Optional.of(tunnelInterface);
177
178         doReturn(Futures.immediateCheckedFuture(tunnelInterfaceOptional)).when(mockReadTx).read(
179                 LogicalDatastoreType.OPERATIONAL, tunnelInterfaceId);
180         List<BfdStatus> bfdStatus = new ArrayList<BfdStatus>();
181         bfdStatus.add(new BfdStatusBuilder().setBfdStatusKey(SouthboundUtils.BFD_OP_STATE).setBfdStatusValue(SouthboundUtils.BFD_STATE_UP).build());
182         List bfdStatusSpy = spy(bfdStatus);
183         when(newTunnel.getBfdStatus()).thenReturn(bfdStatusSpy);
184         updateHelper.updatePhysicalSwitch(dataBroker, tunnelsInstanceIdentifier, newTunnel, oldTunnel);
185
186         //verify
187         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
188                 IfmUtil.buildStateInterfaceId(hwVTEPInterfaceEnabled.getName());
189         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up);
190         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(hwVTEPInterfaceEnabled.getName()));
191         verify(mockWriteTx).merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
192     }
193
194     @Test
195     public void testStartBFDMonitoring(){
196
197         updateHelper.startBfdMonitoring(dataBroker, tunnelsInstanceIdentifier, newTunnel);
198
199         TunnelsBuilder tBuilder = new TunnelsBuilder();
200         tBuilder.setKey(new TunnelsKey(newTunnel.getLocalLocatorRef(), newTunnel.getRemoteLocatorRef()));
201         tBuilder.setLocalLocatorRef(newTunnel.getLocalLocatorRef());
202         tBuilder.setRemoteLocatorRef(newTunnel.getLocalLocatorRef());
203         List <BfdParams> bfdParams = new ArrayList<>();
204         SouthboundUtils.fillBfdParameters(bfdParams, null);
205         tBuilder.setBfdParams(bfdParams);
206         //Verify
207         verify(mockWriteTx).put(LogicalDatastoreType.CONFIGURATION, tunnelsInstanceIdentifier, tBuilder.build(), true);
208     }
209 }