2 * Copyright (c) 2016 CableLabs 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.command;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.when;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.unimgr.impl.UnimgrConstants;
24 import org.opendaylight.unimgr.impl.UnimgrMapper;
25 import org.opendaylight.unimgr.utils.MdsalUtils;
26 import org.opendaylight.unimgr.utils.OvsdbUtils;
27 import org.opendaylight.unimgr.utils.UniUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.yangtools.yang.binding.InstanceIdentifier;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 import com.google.common.base.Optional;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({UniUtils.class, OvsdbUtils.class, MdsalUtils.class, UnimgrMapper.class})
50 public class UniAddCommandTest {
52 private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8");
54 private UniAddCommand uniAddCommand;
55 private DataTreeModification<Node> uni;
56 private DataBroker dataBroker;
58 private Optional<Node> optUniNode;
60 @SuppressWarnings("unchecked")
63 PowerMockito.mockStatic(UniUtils.class);
64 PowerMockito.mockStatic(OvsdbUtils.class);
65 PowerMockito.mockStatic(MdsalUtils.class);
66 PowerMockito.mockStatic(UnimgrMapper.class);
67 dataBroker = mock(DataBroker.class);
68 uniNode = mock(Node.class);
69 optUniNode = mock(Optional.class);
70 uni = DataTreeModificationHelper.getUniNode(uniNode);
71 uniAddCommand = new UniAddCommand(dataBroker, uni);
75 * Test method for {@link org.opendaylight.unimgr.command.uniAddCommandTest#execute()}.
78 @SuppressWarnings({ "unchecked", "rawtypes" })
80 public void testExecute() throws Exception {
81 final Optional<Node> optionalOvsdbNode = mock(Optional.class);
82 final UniAugmentation uniAugmentation = mock(UniAugmentation.class);
83 final OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
84 final ConnectionInfo connectionInfo = mock(ConnectionInfo.class);
85 final IpAddress ipAddress = mock(IpAddress.class);
86 final Ipv4Address ipv4Address = mock(Ipv4Address.class);
87 final OvsdbNodeRef ovsNodedRef = mock(OvsdbNodeRef.class);
88 final Node node = mock(Node.class);
89 final NodeId nodeId = mock(NodeId.class);
90 final List<Node> nodes = new ArrayList<Node>();
91 final InstanceIdentifier uniKey = InstanceIdentifier
92 .create(NetworkTopology.class)
93 .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
94 .child(Node.class, new NodeKey(OVSDB_NODE_ID));
97 // Case UNI1 : uni.getOvsdbNodeRef() != null, !optionalNode.isPresent()
98 when(uniNode.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation);
99 when(optUniNode.isPresent()).thenReturn(false);
100 when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
101 any(InstanceIdentifier.class))).thenReturn(optUniNode).thenReturn(optionalOvsdbNode);
102 when(optionalOvsdbNode.isPresent()).thenReturn(false).thenReturn(true);
103 when(optionalOvsdbNode.get()).thenReturn(node);
104 when(uniAugmentation.getIpAddress()).thenReturn(ipAddress);
105 when(uniAugmentation.getSpeed()).thenReturn(mock(Speed.class));
106 when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef);
107 when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
108 when(connectionInfo.getRemoteIp()).thenReturn(ipAddress);
109 when(ipAddress.getIpv4Address()).thenReturn(ipv4Address);
110 when(ipv4Address.toString()).thenReturn("ipv4Address_test");
111 when(ipv4Address.getValue()).thenReturn("ipv4AddressValue_test");
112 when(ovsNodedRef.getValue()).thenReturn(uniKey);
113 when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation);
114 when(node.getNodeId()).thenReturn(nodeId);
115 when(nodeId.toString()).thenReturn("ovsdbNodeId_test");
116 when(OvsdbUtils.findOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
117 .thenReturn(optionalOvsdbNode);
118 when(OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
120 when(UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
121 any(UniAugmentation.class), any(Node.class), any(DataBroker.class))).thenReturn(true);
122 when(UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
123 any(UniAugmentation.class), any(InstanceIdentifier.class), any(DataBroker.class)))
125 when(OvsdbUtils.createOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
127 PowerMockito.doNothing().when(OvsdbUtils.class, "createBridgeNode",
129 uniAugmentation, UnimgrConstants.DEFAULT_BRIDGE_NAME);
130 when(UniUtils.getUniNodes(any(DataBroker.class))).thenReturn(nodes);
131 when(UnimgrMapper.getOvsdbNodeIid(any(NodeId.class))).thenReturn(uniKey);
132 when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class),
133 any(LogicalDatastoreType.class))).thenReturn(uniKey);
134 verifyExecute(1, 0, 1, 0);
136 // Case UNI2 : optionalNode.isPresent()
137 when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
138 any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode);
139 verifyExecute(1, 0, 1, 0);
141 // Case UNI3 : uni.getOvsdbNodeRef() == null, optionalNode.isPresent()
142 when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null);
143 when(optionalOvsdbNode.isPresent()).thenReturn(true);
144 when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
145 any(InstanceIdentifier.class))).thenReturn(optUniNode).thenReturn(optionalOvsdbNode);
146 verifyExecute(2, 1, 3, 0);
148 // Case UNI4 : uni.getOvsdbNodeRef() == null, !optionalNode.isPresent()
149 when(optionalOvsdbNode.isPresent()).thenReturn(false);
150 verifyExecute(2, 1, 4, 0);
153 @SuppressWarnings("unchecked")
154 private void verifyExecute(int qosTimes, int bridgeTimes, int updateNodeTime, int updateIIDTimes){
155 uniAddCommand.execute();
156 PowerMockito.verifyStatic(times(qosTimes));
157 OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class));
158 PowerMockito.verifyStatic(times(bridgeTimes));
159 OvsdbUtils.createBridgeNode(any(DataBroker.class), any(InstanceIdentifier.class),
160 any(UniAugmentation.class), any(String.class));
161 PowerMockito.verifyStatic(times(updateNodeTime));
162 UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
163 any(UniAugmentation.class), any(Node.class), any(DataBroker.class));
164 PowerMockito.verifyStatic(times(updateIIDTimes));
165 UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
166 any(UniAugmentation.class), any(InstanceIdentifier.class), any(DataBroker.class));