Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / command / UniCreateCommandTest.java
1 /*
2  * Copyright (c) 2016 CableLabs and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.unimgr.command;
9
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;
14
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.unimgr.impl.UnimgrConstants;
28 import org.opendaylight.unimgr.impl.UnimgrMapper;
29 import org.opendaylight.unimgr.utils.MdsalUtils;
30 import org.opendaylight.unimgr.utils.OvsdbUtils;
31 import org.opendaylight.unimgr.utils.UniUtils;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 import com.google.common.base.Optional;
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({UniUtils.class, OvsdbUtils.class, MdsalUtils.class, UnimgrMapper.class})
55 public class UniCreateCommandTest {
56
57     private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8");
58
59     private UniCreateCommand uniCreateCommand;
60     private Map<InstanceIdentifier<?>, DataObject> changes;
61     private DataBroker dataBroker;
62
63     @SuppressWarnings("unchecked")
64     @Before
65     public void setUp(){
66         PowerMockito.mockStatic(UniUtils.class);
67         PowerMockito.mockStatic(OvsdbUtils.class);
68         PowerMockito.mockStatic(MdsalUtils.class);
69         PowerMockito.mockStatic(UnimgrMapper.class);
70         changes = mock(Map.class);
71         dataBroker = mock(DataBroker.class);
72         uniCreateCommand = new UniCreateCommand(dataBroker, changes);
73     }
74
75     /**
76      * Test method for {@link org.opendaylight.unimgr.command.UniCreateCommand#execute()}.
77      * @throws Exception
78      */
79     @SuppressWarnings({ "unchecked", "rawtypes" })
80     @Test
81     public void testExecute() throws Exception {
82         final Optional<Node> optionalOvsdbNode = mock(Optional.class);
83         final UniAugmentation uniAugmentation = mock(UniAugmentation.class);
84         final OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
85         final ConnectionInfo connectionInfo = mock(ConnectionInfo.class);
86         final IpAddress ipAddress = mock(IpAddress.class);
87         final Ipv4Address ipv4Address = mock(Ipv4Address.class);
88         final OvsdbNodeRef ovsNodedRef = mock(OvsdbNodeRef.class);
89         final Node node = mock(Node.class);
90         final NodeId nodeId = mock(NodeId.class);
91         final List<Node> nodes = new ArrayList<Node>();
92         final InstanceIdentifier uniKey = InstanceIdentifier
93                 .create(NetworkTopology.class)
94                 .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
95                 .child(Node.class, new NodeKey(OVSDB_NODE_ID));
96         final Set<Entry<InstanceIdentifier<?>, DataObject>> setCreated = new HashSet<Map.Entry<InstanceIdentifier<?>,DataObject>>();
97         Entry<InstanceIdentifier<?>, DataObject> created = new Entry<InstanceIdentifier<?>, DataObject>() {
98             @Override
99             public DataObject setValue(DataObject value) { return null; }
100             @Override
101             public DataObject getValue() { return uniAugmentation; }
102             @Override
103             public InstanceIdentifier getKey() { return uniKey; }
104         };
105         nodes.add(node);
106         setCreated.add(created);
107
108         // Case UNI1 : uni.getOvsdbNodeRef() != null, !optionalNode.isPresent()
109         when(changes.entrySet()).thenReturn(setCreated);
110         when(optionalOvsdbNode.isPresent()).thenReturn(false).thenReturn(true);
111         when(optionalOvsdbNode.get()).thenReturn(node);
112         when(uniAugmentation.getIpAddress()).thenReturn(ipAddress);
113         when(uniAugmentation.getSpeed()).thenReturn(mock(Speed.class));
114         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef);
115         when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
116         when(connectionInfo.getRemoteIp()).thenReturn(ipAddress);
117         when(ipAddress.getIpv4Address()).thenReturn(ipv4Address);
118         when(ipv4Address.toString()).thenReturn("ipv4Address_test");
119         when(ipv4Address.getValue()).thenReturn("ipv4AddressValue_test");
120         when(ovsNodedRef.getValue()).thenReturn(uniKey);
121         when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation);
122         when(node.getNodeId()).thenReturn(nodeId);
123         when(nodeId.toString()).thenReturn("ovsdbNodeId_test");
124         when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
125                 any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode);
126         when(OvsdbUtils.findOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
127                 .thenReturn(optionalOvsdbNode);
128         when(OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
129                 .thenReturn(null);
130         when(UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
131                 any(UniAugmentation.class), any(Node.class), any(DataBroker.class))).thenReturn(true);
132         when(UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
133                 any(UniAugmentation.class), any(InstanceIdentifier.class), any(DataBroker.class)))
134                 .thenReturn(true);
135         when(OvsdbUtils.createOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
136                 .thenReturn(node);
137         PowerMockito.doNothing().when(OvsdbUtils.class, "createBridgeNode",
138                 dataBroker, uniKey,
139                 uniAugmentation, UnimgrConstants.DEFAULT_BRIDGE_NAME);
140         when(UniUtils.getUniNodes(any(DataBroker.class))).thenReturn(nodes);
141         when(UnimgrMapper.getOvsdbNodeIid(any(NodeId.class))).thenReturn(uniKey);
142         when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class),
143                 any(LogicalDatastoreType.class))).thenReturn(uniKey);
144         verifyExecute(1, 0, 1, 0);
145
146         // Case UNI2 : optionalNode.isPresent()
147         when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
148                 any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode);
149         verifyExecute(1, 0, 1, 0);
150
151         // Case UNI3 : uni.getOvsdbNodeRef() == null, optionalNode.isPresent()
152         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null);
153         verifyExecute(2, 1, 3, 0);
154
155         // Case UNI4 : uni.getOvsdbNodeRef() == null, !optionalNode.isPresent()
156         when(optionalOvsdbNode.isPresent()).thenReturn(false);
157         verifyExecute(2, 1, 4, 0);
158
159         // Case OVSDB0 ovsdbNodeAugmentation == null
160         created = new Entry<InstanceIdentifier<?>, DataObject>() {
161             @Override
162             public DataObject setValue(DataObject value) { return null; }
163             @Override
164             public DataObject getValue() { return null; }
165             @Override
166             public InstanceIdentifier getKey() { return uniKey; }
167         };
168         setCreated.clear();
169         setCreated.add(created);
170         verifyExecute(2, 1, 4, 0);
171
172         // Case OVSDB1 : uniNodes != null && !uniNodes.isEmpty(),
173         //               uniAugmentation.getOvsdbNodeRef() != null &&
174         //               uniAugmentation.getOvsdbNodeRef().getValue() != null
175         created = new Entry<InstanceIdentifier<?>, DataObject>() {
176             @Override
177             public DataObject setValue(DataObject value) { return null; }
178             @Override
179             public DataObject getValue() { return ovsdbNodeAugmentation; }
180             @Override
181             public InstanceIdentifier getKey() { return uniKey; }
182         };
183         setCreated.clear();
184         setCreated.add(created);
185
186         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef);
187         when(optionalOvsdbNode.isPresent()).thenReturn(true);
188         verifyExecute(3, 2, 4, 1);
189
190         // Case OVSDB2 : uniNodes != null && !uniNodes.isEmpty(),
191         //               ovsdbNodeAugmentation.getConnectionInfo().getRemoteIp()
192         //                      .equals(uniAugmentation.getIpAddress())
193         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null);
194         verifyExecute(3, 3, 4, 2);
195     }
196
197     private void verifyExecute(int qosTimes, int bridgeTimes, int updateNodeTime, int updateIIDTimes){
198         uniCreateCommand.execute();
199         PowerMockito.verifyStatic(times(qosTimes));
200         OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class));
201         PowerMockito.verifyStatic(times(bridgeTimes));
202         OvsdbUtils.createBridgeNode(any(DataBroker.class), any(InstanceIdentifier.class),
203                 any(UniAugmentation.class), any(String.class));
204         PowerMockito.verifyStatic(times(updateNodeTime));
205         UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
206                 any(UniAugmentation.class), any(Node.class), any(DataBroker.class));
207         PowerMockito.verifyStatic(times(updateIIDTimes));
208         UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
209                 any(UniAugmentation.class), any(InstanceIdentifier.class), any(DataBroker.class));
210     }
211
212 }