2 * Copyright (c) 2016 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
8 package org.opendaylight.unimgr.impl;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 @PrepareForTest({UnimgrUtils.class})
49 @RunWith(PowerMockRunner.class)
50 public class UnimgrMapperTest {
52 private static final String BRIDGE_NAME = "br-0";
53 private static final String NODE_ID = "uni://10.0.0.1";
54 private static final String OVSDB_TOPOLOGY_KEY = "ovsdb:1";
55 private static final String EVC_TOPOLOGY_KEY = "unimgr:evc";
56 private static final String UNI_TOPOLOGY_KEY = "unimgr:uni";
57 private static final String PORT_NAME = "port 0";
58 private static final String IPV4_ADDRESS = "10.0.0.1";
61 public void setUp() throws Exception {
62 PowerMockito.whenNew(TopologyKey.class).withAnyArguments().thenReturn(mock(TopologyKey.class));
63 PowerMockito.whenNew(NodeKey.class).withAnyArguments().thenReturn(mock(NodeKey.class));
67 public void testCreateOvsdbBridgeNodeIid() throws Exception {
68 Node ovsdbNode = mock(Node.class);
69 NodeId nodeId = mock(NodeId.class);
70 when(ovsdbNode.getNodeId()).thenReturn(nodeId);
71 when(nodeId.getValue()).thenReturn(NODE_ID);
72 String bridgeNodeName = NODE_ID
73 + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
75 PowerMockito.whenNew(NodeId.class).withArguments(bridgeNodeName).thenReturn(nodeId);
77 InstanceIdentifier<Node> iid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode, BRIDGE_NAME);
78 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
79 assertTrue(iid.firstKeyOf(Node.class).getNodeId().getValue().equalsIgnoreCase(bridgeNodeName));
83 public void testGetEvcLinkIid() {
84 LinkId id = mock(LinkId.class);
85 InstanceIdentifier<Link> iid = UnimgrMapper.getEvcLinkIid(id);
86 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
90 public void testGetEvcTopologyIid() {
91 InstanceIdentifier<Topology> iid = UnimgrMapper.getEvcTopologyIid();
92 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
96 public void testGetEvcTopologyNodeIid() throws Exception {
97 InstanceIdentifier<Node> iid = UnimgrMapper.getEvcTopologyNodeIid();
98 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
101 @SuppressWarnings("unchecked")
103 public void testGetOvsdbBridgeNodeIid() {
104 Node ovsdbNode = mock(Node.class);
105 OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class, Mockito.RETURNS_DEEP_STUBS);
106 when(ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
107 List<ManagedNodeEntry> managedNodes = new ArrayList<>();
108 ManagedNodeEntry managedNodeEntry = mock(ManagedNodeEntry.class);
109 managedNodes.add(managedNodeEntry);
110 when(ovsdbNodeAugmentation.getManagedNodeEntry()).thenReturn(managedNodes);
111 OvsdbBridgeRef ovsdbBridgeRef = mock(OvsdbBridgeRef.class);
112 when(managedNodeEntry.getBridgeRef()).thenReturn(ovsdbBridgeRef);
114 @SuppressWarnings("rawtypes")
115 InstanceIdentifier nodePath = InstanceIdentifier
116 .create(NetworkTopology.class)
117 .child(Topology.class,
118 new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
120 new NodeKey(new NodeId(NODE_ID)));
121 when(ovsdbBridgeRef.getValue()).thenReturn(nodePath);
123 InstanceIdentifier<Node> iid = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode);
125 verify(ovsdbNodeAugmentation).getManagedNodeEntry();
129 * This test for 2 functions with the
130 * same name that take different parameters.
133 public void testGetOvsdbNodeIid() throws Exception {
134 IpAddress ipAddress = mock(IpAddress.class);
135 Ipv4Address ipV4Address = mock(Ipv4Address.class);
136 when(ipAddress.getIpv4Address()).thenReturn(ipV4Address);
137 when(ipV4Address.getValue()).thenReturn(IPV4_ADDRESS);
138 String nodeIdUri = UnimgrConstants.OVSDB_PREFIX
141 + UnimgrConstants.OVSDB_PORT;
142 NodeId nodeId = mock(NodeId.class);
143 PowerMockito.whenNew(NodeId.class).withArguments(nodeIdUri).thenReturn(nodeId);
146 InstanceIdentifier<Node> iid = UnimgrMapper.getOvsdbNodeIid(ipAddress);
147 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
148 assertTrue(iid.firstKeyOf(Node.class).getNodeId().getValue().equalsIgnoreCase(nodeIdUri));
151 iid = UnimgrMapper.getOvsdbNodeIid(nodeId);
152 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
153 assertTrue(iid.firstKeyOf(Node.class).getNodeId().equals(nodeId));
157 public void testGetOvsdbTopologyIid() throws Exception {
158 InstanceIdentifier<Topology> iid = UnimgrMapper.getOvsdbTopologyIid();
159 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
163 * This test for 2 functions with the
164 * same name that take different parameters.
167 public void testGetTerminationPointIid() throws Exception {
168 Node bridgeNode = mock(Node.class);
169 TpId tpId = mock(TpId.class);
171 PowerMockito.whenNew(TerminationPointKey.class).withAnyArguments().thenReturn(mock(TerminationPointKey.class));
172 when(bridgeNode.getKey()).thenReturn(mock(NodeKey.class));
173 PowerMockito.whenNew(TpId.class).withAnyArguments().thenReturn(mock(TpId.class));
176 InstanceIdentifier<TerminationPoint> iid = UnimgrMapper.getTerminationPointIid(bridgeNode, PORT_NAME);
177 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
178 assertTrue(iid.firstKeyOf(TerminationPoint.class).getTpId().getValue().contains(PORT_NAME));
181 iid = UnimgrMapper.getTerminationPointIid(bridgeNode, tpId);
182 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
183 assertTrue(iid.firstKeyOf(TerminationPoint.class).getTpId().equals(tpId));
187 * This test for 2 functions with the
188 * same name that take different parameters.
191 public void testGetUniIid() throws Exception {
192 DataBroker dataBroker = mock(DataBroker.class);
193 IpAddress ip = mock(IpAddress.class);
195 List<Node> uniNodes = new ArrayList<>();
196 Node node = mock(Node.class);
199 PowerMockito.mockStatic(UnimgrUtils.class);
200 PowerMockito.when(UnimgrUtils.getUniNodes(any(DataBroker.class), any(LogicalDatastoreType.class))).thenReturn(uniNodes);
202 UniAugmentation uniAugmentation = mock(UniAugmentation.class);
203 when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation);
204 when(uniAugmentation.getIpAddress()).thenReturn(ip);
205 when(node.getKey()).thenReturn(mock(NodeKey.class));
208 InstanceIdentifier<Node> iid = UnimgrMapper.getUniIid(dataBroker, ip, LogicalDatastoreType.CONFIGURATION);
209 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
212 iid = UnimgrMapper.getUniIid(dataBroker, ip);
213 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
217 public void testGetUniTopologyIid() {
218 InstanceIdentifier<Topology> iid = UnimgrMapper.getUniTopologyIid();
219 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
223 public void testGetUniTopologyNodeIid() {
224 InstanceIdentifier<Node> iid = UnimgrMapper.getUniTopologyNodeIid();
225 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
229 public void testGetUniNodeIid() throws Exception {
230 NodeId nodeId = mock(NodeId.class);
231 PowerMockito.whenNew(NodeId.class).withAnyArguments().thenReturn(nodeId);
233 InstanceIdentifier<Node> iid = UnimgrMapper.getUniNodeIid(nodeId);
234 assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
235 assertTrue(iid.firstKeyOf(Node.class).getNodeId().equals(nodeId));