2 * Copyright (c) 2015 Cisco Systems, Inc. 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.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.net.InetAddresses;
15 import java.net.InetAddress;
16 import java.net.NetworkInterface;
17 import java.net.SocketException;
18 import java.util.ArrayList;
19 import java.util.Enumeration;
20 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.Optional;
25 import org.apache.commons.lang3.math.NumberUtils;
26 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
29 import org.opendaylight.ovsdb.lib.message.TableUpdates;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
32 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
33 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
34 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
35 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
36 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
37 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
38 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
39 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
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.OvsdbNodeRef;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
67 import org.opendaylight.yangtools.yang.binding.DataObject;
68 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
73 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
74 private final InstanceIdentifierCodec instanceIdentifierCodec;
75 private final Map<UUID,Bridge> updatedBridgeRows;
76 private final Map<UUID, Bridge> oldBridgeRows;
77 private final List<InstanceIdentifier<Node>> updatedBridges = new ArrayList<>();
78 private final Map<NodeId, Node> updatedBridgeNodes;
80 public OvsdbBridgeUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
81 TableUpdates updates, DatabaseSchema dbSchema,
82 Map<NodeId, Node> updatedBridgeNodes) {
83 super(key,updates,dbSchema);
84 this.instanceIdentifierCodec = instanceIdentifierCodec;
85 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
86 oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
87 this.updatedBridgeNodes = updatedBridgeNodes;
91 public void execute(ReadWriteTransaction transaction) {
92 if (updatedBridgeRows == null || updatedBridgeRows.isEmpty()) {
96 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
97 Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
98 if (!connection.isPresent()) {
102 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
103 updateBridge(transaction, entry.getValue(), connectionIId);
108 void updateBridge(ReadWriteTransaction transaction,
109 Bridge bridge, InstanceIdentifier<Node> connectionIId) {
110 // Update the connection node to let it know it manages this bridge
111 Node connectionNode = buildConnectionNode(bridge);
112 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
114 // Update the bridge node with whatever data we are getting
115 InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
116 Node bridgeNode = buildBridgeNode(bridge);
117 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
118 updatedBridges.add(bridgeIid);
119 updatedBridgeNodes.put(getNodeId(bridge), bridgeNode);
120 deleteEntries(transaction, protocolEntriesToRemove(bridgeIid, bridge));
121 deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
122 deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
126 <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
127 List<InstanceIdentifier<T>> entryIids) {
128 for (InstanceIdentifier<T> entryIid : entryIids) {
129 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
133 private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
134 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
135 Preconditions.checkNotNull(bridgeIid);
136 Preconditions.checkNotNull(bridge);
137 List<InstanceIdentifier<BridgeOtherConfigs>> result = new ArrayList<>();
139 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
141 if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
142 for (Entry<String, String> otherConfig:
143 oldBridge.getOtherConfigColumn().getData().entrySet()) {
144 if (bridge.getOtherConfigColumn() == null
145 || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
146 InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
147 .augmentation(OvsdbBridgeAugmentation.class)
148 .child(BridgeOtherConfigs.class,
149 new BridgeOtherConfigsKey(otherConfig.getKey()));
157 private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
158 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
159 Preconditions.checkNotNull(bridgeIid);
160 Preconditions.checkNotNull(bridge);
161 List<InstanceIdentifier<BridgeExternalIds>> result = new ArrayList<>();
163 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
165 if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
166 for (Entry<String, String> externalId:
167 oldBridge.getExternalIdsColumn().getData().entrySet()) {
168 if (bridge.getExternalIdsColumn() == null
169 || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
170 InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
171 .augmentation(OvsdbBridgeAugmentation.class)
172 .child(BridgeExternalIds.class,
173 new BridgeExternalIdsKey(externalId.getKey()));
181 private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
182 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
183 Preconditions.checkNotNull(bridgeIid);
184 Preconditions.checkNotNull(bridge);
185 List<InstanceIdentifier<ProtocolEntry>> result = new ArrayList<>();
186 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
189 if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
190 for (String protocol : oldBridge.getProtocolsColumn().getData()) {
191 if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
192 .contains(protocol)) {
193 Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
194 .inverse().get(protocol);
195 InstanceIdentifier<ProtocolEntry> iid = bridgeIid
196 .augmentation(OvsdbBridgeAugmentation.class)
197 .child(ProtocolEntry.class,
198 new ProtocolEntryKey(proto));
203 } catch (SchemaVersionMismatchException e) {
204 schemaMismatchLog("protocols", "Bridge", e);
209 private Node buildConnectionNode(
211 //Update node with managed node reference
212 NodeBuilder connectionNode = new NodeBuilder();
213 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
215 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
216 List<ManagedNodeEntry> managedBridges = new ArrayList<>();
217 InstanceIdentifier<Node> bridgeIid =
218 SouthboundMapper.createInstanceIdentifier(instanceIdentifierCodec, getOvsdbConnectionInstance(),
220 ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
221 new OvsdbBridgeRef(bridgeIid)).build();
222 managedBridges.add(managedBridge);
223 ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
225 connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
227 LOG.debug("Update node with bridge node ref {}",
228 ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next());
229 return connectionNode.build();
232 private Node buildBridgeNode(Bridge bridge) {
233 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
234 NodeId bridgeNodeId = getNodeId(bridge);
235 bridgeNodeBuilder.setNodeId(bridgeNodeId);
236 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
237 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
238 ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
239 setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
240 setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
241 setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
242 setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
243 setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
244 setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
245 setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
246 setManagedBy(ovsdbBridgeAugmentationBuilder);
247 setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge);
248 setStpEnalbe(ovsdbBridgeAugmentationBuilder,bridge);
249 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
251 LOG.debug("Built with the intent to store bridge data {}",
252 ovsdbBridgeAugmentationBuilder.build());
253 return bridgeNodeBuilder.build();
256 private static void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
258 if (bridge.getAutoAttachColumn() != null
259 && bridge.getAutoAttachColumn().getData() != null
260 && !bridge.getAutoAttachColumn().getData().isEmpty()) {
261 Set<UUID> uuids = bridge.getAutoAttachColumn().getData();
262 for (UUID uuid : uuids) {
263 ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString()));
266 } catch (SchemaVersionMismatchException e) {
267 schemaMismatchLog("auto_attach", "Bridge", e);
271 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
272 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
273 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
276 private static void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
277 ovsdbBridgeAugmentationBuilder.setDatapathType(
278 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
281 private static void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
282 if (bridge.getFailModeColumn() != null
283 && bridge.getFailModeColumn().getData() != null
284 && !bridge.getFailModeColumn().getData().isEmpty()) {
285 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
286 bridge.getFailModeColumn().getData().toArray(failmodeArray);
287 ovsdbBridgeAugmentationBuilder.setFailMode(
288 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
292 private static void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
293 Map<String, String> otherConfigs = bridge
294 .getOtherConfigColumn().getData();
295 if (otherConfigs != null && !otherConfigs.isEmpty()) {
296 List<BridgeOtherConfigs> otherConfigList = new ArrayList<>();
297 for (Entry<String, String> entry : otherConfigs.entrySet()) {
298 String otherConfigKey = entry.getKey();
299 String otherConfigValue = entry.getValue();
300 if (otherConfigKey != null && otherConfigValue != null) {
301 otherConfigList.add(new BridgeOtherConfigsBuilder()
302 .setBridgeOtherConfigKey(otherConfigKey)
303 .setBridgeOtherConfigValue(otherConfigValue)
307 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
311 private static void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
312 Map<String, String> externalIds = bridge.getExternalIdsColumn()
314 if (externalIds != null && !externalIds.isEmpty()) {
315 List<BridgeExternalIds> externalIdsList = new ArrayList<>();
316 for (Entry<String, String> entry : externalIds.entrySet()) {
317 String externalIdKey = entry.getKey();
318 String externalIdValue = entry.getValue();
319 if (externalIdKey != null && externalIdValue != null) {
320 externalIdsList.add(new BridgeExternalIdsBuilder()
321 .setBridgeExternalIdKey(externalIdKey)
322 .setBridgeExternalIdValue(externalIdValue)
326 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
330 private static void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
331 final List<ProtocolEntry> protocols = SouthboundMapper.createMdsalProtocols(bridge);
332 if (!protocols.isEmpty()) {
333 ovsdbBridgeAugmentationBuilder.setProtocolEntry(protocols);
337 private static void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
338 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
340 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
344 private static void setStpEnalbe(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
346 if (bridge.getStpEnableColumn() != null) {
347 Boolean stpEnable = bridge.getStpEnableColumn().getData();
348 if (stpEnable != null) {
349 ovsdbBridgeAugmentationBuilder.setStpEnable(stpEnable);
354 private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
355 Map<UUID, Controller> updatedControllerRows =
356 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
357 LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
358 for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
359 if (controllerEntry != null
360 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
361 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
362 IpAddress bridgeControllerIpAddress = null;
363 for (String targetElement : controllerTarget) {
364 if (InetAddresses.isInetAddress(targetElement)) {
365 bridgeControllerIpAddress = IpAddressBuilder.getDefaultInstance(targetElement);
368 if (NumberUtils.isCreatable(targetElement)) {
373 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
374 networkInterfacesLoop:
375 while (networkInterfaces.hasMoreElements()) {
376 NetworkInterface networkInterface = networkInterfaces.nextElement();
377 Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
378 while (networkInterfaceAddresses.hasMoreElements()) {
379 InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
380 if (bridgeControllerIpAddress.getIpv4Address().getValue()
381 .equals(networkInterfaceAddress.getHostAddress())) {
382 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
383 getOvsdbConnectionInstance().getInstanceIdentifier());
384 break networkInterfacesLoop;
388 } catch (SocketException e) {
389 LOG.warn("Error getting local ip address", e);
395 private InstanceIdentifier<Node> getInstanceIdentifier(Bridge bridge) {
396 return SouthboundMapper.createInstanceIdentifier(instanceIdentifierCodec, getOvsdbConnectionInstance(),
400 private NodeId getNodeId(Bridge bridge) {
401 NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class);
402 return nodeKey.getNodeId();
406 public void onSuccess() {
407 for (InstanceIdentifier<Node> updatedBridge : updatedBridges) {
408 LOG.debug("Updated bridge {} in operational datastore", updatedBridge);
413 public void onFailure(Throwable throwable) {
414 for (InstanceIdentifier<Node> updatedBridge : updatedBridges) {
415 LOG.error("Failed to update bridge {} in operational datastore", updatedBridge);