2 * Copyright © 2015, 2017 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.southbound;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Iterators;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import java.net.InetAddress;
23 import java.net.NetworkInterface;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.mockito.stubbing.Answer;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({SouthboundUtil.class, NetworkInterface.class})
49 public class SouthboundUtilTest {
53 PowerMockito.mockStatic(SouthboundUtil.class, Mockito.CALLS_REAL_METHODS);
56 @SuppressWarnings("unchecked")
58 public void testGetManagingNode() throws Exception {
59 OvsdbBridgeAttributes mn = mock(OvsdbBridgeAttributes.class);
60 DataBroker db = mock(DataBroker.class);
61 OvsdbNodeRef ref = mock(OvsdbNodeRef.class);
62 ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
63 when(db.newReadOnlyTransaction()).thenReturn(transaction);
64 when(mn.getManagedBy()).thenReturn(ref);
65 when(ref.getValue()).thenAnswer(
66 (Answer<InstanceIdentifier<Node>>) invocation -> (InstanceIdentifier<Node>) mock(
67 InstanceIdentifier.class));
68 CheckedFuture<Optional<Node>, ReadFailedException> nf = mock(CheckedFuture.class);
69 when(transaction.read(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class))).thenReturn(nf);
70 doNothing().when(transaction).close();
72 //node, ovsdbNode not null
73 Node node = mock(Node.class);
74 OvsdbNodeAugmentation ovsdbNode = mock(OvsdbNodeAugmentation.class);
75 when(nf.get()).thenReturn(Optional.of(node));
76 when(node.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNode);
77 assertEquals("Failed to return correct Optional object", Optional.of(ovsdbNode),
78 SouthboundUtil.getManagingNode(db, mn));
80 //node not null, ovsdbNode null
81 when(nf.get()).thenReturn(Optional.absent());
82 assertEquals("Failed to return correct Optional object", Optional.absent(),
83 SouthboundUtil.getManagingNode(db, mn));
86 when(nf.get()).thenReturn(null);
87 assertEquals("Failed to return correct Optional object", Optional.absent(),
88 SouthboundUtil.getManagingNode(db, mn));
91 when(mn.getManagedBy()).thenReturn(null);
92 assertEquals("Failed to return correct Optional object", Optional.absent(),
93 SouthboundUtil.getManagingNode(db, mn));
96 @SuppressWarnings("unchecked")
98 public void testReadNode() throws Exception {
99 Optional<DataObject> node = Optional.of(mock(DataObject.class));
100 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
101 InstanceIdentifier<DataObject> connectionIid = mock(InstanceIdentifier.class);
102 CheckedFuture<Optional<DataObject>, ReadFailedException> value = mock(CheckedFuture.class);
103 when(transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid)).thenReturn(value);
104 when(value.checkedGet()).thenReturn(node);
105 assertEquals("Incorrect Optional object received", node, SouthboundUtil.readNode(transaction, connectionIid));
109 public void testGetLocalControllerHostIpAddress() throws Exception {
111 //NetworkInterface.getNetworkInterfaces() returns null case
112 PowerMockito.mockStatic(NetworkInterface.class);
113 when(NetworkInterface.getNetworkInterfaces()).thenReturn(null);
114 assertNull(SouthboundUtil.getLocalControllerHostIpAddress());
116 InetAddress inetAddr = mock(InetAddress.class);
117 when(inetAddr.isLoopbackAddress()).thenReturn(false);
118 when(inetAddr.isSiteLocalAddress()).thenReturn(true);
119 when(inetAddr.getHostAddress()).thenReturn("HostAddress");
121 NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
122 when(iface.getInetAddresses()).thenReturn(Iterators.asEnumeration(
123 Iterators.singletonIterator(inetAddr)));
125 when(NetworkInterface.getNetworkInterfaces()).thenReturn(Iterators.asEnumeration(
126 Iterators.singletonIterator(iface)));
128 assertEquals("HostAddress", SouthboundUtil.getLocalControllerHostIpAddress());
132 public void testGetControllerTarget() throws Exception {
133 Node ovsdbNode = mock(Node.class);
134 OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
135 when(ovsdbNode.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
136 ConnectionInfo connectionInfo = mock(ConnectionInfo.class, Mockito.RETURNS_DEEP_STUBS);
137 when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
139 //ipAddr not null case
140 IpAddress ipAddr = new IpAddress(new Ipv4Address("0.0.0.0"));
141 when(connectionInfo.getLocalIp()).thenReturn(ipAddr);
142 String testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
143 + "0.0.0.0" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
144 assertEquals("Incorrect controller IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
145 verify(ovsdbNode).augmentation(OvsdbNodeAugmentation.class);
146 verify(ovsdbNodeAugmentation).getConnectionInfo();
149 when(connectionInfo.getLocalIp()).thenReturn(null);
151 //suppress call to getLocalControllerHostIpAddress()
152 PowerMockito.doReturn("127.0.0.1").when(SouthboundUtil.class, "getLocalControllerHostIpAddress");
153 testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
154 + "127.0.0.1" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
155 assertEquals("Incorrect Local controller host IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));