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