Add INFO.yaml for GBP
[groupbasedpolicy.git] / neutron-ovsdb / src / test / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / OvsdbHelperTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.groupbasedpolicy.neutron.ovsdb.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.groupbasedpolicy.neutron.ovsdb.AbstractTunnelType;
35 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeVxlan;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62
63 public class OvsdbHelperTest {
64
65     private static final String TUNNEL_PREFIX = "tunnelPrefix";
66     private static final String KEY_1 = "key1";
67     private static final String VALUE_1 = "value1";
68     private static final String KEY_2 = "key2";
69     private static final String VALUE_2 = "value2";
70
71     private DataBroker dataBroker;
72     private InstanceIdentifier<OvsdbTerminationPointAugmentation> tpIid;
73     private OvsdbBridgeAugmentation ovsdbBridgeAugmentation;
74     private OvsdbNodeAugmentation ovsdbNodeAugmentation;
75     private Optional<OvsdbBridgeAugmentation> ovsdbBridgeOptional;
76     private Optional<OvsdbTerminationPointAugmentation> ovsdbTerminationPointOptional;
77     private Optional<Node> nodeOptional;
78     private Node node;
79     private AbstractTunnelType abstractTunnelType;
80     private ReadOnlyTransaction readTransaction;
81     private ReadWriteTransaction readWriteTransaction;
82     private CheckedFuture<Optional<OvsdbBridgeAugmentation>, ReadFailedException> ovsdbBridgeFuture;
83     private CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture;
84     private CheckedFuture<Optional<OvsdbTerminationPointAugmentation>, ReadFailedException> ovsdbTerminationPointFuture;
85
86     @SuppressWarnings({"unchecked", "rawtypes"})
87     @Before
88     public void init() throws Exception {
89         dataBroker = mock(DataBroker.class);
90         tpIid = InstanceIdentifier.create(NetworkTopology.class)
91             .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
92             .child(Node.class)
93             .child(TerminationPoint.class)
94             .augmentation(OvsdbTerminationPointAugmentation.class);
95
96         readTransaction = mock(ReadOnlyTransaction.class);
97         when(dataBroker.newReadOnlyTransaction()).thenReturn(readTransaction);
98         readWriteTransaction = mock(ReadWriteTransaction.class);
99         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
100         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = mock(CheckedFuture.class);
101         when(readWriteTransaction.submit()).thenReturn(submitFuture);
102
103         ovsdbBridgeFuture = mock(CheckedFuture.class);
104         ovsdbBridgeOptional = mock(Optional.class);
105         when(ovsdbBridgeFuture.checkedGet()).thenReturn(ovsdbBridgeOptional);
106         when(ovsdbBridgeOptional.isPresent()).thenReturn(true);
107         ovsdbBridgeAugmentation = mock(OvsdbBridgeAugmentation.class);
108         when(ovsdbBridgeOptional.get()).thenReturn(ovsdbBridgeAugmentation);
109         OvsdbBridgeName bridgeName = mock(OvsdbBridgeName.class);
110         when(ovsdbBridgeAugmentation.getBridgeName()).thenReturn(bridgeName);
111
112         OvsdbNodeRef bareIid = mock(OvsdbNodeRef.class);
113         when(ovsdbBridgeAugmentation.getManagedBy()).thenReturn(bareIid);
114         when(bareIid.getValue()).thenReturn((InstanceIdentifier) InstanceIdentifier.create(Node.class));
115
116         nodeFuture = mock(CheckedFuture.class);
117         nodeOptional = mock(Optional.class);
118         when(nodeFuture.checkedGet()).thenReturn(nodeOptional);
119         when(nodeOptional.isPresent()).thenReturn(true);
120         node = mock(Node.class);
121         when(nodeOptional.get()).thenReturn(node);
122         when(node.getAugmentation(OvsdbBridgeAugmentation.class)).thenReturn(ovsdbBridgeAugmentation);
123
124         ovsdbTerminationPointFuture = mock(CheckedFuture.class);
125         ovsdbTerminationPointOptional = mock(Optional.class);
126         when(ovsdbTerminationPointFuture.checkedGet()).thenReturn(ovsdbTerminationPointOptional);
127         when(ovsdbTerminationPointOptional.isPresent()).thenReturn(true);
128         OvsdbTerminationPointAugmentation ovsdbTp = mock(OvsdbTerminationPointAugmentation.class);
129         when(ovsdbTerminationPointOptional.get()).thenReturn(ovsdbTp);
130
131         abstractTunnelType = mock(AbstractTunnelType.class);
132         when(abstractTunnelType.getTunnelPrefix()).thenReturn(TUNNEL_PREFIX);
133
134         ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
135         when(node.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
136     }
137
138     @SuppressWarnings("unchecked")
139     @Test
140     public void testGetOvsdbBridgeFromTerminationPoint() {
141         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
142             .thenReturn(ovsdbBridgeFuture);
143         assertNotNull(OvsdbHelper.getOvsdbBridgeFromTerminationPoint(tpIid, dataBroker));
144         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
145     }
146
147     @SuppressWarnings("unchecked")
148     @Test
149     public void testGetOvsdbBridgeFromTerminationPoint_PresentFalse() {
150         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
151             .thenReturn(ovsdbBridgeFuture);
152         when(ovsdbBridgeOptional.isPresent()).thenReturn(false);
153         assertNull(OvsdbHelper.getOvsdbBridgeFromTerminationPoint(tpIid, dataBroker));
154         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
155     }
156
157     @SuppressWarnings("unchecked")
158     @Test
159     public void testGetOvsdbBridgeFromTerminationPoint_InvalidIid() {
160         InstanceIdentifier<OvsdbTerminationPointAugmentation> invalidIid =
161                 InstanceIdentifier.create(OvsdbTerminationPointAugmentation.class);
162         assertNull(OvsdbHelper.getOvsdbBridgeFromTerminationPoint(invalidIid, dataBroker));
163         verify(readTransaction, never()).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
164     }
165
166     @SuppressWarnings({"unchecked", "rawtypes"})
167     @Test
168     public void testGetNodeFromBridgeRef() {
169         OvsdbBridgeRef bridgeRef = mock(OvsdbBridgeRef.class);
170         when(bridgeRef.getValue()).thenReturn((InstanceIdentifier) tpIid);
171         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
172             .thenReturn(nodeFuture);
173         assertNotNull(OvsdbHelper.getNodeFromBridgeRef(bridgeRef, dataBroker));
174         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
175     }
176
177     @SuppressWarnings({"unchecked", "rawtypes"})
178     @Test
179     public void testGetNodeFromBridgeRef_PresentFalse() {
180         OvsdbBridgeRef bridgeRef = mock(OvsdbBridgeRef.class);
181         when(bridgeRef.getValue()).thenReturn((InstanceIdentifier) tpIid);
182         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
183             .thenReturn(nodeFuture);
184         when(nodeOptional.isPresent()).thenReturn(false);
185         assertNull(OvsdbHelper.getNodeFromBridgeRef(bridgeRef, dataBroker));
186         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
187     }
188
189     @SuppressWarnings("unchecked")
190     @Test
191     public void testGetOvsdbTerminationPoint() {
192         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
193             .thenReturn(ovsdbTerminationPointFuture);
194         assertNotNull(OvsdbHelper.getOvsdbTerminationPoint(tpIid, dataBroker));
195         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
196     }
197
198     @SuppressWarnings("unchecked")
199     @Test
200     public void testGetOvsdbTerminationPoint_PresentFalse() {
201         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
202             .thenReturn(ovsdbTerminationPointFuture);
203         when(ovsdbTerminationPointOptional.isPresent()).thenReturn(false);
204         assertNull(OvsdbHelper.getOvsdbTerminationPoint(tpIid, dataBroker));
205         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
206     }
207
208     @Test
209     public void testGetNode() {
210         NodeBuilder nodeBuilder = new NodeBuilder();
211         nodeBuilder.setKey(new NodeKey(new NodeId("nodeId")));
212
213         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
214         tpBuilder.setKey(new TerminationPointKey(new TpId("tpId")));
215
216         OvsdbBridgeAugmentationBuilder augmentationBuilder = new OvsdbBridgeAugmentationBuilder();
217         augmentationBuilder.setBridgeName(new OvsdbBridgeName("bridgeName"));
218
219         Node tpNode = OvsdbHelper.getNode(nodeBuilder.build(), Collections.singletonList(tpBuilder.build()),
220                 augmentationBuilder.build());
221         assertNotNull(tpNode);
222         assertEquals("nodeId", tpNode.getKey().getNodeId().getValue());
223         assertEquals(1, tpNode.getTerminationPoint().size());
224         TerminationPoint terminationPoint = tpNode.getTerminationPoint().get(0);
225         assertNotNull(terminationPoint);
226         assertEquals("tpId", terminationPoint.getKey().getTpId().getValue());
227         assertEquals("bridgeName", tpNode.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue());
228     }
229
230     @Test
231     public void testBuildOvsdbBridgeAugmentation() {
232         final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
233             .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
234             .child(Node.class)
235             .build();
236
237         OvsdbBridgeAugmentationBuilder bridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
238         bridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName("bridgeName"));
239         bridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(nodeIid));
240
241         OvsdbNodeAugmentationBuilder nodeAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
242
243         ConnectionInfoBuilder ciBuilder = new ConnectionInfoBuilder();
244         ciBuilder.setLocalIp(new IpAddress(new Ipv4Address("127.0.0.1")));
245         nodeAugmentationBuilder.setConnectionInfo(ciBuilder.build());
246
247         OvsdbBridgeAugmentation augmentation = OvsdbHelper
248             .buildOvsdbBridgeAugmentation(bridgeAugmentationBuilder.build(), nodeAugmentationBuilder.build());
249         assertNotNull(augmentation);
250         assertNotNull("bridgeName", augmentation.getBridgeName().getValue());
251         assertEquals(nodeIid, augmentation.getManagedBy().getValue());
252         assertEquals(1, augmentation.getControllerEntry().size());
253         ControllerEntry controllerEntry = augmentation.getControllerEntry().get(0);
254         assertNotNull(controllerEntry);
255         assertFalse(controllerEntry.getTarget().getValue().isEmpty());
256     }
257
258     @Test
259     public void testBuildOvsdbBridgeAugmentation_ManagerIpNull() {
260         final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
261             .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
262             .child(Node.class)
263             .build();
264
265         OvsdbBridgeAugmentationBuilder bridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
266         bridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName("bridgeName"));
267         bridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(nodeIid));
268
269         OvsdbNodeAugmentationBuilder nodeAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
270
271         OvsdbBridgeAugmentation augmentation = OvsdbHelper
272             .buildOvsdbBridgeAugmentation(bridgeAugmentationBuilder.build(), nodeAugmentationBuilder.build());
273         assertNotNull(augmentation);
274         assertNotNull("bridgeName", augmentation.getBridgeName().getValue());
275         assertEquals(nodeIid, augmentation.getManagedBy().getValue());
276         assertNull(augmentation.getControllerEntry());
277     }
278
279     @Test
280     public void testBuildTerminationPoints() {
281         OvsdbBridgeAugmentationBuilder bridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
282         bridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName("bridgeName"));
283
284         List<Options> options = new ArrayList<>();
285         OvsdbHelper.setOption(options, "optionKey", "optionValue");
286
287         OvsdbTerminationPointAugmentation terminationPointAugmentation = OvsdbHelper
288             .buildOvsdbTerminationPointAugmentation(bridgeAugmentationBuilder.build(), options, abstractTunnelType);
289
290         List<TerminationPoint> terminationPoints = OvsdbHelper.buildTerminationPoints(bridgeAugmentationBuilder.build(),
291                 terminationPointAugmentation, abstractTunnelType);
292         assertNotNull(terminationPoints);
293         assertEquals(1, terminationPoints.size());
294         TerminationPoint terminationPoint = terminationPoints.get(0);
295         assertNotNull(terminationPoint);
296         assertEquals(TUNNEL_PREFIX + "bridgeName", terminationPoint.getTpId().getValue());
297         OvsdbTerminationPointAugmentation tpAugmentation =
298                 terminationPoint.getAugmentation(OvsdbTerminationPointAugmentation.class);
299         assertNotNull(tpAugmentation);
300         assertEquals(TUNNEL_PREFIX + "bridgeName", tpAugmentation.getName());
301         assertEquals(1, tpAugmentation.getOptions().size());
302         Options tpOption = tpAugmentation.getOptions().get(0);
303         assertNotNull(tpOption);
304         assertEquals("optionKey", tpOption.getOption());
305         assertEquals("optionValue", tpOption.getValue());
306         assertEquals(TerminationPoint.class, terminationPoint.getImplementedInterface());
307     }
308
309     @Test
310     public void testBuildOvsdbTerminationPointAugmentation() {
311         OvsdbBridgeAugmentationBuilder bridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
312         bridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName("bridgeName"));
313         List<Options> expectedOptions = new ArrayList<>();
314         OvsdbHelper.setOption(expectedOptions, "optionKey", "optionValue");
315         OvsdbTerminationPointAugmentation augmentation = OvsdbHelper.buildOvsdbTerminationPointAugmentation(
316                 bridgeAugmentationBuilder.build(), expectedOptions, abstractTunnelType);
317         assertNotNull(augmentation);
318         assertEquals(TUNNEL_PREFIX + "bridgeName", augmentation.getName());
319         assertEquals(1, augmentation.getOptions().size());
320         Options option = augmentation.getOptions().get(0);
321         assertNotNull(option);
322         assertEquals("optionKey", option.getOption());
323         assertEquals("optionValue", option.getValue());
324         assertEquals(InterfaceTypeVxlan.class, augmentation.getInterfaceType());
325     }
326
327     @Test
328     public void testSetOption() {
329         List<Options> options = new ArrayList<>();
330         OvsdbHelper.setOption(options, KEY_1, VALUE_1);
331         assertEquals(1, options.size());
332
333         Options option = options.get(0);
334         assertNotNull(option);
335         assertEquals(KEY_1, option.getOption());
336         assertEquals(KEY_1, option.getKey().getOption());
337         assertEquals(VALUE_1, option.getValue());
338
339         OvsdbHelper.setOption(options, KEY_2, VALUE_2);
340         assertEquals(2, options.size());
341
342         option = options.get(0);
343         assertNotNull(option);
344         assertEquals(KEY_1, option.getOption());
345         assertEquals(KEY_1, option.getKey().getOption());
346         assertEquals(VALUE_1, option.getValue());
347
348         option = options.get(1);
349         assertNotNull(option);
350         assertEquals(KEY_2, option.getOption());
351         assertEquals(KEY_2, option.getKey().getOption());
352         assertEquals(VALUE_2, option.getValue());
353     }
354
355     @Test
356     public void testGetNodeIp() {
357         OvsdbNodeAugmentationBuilder nodeAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
358         ConnectionInfoBuilder ciBuilder = new ConnectionInfoBuilder();
359         ciBuilder.setRemoteIp(new IpAddress(new Ipv4Address("192.168.50.10")));
360         nodeAugmentationBuilder.setConnectionInfo(ciBuilder.build());
361
362         IpAddress nodeIpAddress = OvsdbHelper.getNodeIp(nodeAugmentationBuilder.build());
363         assertNotNull(nodeIpAddress);
364         assertEquals("192.168.50.10", nodeIpAddress.getIpv4Address().getValue());
365     }
366
367     @Test
368     public void testGetNodeIp_IpNotSet() {
369         OvsdbNodeAugmentationBuilder nodeAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
370         nodeAugmentationBuilder.setConnectionInfo(new ConnectionInfoBuilder().build());
371         assertNull(OvsdbHelper.getNodeIp(nodeAugmentationBuilder.build()));
372     }
373
374     @Test
375     public void testGetNodeIp_ConnectionInfoNull() {
376         assertNull(OvsdbHelper.getNodeIp(new OvsdbNodeAugmentationBuilder().build()));
377     }
378
379     @Test
380     public void testGetManagedNode_ManagedByNotSet() {
381         assertNull(OvsdbHelper.getManagerNode(new OvsdbBridgeAugmentationBuilder().build(), dataBroker));
382     }
383
384     @SuppressWarnings("unchecked")
385     @Test
386     public void getManagedNode_InvalidTargetTypeForManagedBy() {
387         final InstanceIdentifier<NetworkTopology> nodeIid = InstanceIdentifier.builder(NetworkTopology.class).build();
388         OvsdbBridgeAugmentationBuilder bridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
389         bridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(nodeIid));
390         assertNull(OvsdbHelper.getManagerNode(bridgeAugmentationBuilder.build(), dataBroker));
391         verify(readTransaction, never()).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
392     }
393
394     @SuppressWarnings("unchecked")
395     @Test
396     public void testGetTopologyNode() {
397         InstanceIdentifier<Node> nodeIid = InstanceIdentifier.create(Node.class);
398         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
399             .thenReturn(nodeFuture);
400         assertEquals(node, OvsdbHelper.getTopologyNode(nodeIid, dataBroker));
401         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
402     }
403
404     @SuppressWarnings("unchecked")
405     @Test
406     public void testGetTopologyNode_PresentFalse() {
407         InstanceIdentifier<Node> nodeIid = InstanceIdentifier.create(Node.class);
408         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
409             .thenReturn(nodeFuture);
410         when(nodeOptional.isPresent()).thenReturn(false);
411         assertNull(OvsdbHelper.getTopologyNode(nodeIid, dataBroker));
412         verify(readTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
413     }
414
415     @SuppressWarnings("unchecked")
416     @Test
417     public void testCreateTunnelPort() {
418         InstanceIdentifier<Node> nodeIid = InstanceIdentifier.create(Node.class);
419         when(readWriteTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
420             .thenReturn(nodeFuture);
421         OvsdbHelper.createTunnelPort(nodeIid, node, abstractTunnelType, dataBroker);
422         verify(readWriteTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
423         verify(readWriteTransaction).submit();
424     }
425
426     @Test
427     public void testCreateTunnelPort_BridgeNull() {
428         InstanceIdentifier<Node> nodeIid = InstanceIdentifier.create(Node.class);
429         when(node.getAugmentation(OvsdbBridgeAugmentation.class)).thenReturn(null);
430         OvsdbHelper.createTunnelPort(nodeIid, node, abstractTunnelType, dataBroker);
431         verify(readWriteTransaction, never()).submit();
432     }
433
434     @SuppressWarnings("unchecked")
435     @Test
436     public void testCreateTunnelPort_ManagerNodeNull() {
437         InstanceIdentifier<Node> nodeIid = InstanceIdentifier.create(Node.class);
438         when(readWriteTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
439             .thenReturn(nodeFuture);
440         when(nodeOptional.isPresent()).thenReturn(false);
441         OvsdbHelper.createTunnelPort(nodeIid, node, abstractTunnelType, dataBroker);
442         verify(readWriteTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
443         verify(readWriteTransaction, never()).submit();
444     }
445 }