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
9 package org.opendaylight.ovsdb.southbound.transactions.md;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.ArgumentMatchers.eq;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.ImmutableSet;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.ovsdb.lib.message.TableUpdates;
37 import org.opendaylight.ovsdb.lib.notation.Column;
38 import org.opendaylight.ovsdb.lib.notation.UUID;
39 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
40 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
41 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
42 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
43 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
44 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
45 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
46 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsKey;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.api.support.membermodification.MemberMatcher;
67 import org.powermock.api.support.membermodification.MemberModifier;
68 import org.powermock.reflect.Whitebox;
70 @RunWith(MockitoJUnitRunner.class)
71 public class OpenVSwitchUpdateCommandTest {
73 @Mock private OpenVSwitchUpdateCommand openVSwitchUpdateCommand;
74 @Mock private TableUpdates updates;
75 @Mock private DatabaseSchema dbSchema;
79 openVSwitchUpdateCommand = mock(OpenVSwitchUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
82 @SuppressWarnings("unchecked")
84 // TODO This test needs to be re-done
85 @Ignore("Broken mock-based test")
86 public void testExecute() throws Exception {
87 PowerMockito.mockStatic(TyperUtils.class);
88 UUID uuid = mock(UUID.class);
89 OpenVSwitch ovs = mock(OpenVSwitch.class);
90 when(TyperUtils.extractRowsUpdated(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
91 .thenReturn(ImmutableMap.of(uuid, ovs));
92 OpenVSwitch ovs1 = mock(OpenVSwitch.class);
93 when(TyperUtils.extractRowsOld(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
94 .thenReturn(ImmutableMap.of(uuid, ovs1));
96 //mock getUpdates() and getDbSchema()
97 when(openVSwitchUpdateCommand.getUpdates()).thenReturn(updates);
98 when(openVSwitchUpdateCommand.getDbSchema()).thenReturn(dbSchema);
100 //Test getInstanceIdentifier(): case 1:
101 // ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) == true
102 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
103 when(ovs.getExternalIdsColumn()).thenReturn(column);
104 when(column.getData()).thenReturn(ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY, "iidString"));
105 PowerMockito.mockStatic(SouthboundUtil.class);
106 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getOvsdbConnectionInstance"));
107 InstanceIdentifier<Node> iid = mock(InstanceIdentifier.class);
108 // when((InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(
109 // any(InstanceIdentifierCodec.class), anyString())).thenReturn(iid);
110 OvsdbConnectionInstance ovsdbConnectionInstance = mock(OvsdbConnectionInstance.class);
111 when(ovsdbConnectionInstance.getInstanceIdentifier()).thenReturn(iid);
112 when(openVSwitchUpdateCommand.getOvsdbConnectionInstance()).thenReturn(ovsdbConnectionInstance);
113 doNothing().when(ovsdbConnectionInstance).setInstanceIdentifier(any(InstanceIdentifier.class));
115 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
116 PowerMockito.whenNew(OvsdbNodeAugmentationBuilder.class).withNoArguments().thenReturn(ovsdbNodeBuilder);
118 //suppress the setter methods of the class
119 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOvsVersion",
120 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
121 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDbVersion",
122 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
123 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDataPathTypes",
124 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
125 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setInterfaceTypes",
126 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
127 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setExternalIds",
128 ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class, OpenVSwitch.class));
129 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOtherConfig",
130 InstanceIdentifierCodec.class, ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class,
131 OpenVSwitch.class, OpenVSwitch.class));
132 MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getConnectionInfo"));
133 when(openVSwitchUpdateCommand.getConnectionInfo()).thenReturn(mock(ConnectionInfo.class));
134 when(ovsdbNodeBuilder.setConnectionInfo(any(ConnectionInfo.class))).thenReturn(ovsdbNodeBuilder);
136 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
137 PowerMockito.whenNew(NodeBuilder.class).withNoArguments().thenReturn(nodeBuilder);
139 //suppress getNodeId()
140 MemberModifier.suppress(
141 MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getNodeId", InstanceIdentifierCodec.class,
143 when(nodeBuilder.setNodeId(any(NodeId.class))).thenReturn(nodeBuilder);
144 when(ovsdbNodeBuilder.build()).thenReturn(mock(OvsdbNodeAugmentation.class));
145 when(nodeBuilder.addAugmentation(eq(OvsdbNodeAugmentation.class), any(OvsdbNodeAugmentation.class)))
146 .thenReturn(nodeBuilder);
147 when(nodeBuilder.build()).thenReturn(mock(Node.class));
148 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
149 doNothing().when(transaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
152 openVSwitchUpdateCommand.execute(transaction);
153 verify(openVSwitchUpdateCommand, times(2)).getUpdates();
154 verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
156 //Test getInstanceIdentifier(): case 2: ovs.getExternalIdsColumn() is null
157 when(ovs.getExternalIdsColumn()).thenReturn(null);
158 when(ovs.getUuid()).thenReturn(uuid);
159 openVSwitchUpdateCommand.execute(transaction);
160 verify(openVSwitchUpdateCommand, times(4)).getUpdates();
161 verify(openVSwitchUpdateCommand, times(4)).getDbSchema();
165 @SuppressWarnings("unchecked")
166 public void testSetOtherConfig() throws Exception {
167 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
169 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
170 when(openVSwitch.getOtherConfigColumn()).thenReturn(column);
171 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
173 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
174 when(oldEntry.getOtherConfigColumn()).thenReturn(column);
175 doNothing().when(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
176 any(OpenVSwitch.class));
177 doNothing().when(openVSwitchUpdateCommand).setNewOtherConfigs(any(OvsdbNodeAugmentationBuilder.class),
180 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
181 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
182 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOtherConfig",
183 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
184 verify(openVSwitch, times(2)).getOtherConfigColumn();
185 verify(oldEntry, times(2)).getOtherConfigColumn();
186 verify(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
187 any(OpenVSwitch.class));
191 public void testRemoveOldConfigs() throws Exception {
192 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
193 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
195 //suppress getNodeId()
196 doReturn(null).when(openVSwitchUpdateCommand).getNodeId(any());
197 OpenVSwitch ovs = mock(OpenVSwitch.class);
198 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
199 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
200 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
204 public void testSetNewOtherConfigs() throws Exception {
205 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
206 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
207 ImmutableMap.of("otherConfigKey", "otherConfigValue"));
209 final Map<OpenvswitchOtherConfigsKey, OpenvswitchOtherConfigs> otherConfigsList =
210 ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
211 assertEquals(1, otherConfigsList.size());
212 final OpenvswitchOtherConfigs otherConfig = otherConfigsList.values().iterator().next();
213 assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
214 assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
218 @SuppressWarnings("unchecked")
219 public void testSetExternalIds() throws Exception {
220 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
221 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
223 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
224 when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
225 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
226 when(oldEntry.getExternalIdsColumn()).thenReturn(column);
227 doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
228 any(OpenVSwitch.class));
229 doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
230 any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
232 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
233 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
234 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
235 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
236 verify(openVSwitch, times(2)).getExternalIdsColumn();
237 verify(oldEntry, times(2)).getExternalIdsColumn();
238 verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
239 any(OpenVSwitch.class));
243 public void testRemoveExternalIds() throws Exception {
244 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
245 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
247 //suppress getNodeId()
248 OpenVSwitch ovs = mock(OpenVSwitch.class);
249 doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
250 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
251 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
252 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
256 public void testSetNewExternalIds() throws Exception {
257 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
258 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
259 ImmutableMap.of("externalIdsKey", "externalIdsValue"));
261 final Map<OpenvswitchExternalIdsKey, OpenvswitchExternalIds> externalIdsList =
262 ovsdbNodeBuilder.getOpenvswitchExternalIds();
263 assertEquals(1, externalIdsList.size());
264 final OpenvswitchExternalIds externalId = externalIdsList.values().iterator().next();
265 assertEquals("externalIdsKey", externalId.getExternalIdKey());
266 assertEquals("externalIdsValue", externalId.getExternalIdValue());
270 @SuppressWarnings("unchecked")
271 public void testSetInterfaceTypes() throws Exception {
272 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
274 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
275 when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
276 when(column.getData()).thenReturn(ImmutableSet.of(
280 "dpdkvhostuserclient",
291 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
293 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
294 verify(openVSwitch).getIfaceTypesColumn();
296 Map<InterfaceTypeEntryKey, InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
297 assertEquals(14, interfaceTypeEntries.size());
301 @SuppressWarnings("unchecked")
302 public void testSetDataPathTypes() throws Exception {
303 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
304 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
305 when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
306 when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
308 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
309 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
311 verify(openVSwitch).getDatapathTypesColumn();
312 Map<DatapathTypeEntryKey, DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
313 assertEquals(2, entries.size());
314 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeNetdev.class)));
315 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeSystem.class)));
319 @SuppressWarnings("unchecked")
320 public void testSetOvsVersion() throws Exception {
321 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
322 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
323 when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
324 when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
325 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
326 when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
328 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
329 verify(ovsdbNodeBuilder).setOvsVersion(anyString());
330 verify(openVSwitch).getOvsVersionColumn();
334 @SuppressWarnings("unchecked")
335 public void testSetDbVersion() throws Exception {
336 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
337 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
338 when(openVSwitch.getDbVersionColumn()).thenReturn(column);
339 when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
340 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
342 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
343 verify(ovsdbNodeBuilder).setDbVersion(anyString());
344 verify(openVSwitch).getDbVersionColumn();