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.util.concurrent.CheckedFuture;
21 import java.net.InetAddress;
22 import java.net.NetworkInterface;
23 import java.util.Enumeration;
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.api.support.membermodification.MemberMatcher;
45 import org.powermock.api.support.membermodification.MemberModifier;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({SouthboundUtil.class, NetworkInterface.class})
51 public class SouthboundUtilTest {
55 PowerMockito.mockStatic(SouthboundUtil.class, Mockito.CALLS_REAL_METHODS);
58 @SuppressWarnings("unchecked")
60 public void testGetManagingNode() throws Exception {
61 OvsdbBridgeAttributes mn = mock(OvsdbBridgeAttributes.class);
62 DataBroker db = mock(DataBroker.class);
63 OvsdbNodeRef ref = mock(OvsdbNodeRef.class);
64 ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
65 when(db.newReadOnlyTransaction()).thenReturn(transaction);
66 when(mn.getManagedBy()).thenReturn(ref);
67 when(ref.getValue()).thenAnswer(
68 (Answer<InstanceIdentifier<Node>>) invocation -> (InstanceIdentifier<Node>) mock(
69 InstanceIdentifier.class));
70 CheckedFuture<Optional<Node>, ReadFailedException> nf = mock(CheckedFuture.class);
71 when(transaction.read(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class))).thenReturn(nf);
72 doNothing().when(transaction).close();
74 //node, ovsdbNode not null
75 Node node = mock(Node.class);
76 OvsdbNodeAugmentation ovsdbNode = mock(OvsdbNodeAugmentation.class);
77 when(nf.get()).thenReturn(Optional.of(node));
78 when(node.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNode);
79 assertEquals("Failed to return correct Optional object", Optional.of(ovsdbNode),
80 SouthboundUtil.getManagingNode(db, mn));
82 //node not null, ovsdbNode null
83 when(nf.get()).thenReturn(Optional.absent());
84 assertEquals("Failed to return correct Optional object", Optional.absent(),
85 SouthboundUtil.getManagingNode(db, mn));
88 when(nf.get()).thenReturn(null);
89 assertEquals("Failed to return correct Optional object", Optional.absent(),
90 SouthboundUtil.getManagingNode(db, mn));
93 when(mn.getManagedBy()).thenReturn(null);
94 assertEquals("Failed to return correct Optional object", Optional.absent(),
95 SouthboundUtil.getManagingNode(db, mn));
98 @SuppressWarnings("unchecked")
100 public void testReadNode() throws Exception {
101 Optional<DataObject> node = Optional.of(mock(DataObject.class));
102 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
103 InstanceIdentifier<DataObject> connectionIid = mock(InstanceIdentifier.class);
104 CheckedFuture<Optional<DataObject>, ReadFailedException> value = mock(CheckedFuture.class);
105 when(transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid)).thenReturn(value);
106 when(value.checkedGet()).thenReturn(node);
107 assertEquals("Incorrect Optional object received", node, SouthboundUtil.readNode(transaction, connectionIid));
111 public void testGetLocalControllerHostIpAddress() throws Exception {
113 //NetworkInterface.getNetworkInterfaces() returns null case
114 PowerMockito.mockStatic(NetworkInterface.class);
115 when(NetworkInterface.getNetworkInterfaces()).thenReturn(null);
116 assertNull(SouthboundUtil.getLocalControllerHostIpAddress());
118 @SuppressWarnings("unchecked")
119 Enumeration<NetworkInterface> ifaces = mock(Enumeration.class);
120 when(NetworkInterface.getNetworkInterfaces()).thenReturn(ifaces);
121 when(ifaces.hasMoreElements()).thenReturn(true).thenReturn(false);
122 NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
123 when(ifaces.nextElement()).thenReturn(iface);
125 @SuppressWarnings("unchecked")
126 Enumeration<InetAddress> inetAddrs = mock(Enumeration.class);
127 when(iface.getInetAddresses()).thenReturn(inetAddrs);
128 when(inetAddrs.hasMoreElements()).thenReturn(true).thenReturn(false);
129 InetAddress inetAddr = mock(InetAddress.class);
130 when(inetAddrs.nextElement()).thenReturn(inetAddr);
131 when(inetAddr.isLoopbackAddress()).thenReturn(false);
132 when(inetAddr.isSiteLocalAddress()).thenReturn(true);
133 when(inetAddr.getHostAddress()).thenReturn("HostAddress");
134 assertEquals("HostAddress", SouthboundUtil.getLocalControllerHostIpAddress());
138 public void testGetControllerTarget() throws Exception {
139 Node ovsdbNode = mock(Node.class);
140 OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
141 when(ovsdbNode.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
142 ConnectionInfo connectionInfo = mock(ConnectionInfo.class, Mockito.RETURNS_DEEP_STUBS);
143 when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
145 //ipAddr not null case
146 IpAddress ipAddr = new IpAddress(new Ipv4Address("0.0.0.0"));
147 when(connectionInfo.getLocalIp()).thenReturn(ipAddr);
148 String testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
149 + "0.0.0.0" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
150 assertEquals("Incorrect controller IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
151 verify(ovsdbNode).augmentation(OvsdbNodeAugmentation.class);
152 verify(ovsdbNodeAugmentation).getConnectionInfo();
155 when(connectionInfo.getLocalIp()).thenReturn(null);
157 //suppress call to getLocalControllerHostIpAddress()
158 MemberModifier.suppress(MemberMatcher.method(SouthboundUtil.class, "getLocalControllerHostIpAddress"));
159 PowerMockito.when(SouthboundUtil.class, "getLocalControllerHostIpAddress").thenReturn("127.0.0.1");
160 testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
161 + "127.0.0.1" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
162 assertEquals("Incorrect Local controller host IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));