2 * Copyright (c) 2015 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.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
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.lang.reflect.Field;
22 import java.net.InetAddress;
23 import java.net.NetworkInterface;
24 import java.util.Enumeration;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
33 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.api.support.membermodification.MemberMatcher;
46 import org.powermock.api.support.membermodification.MemberModifier;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.powermock.reflect.Whitebox;
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({SouthboundUtil.class, NetworkInterface.class})
53 public class SouthboundUtilTest {
57 PowerMockito.mockStatic(SouthboundUtil.class, Mockito.CALLS_REAL_METHODS);
61 public void testSetInstanceIdentifierCodec() throws Exception {
62 InstanceIdentifierCodec iidc = mock(InstanceIdentifierCodec.class);
63 SouthboundUtil.setInstanceIdentifierCodec(iidc);
64 assertEquals("InstanceIdentifierCodec object not correctly set", iidc,
65 SouthboundUtil.getInstanceIdentifierCodec());
69 public void testSerializeInstanceIdentifier() throws Exception {
70 InstanceIdentifier<?> iid = mock(InstanceIdentifier.class);
71 InstanceIdentifierCodec iidc = (InstanceIdentifierCodec) setField("instanceIdentifierCodec",
72 mock(InstanceIdentifierCodec.class));
73 when(iidc.serialize(iid)).thenReturn("serializeInstanceIdentifier");
74 assertEquals("Incorrect String returned", "serializeInstanceIdentifier",
75 SouthboundUtil.serializeInstanceIdentifier(iid));
76 verify(iidc).serialize(any(InstanceIdentifier.class));
79 @SuppressWarnings({ "unchecked", "rawtypes" })
81 public void testDeserializeInstanceIdentifier() throws Exception {
82 InstanceIdentifier result = mock(InstanceIdentifier.class);
83 InstanceIdentifierCodec iidc = (InstanceIdentifierCodec) setField("instanceIdentifierCodec",
84 mock(InstanceIdentifierCodec.class));
85 when(iidc.bindingDeserializer(anyString())).thenReturn(result);
86 assertEquals(result, SouthboundUtil.deserializeInstanceIdentifier("iidString"));
87 verify(iidc).bindingDeserializer(anyString());
90 @SuppressWarnings("unchecked")
92 public void testGetManagingNode() throws Exception {
93 OvsdbBridgeAttributes mn = mock(OvsdbBridgeAttributes.class);
94 DataBroker db = mock(DataBroker.class);
95 OvsdbNodeRef ref = mock(OvsdbNodeRef.class);
96 ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
97 when(db.newReadOnlyTransaction()).thenReturn(transaction);
98 when(mn.getManagedBy()).thenReturn(ref);
99 when(ref.getValue()).thenAnswer(new Answer<InstanceIdentifier<Node>>() {
100 public InstanceIdentifier<Node> answer(InvocationOnMock invocation) throws Throwable {
101 return (InstanceIdentifier<Node>) mock(InstanceIdentifier.class);
104 CheckedFuture<Optional<Node>, ReadFailedException> nf = mock(CheckedFuture.class);
105 when(transaction.read(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class))).thenReturn(nf);
106 doNothing().when(transaction).close();
107 Optional<Node> optional = mock(Optional.class);
108 when(nf.get()).thenReturn(optional);
109 when(optional.isPresent()).thenReturn(true);
111 //node, ovsdbNode not null
112 Node node = mock(Node.class);
113 OvsdbNodeAugmentation ovsdbNode = mock(OvsdbNodeAugmentation.class);
114 when(optional.get()).thenReturn(node);
115 when(node.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNode);
116 assertEquals("Failed to return correct Optional object", Optional.of(ovsdbNode),
117 SouthboundUtil.getManagingNode(db, mn));
119 //node not null, ovsdbNode null
120 when(optional.get()).thenReturn(null);
121 assertEquals("Failed to return correct Optional object", Optional.absent(),
122 SouthboundUtil.getManagingNode(db, mn));
125 when(nf.get()).thenReturn(null);
126 assertEquals("Failed to return correct Optional object", Optional.absent(),
127 SouthboundUtil.getManagingNode(db, mn));
130 when(mn.getManagedBy()).thenReturn(null);
131 assertEquals("Failed to return correct Optional object", Optional.absent(),
132 SouthboundUtil.getManagingNode(db, mn));
135 @SuppressWarnings("unchecked")
137 public void testReadNode() throws Exception {
138 Optional<DataObject> node = mock(Optional.class);
139 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
140 InstanceIdentifier<DataObject> connectionIid = mock(InstanceIdentifier.class);
141 CheckedFuture<Optional<DataObject>, ReadFailedException> value = mock(CheckedFuture.class);
142 when(transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid)).thenReturn(value);
143 when(value.checkedGet()).thenReturn(node);
144 assertEquals("Incorrect Optional object received", node, SouthboundUtil.readNode(transaction, connectionIid));
147 @SuppressWarnings("unchecked")
149 public void testGetLocalControllerHostIpAddress() throws Exception {
151 //NetworkInterface.getNetworkInterfaces() returns null case
152 PowerMockito.mockStatic(NetworkInterface.class);
153 when(NetworkInterface.getNetworkInterfaces()).thenReturn(null);
154 assertEquals(null, Whitebox.invokeMethod(SouthboundUtil.class, "getLocalControllerHostIpAddress"));
156 Enumeration<NetworkInterface> ifaces = mock(Enumeration.class);
157 when(NetworkInterface.getNetworkInterfaces()).thenReturn(ifaces);
158 when(ifaces.hasMoreElements()).thenReturn(true).thenReturn(false);
159 NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
160 when(ifaces.nextElement()).thenReturn(iface);
162 Enumeration<InetAddress> inetAddrs = mock(Enumeration.class);
163 when(iface.getInetAddresses()).thenReturn(inetAddrs);
164 when(inetAddrs.hasMoreElements()).thenReturn(true).thenReturn(false);
165 InetAddress inetAddr = mock(InetAddress.class);
166 when(inetAddrs.nextElement()).thenReturn(inetAddr);
167 when(inetAddr.isLoopbackAddress()).thenReturn(false);
168 when(inetAddr.isSiteLocalAddress()).thenReturn(true);
169 when(inetAddr.getHostAddress()).thenReturn("HostAddress");
170 assertEquals("HostAddress", Whitebox.invokeMethod(SouthboundUtil.class, "getLocalControllerHostIpAddress"));
174 public void testGetControllerTarget() throws Exception {
175 Node ovsdbNode = mock(Node.class);
176 OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
177 when(ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
178 ConnectionInfo connectionInfo = mock(ConnectionInfo.class, Mockito.RETURNS_DEEP_STUBS);
179 when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
181 //ipAddr not null case
182 IpAddress ipAddr = mock(IpAddress.class);
183 when(connectionInfo.getLocalIp()).thenReturn(ipAddr);
184 char[] ipAddress = {'0', '.', '0', '.', '0', '.', '0'};
185 when(connectionInfo.getLocalIp().getValue()).thenReturn(ipAddress);
186 String testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
187 + String.valueOf(ipAddress) + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
188 assertEquals("Incorrect controller IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
189 verify(ovsdbNode).getAugmentation(OvsdbNodeAugmentation.class);
190 verify(ovsdbNodeAugmentation).getConnectionInfo();
193 when(connectionInfo.getLocalIp()).thenReturn(null);
195 //suppress call to getLocalControllerHostIpAddress()
196 MemberModifier.suppress(MemberMatcher.method(SouthboundUtil.class, "getLocalControllerHostIpAddress"));
197 PowerMockito.when(SouthboundUtil.class, "getLocalControllerHostIpAddress").thenReturn("127.0.0.1");
198 testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
199 + "127.0.0.1" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
200 assertEquals("Incorrect Local controller host IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
203 private Object getField(String fieldName) throws Exception {
204 Field field = SouthboundUtil.class.getDeclaredField(fieldName);
205 field.setAccessible(true);
206 return field.get(SouthboundUtil.class);
209 private Object setField(String fieldName, InstanceIdentifierCodec fieldValue) throws Exception {
210 Field field = SouthboundUtil.class.getDeclaredField(fieldName);
211 field.setAccessible(true);
212 field.set(field.get(SouthboundUtil.class), fieldValue);
213 return field.get(SouthboundUtil.class);