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 com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.net.InetAddresses;
16 import java.net.InetAddress;
17 import java.net.NetworkInterface;
18 import java.util.ArrayList;
19 import java.util.Enumeration;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import org.apache.commons.lang3.math.NumberUtils;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
28 import org.opendaylight.ovsdb.lib.message.TableUpdates;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
31 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
32 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
33 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
34 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
35 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
36 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
37 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
65 import org.opendaylight.yangtools.yang.binding.DataObject;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
70 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
71 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
72 private Map<UUID,Bridge> updatedBridgeRows;
73 private Map<UUID, Bridge> oldBridgeRows;
75 public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
76 DatabaseSchema dbSchema) {
77 super(key,updates,dbSchema);
78 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
79 oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
83 public void execute(ReadWriteTransaction transaction) {
84 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
85 updateBridge(transaction, entry.getValue());
89 private void updateBridge(ReadWriteTransaction transaction,
91 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
92 Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
93 if (connection.isPresent()) {
94 LOG.debug("Connection {} is present",connection);
96 // Update the connection node to let it know it manages this bridge
97 Node connectionNode = buildConnectionNode(bridge);
98 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
100 // Update the bridge node with whatever data we are getting
101 InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
102 Node bridgeNode = buildBridgeNode(bridge);
103 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
104 deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
105 deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
106 deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
110 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
111 List<InstanceIdentifier<T>> entryIids) {
112 for (InstanceIdentifier<T> entryIid: entryIids) {
113 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
117 private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
118 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
119 Preconditions.checkNotNull(bridgeIid);
120 Preconditions.checkNotNull(bridge);
121 List<InstanceIdentifier<BridgeOtherConfigs>> result = new ArrayList<>();
123 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
125 if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
126 for (Entry<String, String> otherConfig:
127 oldBridge.getOtherConfigColumn().getData().entrySet()) {
128 if (bridge.getOtherConfigColumn() == null
129 || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
130 InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
131 .augmentation(OvsdbBridgeAugmentation.class)
132 .child(BridgeOtherConfigs.class,
133 new BridgeOtherConfigsKey(otherConfig.getKey()));
141 private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
142 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
143 Preconditions.checkNotNull(bridgeIid);
144 Preconditions.checkNotNull(bridge);
145 List<InstanceIdentifier<BridgeExternalIds>> result = new ArrayList<>();
147 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
149 if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
150 for (Entry<String, String> externalId:
151 oldBridge.getExternalIdsColumn().getData().entrySet()) {
152 if (bridge.getExternalIdsColumn() == null
153 || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
154 InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
155 .augmentation(OvsdbBridgeAugmentation.class)
156 .child(BridgeExternalIds.class,
157 new BridgeExternalIdsKey(externalId.getKey()));
165 private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
166 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
167 Preconditions.checkNotNull(bridgeIid);
168 Preconditions.checkNotNull(bridge);
169 List<InstanceIdentifier<ProtocolEntry>> result = new ArrayList<>();
170 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
173 if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
174 for (String protocol : oldBridge.getProtocolsColumn().getData()) {
175 if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
176 .contains(protocol)) {
177 Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
178 .inverse().get(protocol);
179 InstanceIdentifier<ProtocolEntry> iid = bridgeIid
180 .augmentation(OvsdbBridgeAugmentation.class)
181 .child(ProtocolEntry.class,
182 new ProtocolEntryKey(proto));
187 } catch (SchemaVersionMismatchException e) {
188 schemaMismatchLog("protocols", "Bridge", e);
193 private Node buildConnectionNode(
195 //Update node with managed node reference
196 NodeBuilder connectionNode = new NodeBuilder();
197 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
199 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
200 List<ManagedNodeEntry> managedBridges = new ArrayList<>();
201 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
203 ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
204 new OvsdbBridgeRef(bridgeIid)).build();
205 managedBridges.add(managedBridge);
206 ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
208 connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
210 LOG.debug("Update node with bridge node ref {}",
211 ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next());
212 return connectionNode.build();
215 private Node buildBridgeNode(Bridge bridge) {
216 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
217 NodeId bridgeNodeId = getNodeId(bridge);
218 bridgeNodeBuilder.setNodeId(bridgeNodeId);
219 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
220 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
221 ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
222 setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
223 setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
224 setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
225 setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
226 setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
227 setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
228 setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
229 setManagedBy(ovsdbBridgeAugmentationBuilder);
230 setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge);
231 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
233 LOG.debug("Built with the intent to store bridge data {}",
234 ovsdbBridgeAugmentationBuilder.build());
235 return bridgeNodeBuilder.build();
238 private void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
241 if (bridge.getAutoAttachColumn() != null
242 && bridge.getAutoAttachColumn().getData() != null
243 && !bridge.getAutoAttachColumn().getData().isEmpty()) {
244 Set<UUID> uuids = bridge.getAutoAttachColumn().getData();
245 for (UUID uuid : uuids) {
246 ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString()));
249 } catch (SchemaVersionMismatchException e) {
250 schemaMismatchLog("auto_attach", "Bridge", e);
254 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
255 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
256 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
259 private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
261 ovsdbBridgeAugmentationBuilder.setDatapathType(
262 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
265 private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
267 if (bridge.getFailModeColumn() != null
268 && bridge.getFailModeColumn().getData() != null
269 && !bridge.getFailModeColumn().getData().isEmpty()) {
270 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
271 bridge.getFailModeColumn().getData().toArray(failmodeArray);
272 ovsdbBridgeAugmentationBuilder.setFailMode(
273 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
277 private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
279 Map<String, String> otherConfigs = bridge
280 .getOtherConfigColumn().getData();
281 if (otherConfigs != null && !otherConfigs.isEmpty()) {
282 Set<String> otherConfigKeys = otherConfigs.keySet();
283 List<BridgeOtherConfigs> otherConfigList = new ArrayList<>();
284 String otherConfigValue;
285 for (String otherConfigKey : otherConfigKeys) {
286 otherConfigValue = otherConfigs.get(otherConfigKey);
287 if (otherConfigKey != null && otherConfigValue != null) {
288 otherConfigList.add(new BridgeOtherConfigsBuilder()
289 .setBridgeOtherConfigKey(otherConfigKey)
290 .setBridgeOtherConfigValue(otherConfigValue)
294 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
298 private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
300 Map<String, String> externalIds = bridge.getExternalIdsColumn()
302 if (externalIds != null && !externalIds.isEmpty()) {
303 Set<String> externalIdKeys = externalIds.keySet();
304 List<BridgeExternalIds> externalIdsList = new ArrayList<>();
305 String externalIdValue;
306 for (String externalIdKey : externalIdKeys) {
307 externalIdValue = externalIds.get(externalIdKey);
308 if (externalIdKey != null && externalIdValue != null) {
309 externalIdsList.add(new BridgeExternalIdsBuilder()
310 .setBridgeExternalIdKey(externalIdKey)
311 .setBridgeExternalIdValue(externalIdValue)
315 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
319 private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
321 if (SouthboundMapper.createMdsalProtocols(bridge) != null
322 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
323 ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
327 private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
329 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
331 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
335 private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
337 Map<UUID, Controller> updatedControllerRows =
338 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
339 LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
340 for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
341 if (controllerEntry != null
342 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
343 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
344 IpAddress bridgeControllerIpAddress = null;
345 PortNumber bridgeControllerPortNumber = null;
346 for (String targetElement : controllerTarget) {
347 if (InetAddresses.isInetAddress(targetElement)) {
348 bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
351 if (NumberUtils.isNumber(targetElement)) {
352 bridgeControllerPortNumber = new PortNumber(
353 Integer.valueOf(String.valueOf(targetElement)));
358 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
359 networkInterfacesLoop:
360 while (networkInterfaces.hasMoreElements()) {
361 NetworkInterface networkInterface = networkInterfaces.nextElement();
362 Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
363 while (networkInterfaceAddresses.hasMoreElements()) {
364 InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
365 if (bridgeControllerIpAddress.getIpv4Address().getValue()
366 .equals(networkInterfaceAddress.getHostAddress())) {
367 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
368 getOvsdbConnectionInstance().getInstanceIdentifier());
369 break networkInterfacesLoop;
373 } catch (Exception e) {
374 LOG.warn("Error getting local ip address", e);
380 private InstanceIdentifier<Node> getInstanceIdentifier(Bridge bridge) {
381 return SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
385 private NodeId getNodeId(Bridge bridge) {
386 NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class, NodeKey.class);
387 return nodeKey.getNodeId();