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(any(OvsdbNodeAugmentation.class))).thenReturn(nodeBuilder);
146 when(nodeBuilder.build()).thenReturn(mock(Node.class));
147 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
148 doNothing().when(transaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
151 openVSwitchUpdateCommand.execute(transaction);
152 verify(openVSwitchUpdateCommand, times(2)).getUpdates();
153 verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
155 //Test getInstanceIdentifier(): case 2: ovs.getExternalIdsColumn() is null
156 when(ovs.getExternalIdsColumn()).thenReturn(null);
157 when(ovs.getUuid()).thenReturn(uuid);
158 openVSwitchUpdateCommand.execute(transaction);
159 verify(openVSwitchUpdateCommand, times(4)).getUpdates();
160 verify(openVSwitchUpdateCommand, times(4)).getDbSchema();
164 @SuppressWarnings("unchecked")
165 public void testSetOtherConfig() throws Exception {
166 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
168 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
169 when(openVSwitch.getOtherConfigColumn()).thenReturn(column);
170 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
172 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
173 when(oldEntry.getOtherConfigColumn()).thenReturn(column);
174 doNothing().when(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
175 any(OpenVSwitch.class));
176 doNothing().when(openVSwitchUpdateCommand).setNewOtherConfigs(any(OvsdbNodeAugmentationBuilder.class),
179 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
180 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
181 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOtherConfig",
182 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
183 verify(openVSwitch, times(2)).getOtherConfigColumn();
184 verify(oldEntry, times(2)).getOtherConfigColumn();
185 verify(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
186 any(OpenVSwitch.class));
190 public void testRemoveOldConfigs() throws Exception {
191 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
192 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
194 doReturn(new NodeId("foo")).when(openVSwitchUpdateCommand).getNodeId(any());
195 OpenVSwitch ovs = mock(OpenVSwitch.class);
196 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
197 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
198 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
202 public void testSetNewOtherConfigs() throws Exception {
203 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
204 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
205 ImmutableMap.of("otherConfigKey", "otherConfigValue"));
207 final Map<OpenvswitchOtherConfigsKey, OpenvswitchOtherConfigs> otherConfigsList =
208 ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
209 assertEquals(1, otherConfigsList.size());
210 final OpenvswitchOtherConfigs otherConfig = otherConfigsList.values().iterator().next();
211 assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
212 assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
216 @SuppressWarnings("unchecked")
217 public void testSetExternalIds() throws Exception {
218 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
219 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
221 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
222 when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
223 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
224 when(oldEntry.getExternalIdsColumn()).thenReturn(column);
225 doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
226 any(OpenVSwitch.class));
227 doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
228 any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
230 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
231 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
232 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
233 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
234 verify(openVSwitch, times(2)).getExternalIdsColumn();
235 verify(oldEntry, times(2)).getExternalIdsColumn();
236 verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
237 any(OpenVSwitch.class));
241 public void testRemoveExternalIds() throws Exception {
242 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
243 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
245 //suppress getNodeId()
246 OpenVSwitch ovs = mock(OpenVSwitch.class);
247 doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
248 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
249 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
250 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
254 public void testSetNewExternalIds() throws Exception {
255 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
256 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
257 ImmutableMap.of("externalIdsKey", "externalIdsValue"));
259 final Map<OpenvswitchExternalIdsKey, OpenvswitchExternalIds> externalIdsList =
260 ovsdbNodeBuilder.getOpenvswitchExternalIds();
261 assertEquals(1, externalIdsList.size());
262 final OpenvswitchExternalIds externalId = externalIdsList.values().iterator().next();
263 assertEquals("externalIdsKey", externalId.getExternalIdKey());
264 assertEquals("externalIdsValue", externalId.getExternalIdValue());
268 @SuppressWarnings("unchecked")
269 public void testSetInterfaceTypes() throws Exception {
270 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
272 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
273 when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
274 when(column.getData()).thenReturn(ImmutableSet.of(
278 "dpdkvhostuserclient",
289 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
291 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
292 verify(openVSwitch).getIfaceTypesColumn();
294 Map<InterfaceTypeEntryKey, InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
295 assertEquals(14, interfaceTypeEntries.size());
299 @SuppressWarnings("unchecked")
300 public void testSetDataPathTypes() throws Exception {
301 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
302 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
303 when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
304 when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
306 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
307 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
309 verify(openVSwitch).getDatapathTypesColumn();
310 Map<DatapathTypeEntryKey, DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
311 assertEquals(2, entries.size());
312 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeNetdev.class)));
313 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeSystem.class)));
317 @SuppressWarnings("unchecked")
318 public void testSetOvsVersion() throws Exception {
319 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
320 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
321 when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
322 when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
323 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
324 when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
326 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
327 verify(ovsdbNodeBuilder).setOvsVersion(anyString());
328 verify(openVSwitch).getOvsVersionColumn();
332 @SuppressWarnings("unchecked")
333 public void testSetDbVersion() throws Exception {
334 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
335 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
336 when(openVSwitch.getDbVersionColumn()).thenReturn(column);
337 when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
338 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
340 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
341 verify(ovsdbNodeBuilder).setDbVersion(anyString());
342 verify(openVSwitch).getDbVersionColumn();