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
9 package org.opendaylight.ovsdb.southbound.transactions.md;
11 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
13 import java.net.InetAddress;
14 import java.net.NetworkInterface;
15 import java.util.ArrayList;
16 import java.util.Enumeration;
17 import java.util.List;
19 import java.util.Map.Entry;
22 import org.apache.commons.lang3.math.NumberUtils;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
26 import org.opendaylight.ovsdb.lib.message.TableUpdates;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
29 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
30 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
31 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
32 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
33 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
34 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
35 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
63 import org.opendaylight.yangtools.yang.binding.DataObject;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.base.Optional;
69 import com.google.common.base.Preconditions;
70 import com.google.common.net.InetAddresses;
72 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
73 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
74 private Map<UUID,Bridge> updatedBridgeRows;
75 private Map<UUID, Bridge> oldBridgeRows;
77 public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
78 DatabaseSchema dbSchema) {
79 super(key,updates,dbSchema);
80 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
81 oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
85 public void execute(ReadWriteTransaction transaction) {
86 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
87 updateBridge(transaction, entry.getValue());
91 private void updateBridge(ReadWriteTransaction transaction,
93 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
94 Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
95 if (connection.isPresent()) {
96 LOG.debug("Connection {} is present",connection);
98 // Update the connection node to let it know it manages this bridge
99 Node connectionNode = buildConnectionNode(bridge);
100 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
102 // Update the bridge node with whatever data we are getting
103 InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
104 Node bridgeNode = buildBridgeNode(bridge);
105 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
106 deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
107 deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
108 deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
112 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
113 List<InstanceIdentifier<T>> entryIids) {
114 for (InstanceIdentifier<T> entryIid: entryIids) {
115 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
119 private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
120 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
121 Preconditions.checkNotNull(bridgeIid);
122 Preconditions.checkNotNull(bridge);
123 List<InstanceIdentifier<BridgeOtherConfigs>> result = new ArrayList<>();
125 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
127 if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
128 for (Entry<String, String> otherConfig:
129 oldBridge.getOtherConfigColumn().getData().entrySet()) {
130 if (bridge.getOtherConfigColumn() == null
131 || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
132 InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
133 .augmentation(OvsdbBridgeAugmentation.class)
134 .child(BridgeOtherConfigs.class,
135 new BridgeOtherConfigsKey(otherConfig.getKey()));
143 private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
144 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
145 Preconditions.checkNotNull(bridgeIid);
146 Preconditions.checkNotNull(bridge);
147 List<InstanceIdentifier<BridgeExternalIds>> result = new ArrayList<>();
149 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
151 if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
152 for (Entry<String, String> externalId:
153 oldBridge.getExternalIdsColumn().getData().entrySet()) {
154 if (bridge.getExternalIdsColumn() == null
155 || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
156 InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
157 .augmentation(OvsdbBridgeAugmentation.class)
158 .child(BridgeExternalIds.class,
159 new BridgeExternalIdsKey(externalId.getKey()));
167 private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
168 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
169 Preconditions.checkNotNull(bridgeIid);
170 Preconditions.checkNotNull(bridge);
171 List<InstanceIdentifier<ProtocolEntry>> result = new ArrayList<>();
172 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
175 if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
176 for (String protocol : oldBridge.getProtocolsColumn().getData()) {
177 if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
178 .contains(protocol)) {
179 Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
180 .inverse().get(protocol);
181 InstanceIdentifier<ProtocolEntry> iid = bridgeIid
182 .augmentation(OvsdbBridgeAugmentation.class)
183 .child(ProtocolEntry.class,
184 new ProtocolEntryKey(proto));
189 } catch (SchemaVersionMismatchException e) {
190 schemaMismatchLog("protocols", "Bridge", e);
195 private Node buildConnectionNode(
197 //Update node with managed node reference
198 NodeBuilder connectionNode = new NodeBuilder();
199 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
201 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
202 List<ManagedNodeEntry> managedBridges = new ArrayList<>();
203 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
205 ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
206 new OvsdbBridgeRef(bridgeIid)).build();
207 managedBridges.add(managedBridge);
208 ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
210 connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
212 LOG.debug("Update node with bridge node ref {}",
213 ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next());
214 return connectionNode.build();
217 private Node buildBridgeNode(Bridge bridge) {
218 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
219 NodeId bridgeNodeId = getNodeId(bridge);
220 bridgeNodeBuilder.setNodeId(bridgeNodeId);
221 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
222 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
223 ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
224 setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
225 setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
226 setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
227 setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
228 setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
229 setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
230 setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
231 setManagedBy(ovsdbBridgeAugmentationBuilder);
232 setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge);
233 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
235 LOG.debug("Built with the intent to store bridge data {}",
236 ovsdbBridgeAugmentationBuilder.build());
237 return bridgeNodeBuilder.build();
240 private void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
243 if (bridge.getAutoAttachColumn() != null
244 && bridge.getAutoAttachColumn().getData() != null
245 && !bridge.getAutoAttachColumn().getData().isEmpty()) {
246 Set<UUID> uuids = bridge.getAutoAttachColumn().getData();
247 for (UUID uuid : uuids) {
248 ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString()));
251 } catch (SchemaVersionMismatchException e) {
252 schemaMismatchLog("auto_attach", "Bridge", e);
256 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
257 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
258 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
261 private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
263 ovsdbBridgeAugmentationBuilder.setDatapathType(
264 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
267 private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
269 if (bridge.getFailModeColumn() != null
270 && bridge.getFailModeColumn().getData() != null
271 && !bridge.getFailModeColumn().getData().isEmpty()) {
272 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
273 bridge.getFailModeColumn().getData().toArray(failmodeArray);
274 ovsdbBridgeAugmentationBuilder.setFailMode(
275 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
279 private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
281 Map<String, String> otherConfigs = bridge
282 .getOtherConfigColumn().getData();
283 if (otherConfigs != null && !otherConfigs.isEmpty()) {
284 Set<String> otherConfigKeys = otherConfigs.keySet();
285 List<BridgeOtherConfigs> otherConfigList = new ArrayList<>();
286 String otherConfigValue;
287 for (String otherConfigKey : otherConfigKeys) {
288 otherConfigValue = otherConfigs.get(otherConfigKey);
289 if (otherConfigKey != null && otherConfigValue != null) {
290 otherConfigList.add(new BridgeOtherConfigsBuilder()
291 .setBridgeOtherConfigKey(otherConfigKey)
292 .setBridgeOtherConfigValue(otherConfigValue)
296 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
300 private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
302 Map<String, String> externalIds = bridge.getExternalIdsColumn()
304 if (externalIds != null && !externalIds.isEmpty()) {
305 Set<String> externalIdKeys = externalIds.keySet();
306 List<BridgeExternalIds> externalIdsList = new ArrayList<>();
307 String externalIdValue;
308 for (String externalIdKey : externalIdKeys) {
309 externalIdValue = externalIds.get(externalIdKey);
310 if (externalIdKey != null && externalIdValue != null) {
311 externalIdsList.add(new BridgeExternalIdsBuilder()
312 .setBridgeExternalIdKey(externalIdKey)
313 .setBridgeExternalIdValue(externalIdValue)
317 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
321 private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
323 if (SouthboundMapper.createMdsalProtocols(bridge) != null
324 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
325 ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
329 private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
331 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
333 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
337 private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
339 Map<UUID, Controller> updatedControllerRows =
340 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
341 LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
342 for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
343 if (controllerEntry != null
344 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
345 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
346 IpAddress bridgeControllerIpAddress = null;
347 PortNumber bridgeControllerPortNumber = null;
348 for (String targetElement : controllerTarget) {
349 if (InetAddresses.isInetAddress(targetElement)) {
350 bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
353 if (NumberUtils.isNumber(targetElement)) {
354 bridgeControllerPortNumber = new PortNumber(
355 Integer.valueOf(String.valueOf(targetElement)));
360 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
361 networkInterfacesLoop:
362 while (networkInterfaces.hasMoreElements()) {
363 NetworkInterface networkInterface = networkInterfaces.nextElement();
364 Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
365 while (networkInterfaceAddresses.hasMoreElements()) {
366 InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
367 if (bridgeControllerIpAddress.getIpv4Address().getValue()
368 .equals(networkInterfaceAddress.getHostAddress())) {
369 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
370 getOvsdbConnectionInstance().getInstanceIdentifier());
371 break networkInterfacesLoop;
375 } catch (Exception e) {
376 LOG.warn("Error getting local ip address", e);
381 private InstanceIdentifier<Node> getInstanceIdentifier(Bridge bridge) {
382 return SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
386 private NodeId getNodeId(Bridge bridge) {
387 NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class, NodeKey.class);
388 return nodeKey.getNodeId();