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
8 package org.opendaylight.ovsdb.southbound.transactions.md;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.ovsdb.lib.message.TableUpdates;
36 import org.opendaylight.ovsdb.lib.notation.Column;
37 import org.opendaylight.ovsdb.lib.notation.UUID;
38 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
39 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
40 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
41 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
42 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
43 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
44 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
45 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsKey;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.api.support.membermodification.MemberMatcher;
66 import org.powermock.api.support.membermodification.MemberModifier;
67 import org.powermock.reflect.Whitebox;
69 @RunWith(MockitoJUnitRunner.class)
70 public class OpenVSwitchUpdateCommandTest {
72 @Mock private OpenVSwitchUpdateCommand openVSwitchUpdateCommand;
73 @Mock private TableUpdates updates;
74 @Mock private DatabaseSchema dbSchema;
78 openVSwitchUpdateCommand = mock(OpenVSwitchUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
81 @SuppressWarnings("unchecked")
83 // TODO This test needs to be re-done
84 @Ignore("Broken mock-based test")
85 public void testExecute() throws Exception {
86 PowerMockito.mockStatic(TyperUtils.class);
87 UUID uuid = mock(UUID.class);
88 OpenVSwitch ovs = mock(OpenVSwitch.class);
89 when(TyperUtils.extractRowsUpdated(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
90 .thenReturn(ImmutableMap.of(uuid, ovs));
91 OpenVSwitch ovs1 = mock(OpenVSwitch.class);
92 when(TyperUtils.extractRowsOld(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
93 .thenReturn(ImmutableMap.of(uuid, ovs1));
95 //mock getUpdates() and getDbSchema()
96 when(openVSwitchUpdateCommand.getUpdates()).thenReturn(updates);
97 when(openVSwitchUpdateCommand.getDbSchema()).thenReturn(dbSchema);
99 //Test getInstanceIdentifier(): case 1:
100 // ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) == true
101 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
102 when(ovs.getExternalIdsColumn()).thenReturn(column);
103 when(column.getData()).thenReturn(ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY, "iidString"));
104 PowerMockito.mockStatic(SouthboundUtil.class);
105 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getOvsdbConnectionInstance"));
106 InstanceIdentifier<Node> iid = mock(InstanceIdentifier.class);
107 // when((InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(
108 // any(InstanceIdentifierCodec.class), anyString())).thenReturn(iid);
109 OvsdbConnectionInstance ovsdbConnectionInstance = mock(OvsdbConnectionInstance.class);
110 when(ovsdbConnectionInstance.getInstanceIdentifier()).thenReturn(iid);
111 when(openVSwitchUpdateCommand.getOvsdbConnectionInstance()).thenReturn(ovsdbConnectionInstance);
112 doNothing().when(ovsdbConnectionInstance).setInstanceIdentifier(any(InstanceIdentifier.class));
114 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
115 PowerMockito.whenNew(OvsdbNodeAugmentationBuilder.class).withNoArguments().thenReturn(ovsdbNodeBuilder);
117 //suppress the setter methods of the class
118 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOvsVersion",
119 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
120 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDbVersion",
121 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
122 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDataPathTypes",
123 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
124 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setInterfaceTypes",
125 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
126 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setExternalIds",
127 ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class, OpenVSwitch.class));
128 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOtherConfig",
129 InstanceIdentifierCodec.class, ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class,
130 OpenVSwitch.class, OpenVSwitch.class));
131 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getConnectionInfo"));
132 when(openVSwitchUpdateCommand.getConnectionInfo()).thenReturn(mock(ConnectionInfo.class));
133 when(ovsdbNodeBuilder.setConnectionInfo(any(ConnectionInfo.class))).thenReturn(ovsdbNodeBuilder);
135 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
136 PowerMockito.whenNew(NodeBuilder.class).withNoArguments().thenReturn(nodeBuilder);
138 //suppress getNodeId()
139 MemberModifier.suppress(
140 MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getNodeId", InstanceIdentifierCodec.class,
142 when(nodeBuilder.setNodeId(any(NodeId.class))).thenReturn(nodeBuilder);
143 when(ovsdbNodeBuilder.build()).thenReturn(mock(OvsdbNodeAugmentation.class));
144 when(nodeBuilder.addAugmentation(any(OvsdbNodeAugmentation.class))).thenReturn(nodeBuilder);
145 when(nodeBuilder.build()).thenReturn(mock(Node.class));
146 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
147 doNothing().when(transaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
150 openVSwitchUpdateCommand.execute(transaction);
151 verify(openVSwitchUpdateCommand, times(2)).getUpdates();
152 verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
154 //Test getInstanceIdentifier(): case 2: ovs.getExternalIdsColumn() is null
155 when(ovs.getExternalIdsColumn()).thenReturn(null);
156 when(ovs.getUuid()).thenReturn(uuid);
157 openVSwitchUpdateCommand.execute(transaction);
158 verify(openVSwitchUpdateCommand, times(4)).getUpdates();
159 verify(openVSwitchUpdateCommand, times(4)).getDbSchema();
163 @SuppressWarnings("unchecked")
164 public void testSetOtherConfig() throws Exception {
165 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
167 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
168 when(openVSwitch.getOtherConfigColumn()).thenReturn(column);
169 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
171 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
172 when(oldEntry.getOtherConfigColumn()).thenReturn(column);
173 doNothing().when(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
174 any(OpenVSwitch.class));
175 doNothing().when(openVSwitchUpdateCommand).setNewOtherConfigs(any(OvsdbNodeAugmentationBuilder.class),
178 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
179 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
180 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOtherConfig",
181 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
182 verify(openVSwitch, times(2)).getOtherConfigColumn();
183 verify(oldEntry, times(2)).getOtherConfigColumn();
184 verify(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
185 any(OpenVSwitch.class));
189 public void testRemoveOldConfigs() throws Exception {
190 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
191 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
193 doReturn(new NodeId("foo")).when(openVSwitchUpdateCommand).getNodeId(any());
194 OpenVSwitch ovs = mock(OpenVSwitch.class);
195 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
196 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
197 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
201 public void testSetNewOtherConfigs() throws Exception {
202 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
203 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
204 ImmutableMap.of("otherConfigKey", "otherConfigValue"));
206 final Map<OpenvswitchOtherConfigsKey, OpenvswitchOtherConfigs> otherConfigsList =
207 ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
208 assertEquals(1, otherConfigsList.size());
209 final OpenvswitchOtherConfigs otherConfig = otherConfigsList.values().iterator().next();
210 assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
211 assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
215 @SuppressWarnings("unchecked")
216 public void testSetExternalIds() throws Exception {
217 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
218 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
220 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
221 when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
222 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
223 when(oldEntry.getExternalIdsColumn()).thenReturn(column);
224 doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
225 any(OpenVSwitch.class));
226 doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
227 any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
229 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
230 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
231 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
232 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
233 verify(openVSwitch, times(2)).getExternalIdsColumn();
234 verify(oldEntry, times(2)).getExternalIdsColumn();
235 verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
236 any(OpenVSwitch.class));
240 public void testRemoveExternalIds() throws Exception {
241 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
242 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
244 //suppress getNodeId()
245 OpenVSwitch ovs = mock(OpenVSwitch.class);
246 doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
247 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
248 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
249 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
253 public void testSetNewExternalIds() throws Exception {
254 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
255 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
256 ImmutableMap.of("externalIdsKey", "externalIdsValue"));
258 final Map<OpenvswitchExternalIdsKey, OpenvswitchExternalIds> externalIdsList =
259 ovsdbNodeBuilder.getOpenvswitchExternalIds();
260 assertEquals(1, externalIdsList.size());
261 final OpenvswitchExternalIds externalId = externalIdsList.values().iterator().next();
262 assertEquals("externalIdsKey", externalId.getExternalIdKey());
263 assertEquals("externalIdsValue", externalId.getExternalIdValue());
267 @SuppressWarnings("unchecked")
268 public void testSetInterfaceTypes() throws Exception {
269 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
271 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
272 when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
273 when(column.getData()).thenReturn(ImmutableSet.of(
277 "dpdkvhostuserclient",
288 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
290 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
291 verify(openVSwitch).getIfaceTypesColumn();
293 Map<InterfaceTypeEntryKey, InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
294 assertEquals(14, interfaceTypeEntries.size());
298 @SuppressWarnings("unchecked")
299 public void testSetDataPathTypes() throws Exception {
300 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
301 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
302 when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
303 when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
305 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
306 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
308 verify(openVSwitch).getDatapathTypesColumn();
309 Map<DatapathTypeEntryKey, DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
310 assertEquals(2, entries.size());
311 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeNetdev.VALUE)));
312 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeSystem.VALUE)));
316 @SuppressWarnings("unchecked")
317 public void testSetOvsVersion() throws Exception {
318 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
319 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
320 when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
321 when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
322 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
323 when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
325 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
326 verify(ovsdbNodeBuilder).setOvsVersion(anyString());
327 verify(openVSwitch).getOvsVersionColumn();
331 @SuppressWarnings("unchecked")
332 public void testSetDbVersion() throws Exception {
333 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
334 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
335 when(openVSwitch.getDbVersionColumn()).thenReturn(column);
336 when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
337 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
339 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
340 verify(ovsdbNodeBuilder).setDbVersion(anyString());
341 verify(openVSwitch).getDbVersionColumn();