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.net.SocketException;
19 import java.util.ArrayList;
20 import java.util.Enumeration;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import org.apache.commons.lang3.math.NumberUtils;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.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.OvsdbConnectionInstance;
36 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
37 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
38 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
66 import org.opendaylight.yangtools.yang.binding.DataObject;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
72 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
73 private Map<UUID,Bridge> updatedBridgeRows;
74 private Map<UUID, Bridge> oldBridgeRows;
76 public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
77 DatabaseSchema dbSchema) {
78 super(key,updates,dbSchema);
79 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
80 oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
84 public void execute(ReadWriteTransaction transaction) {
85 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
86 updateBridge(transaction, entry.getValue());
90 private void updateBridge(ReadWriteTransaction transaction,
92 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
93 Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
94 if (connection.isPresent()) {
95 LOG.debug("Connection {} is present",connection);
97 // Update the connection node to let it know it manages this bridge
98 Node connectionNode = buildConnectionNode(bridge);
99 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
101 // Update the bridge node with whatever data we are getting
102 InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
103 Node bridgeNode = buildBridgeNode(bridge);
104 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
105 deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
106 deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
107 deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
111 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
112 List<InstanceIdentifier<T>> entryIids) {
113 for (InstanceIdentifier<T> entryIid: entryIids) {
114 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
118 private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
119 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
120 Preconditions.checkNotNull(bridgeIid);
121 Preconditions.checkNotNull(bridge);
122 List<InstanceIdentifier<BridgeOtherConfigs>> result = new ArrayList<>();
124 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
126 if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
127 for (Entry<String, String> otherConfig:
128 oldBridge.getOtherConfigColumn().getData().entrySet()) {
129 if (bridge.getOtherConfigColumn() == null
130 || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
131 InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
132 .augmentation(OvsdbBridgeAugmentation.class)
133 .child(BridgeOtherConfigs.class,
134 new BridgeOtherConfigsKey(otherConfig.getKey()));
142 private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
143 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
144 Preconditions.checkNotNull(bridgeIid);
145 Preconditions.checkNotNull(bridge);
146 List<InstanceIdentifier<BridgeExternalIds>> result = new ArrayList<>();
148 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
150 if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
151 for (Entry<String, String> externalId:
152 oldBridge.getExternalIdsColumn().getData().entrySet()) {
153 if (bridge.getExternalIdsColumn() == null
154 || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
155 InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
156 .augmentation(OvsdbBridgeAugmentation.class)
157 .child(BridgeExternalIds.class,
158 new BridgeExternalIdsKey(externalId.getKey()));
166 private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
167 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
168 Preconditions.checkNotNull(bridgeIid);
169 Preconditions.checkNotNull(bridge);
170 List<InstanceIdentifier<ProtocolEntry>> result = new ArrayList<>();
171 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
174 if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
175 for (String protocol : oldBridge.getProtocolsColumn().getData()) {
176 if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
177 .contains(protocol)) {
178 Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
179 .inverse().get(protocol);
180 InstanceIdentifier<ProtocolEntry> iid = bridgeIid
181 .augmentation(OvsdbBridgeAugmentation.class)
182 .child(ProtocolEntry.class,
183 new ProtocolEntryKey(proto));
188 } catch (SchemaVersionMismatchException e) {
189 schemaMismatchLog("protocols", "Bridge", e);
194 private Node buildConnectionNode(
196 //Update node with managed node reference
197 NodeBuilder connectionNode = new NodeBuilder();
198 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
200 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
201 List<ManagedNodeEntry> managedBridges = new ArrayList<>();
202 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
204 ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
205 new OvsdbBridgeRef(bridgeIid)).build();
206 managedBridges.add(managedBridge);
207 ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
209 connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
211 LOG.debug("Update node with bridge node ref {}",
212 ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next());
213 return connectionNode.build();
216 private Node buildBridgeNode(Bridge bridge) {
217 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
218 NodeId bridgeNodeId = getNodeId(bridge);
219 bridgeNodeBuilder.setNodeId(bridgeNodeId);
220 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
221 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
222 ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
223 setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
224 setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
225 setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
226 setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
227 setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
228 setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
229 setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
230 setManagedBy(ovsdbBridgeAugmentationBuilder);
231 setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge);
232 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
234 LOG.debug("Built with the intent to store bridge data {}",
235 ovsdbBridgeAugmentationBuilder.build());
236 return bridgeNodeBuilder.build();
239 private void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
242 if (bridge.getAutoAttachColumn() != null
243 && bridge.getAutoAttachColumn().getData() != null
244 && !bridge.getAutoAttachColumn().getData().isEmpty()) {
245 Set<UUID> uuids = bridge.getAutoAttachColumn().getData();
246 for (UUID uuid : uuids) {
247 ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString()));
250 } catch (SchemaVersionMismatchException e) {
251 schemaMismatchLog("auto_attach", "Bridge", e);
255 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
256 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
257 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
260 private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
262 ovsdbBridgeAugmentationBuilder.setDatapathType(
263 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
266 private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
268 if (bridge.getFailModeColumn() != null
269 && bridge.getFailModeColumn().getData() != null
270 && !bridge.getFailModeColumn().getData().isEmpty()) {
271 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
272 bridge.getFailModeColumn().getData().toArray(failmodeArray);
273 ovsdbBridgeAugmentationBuilder.setFailMode(
274 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
278 private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
280 Map<String, String> otherConfigs = bridge
281 .getOtherConfigColumn().getData();
282 if (otherConfigs != null && !otherConfigs.isEmpty()) {
283 Set<String> otherConfigKeys = otherConfigs.keySet();
284 List<BridgeOtherConfigs> otherConfigList = new ArrayList<>();
285 String otherConfigValue;
286 for (String otherConfigKey : otherConfigKeys) {
287 otherConfigValue = otherConfigs.get(otherConfigKey);
288 if (otherConfigKey != null && otherConfigValue != null) {
289 otherConfigList.add(new BridgeOtherConfigsBuilder()
290 .setBridgeOtherConfigKey(otherConfigKey)
291 .setBridgeOtherConfigValue(otherConfigValue)
295 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
299 private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
301 Map<String, String> externalIds = bridge.getExternalIdsColumn()
303 if (externalIds != null && !externalIds.isEmpty()) {
304 Set<String> externalIdKeys = externalIds.keySet();
305 List<BridgeExternalIds> externalIdsList = new ArrayList<>();
306 String externalIdValue;
307 for (String externalIdKey : externalIdKeys) {
308 externalIdValue = externalIds.get(externalIdKey);
309 if (externalIdKey != null && externalIdValue != null) {
310 externalIdsList.add(new BridgeExternalIdsBuilder()
311 .setBridgeExternalIdKey(externalIdKey)
312 .setBridgeExternalIdValue(externalIdValue)
316 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
320 private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
322 if (SouthboundMapper.createMdsalProtocols(bridge) != null
323 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
324 ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
328 private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
330 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
332 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
336 private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
338 Map<UUID, Controller> updatedControllerRows =
339 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
340 LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
341 for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
342 if (controllerEntry != null
343 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
344 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
345 IpAddress bridgeControllerIpAddress = null;
346 PortNumber bridgeControllerPortNumber = null;
347 for (String targetElement : controllerTarget) {
348 if (InetAddresses.isInetAddress(targetElement)) {
349 bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
352 if (NumberUtils.isNumber(targetElement)) {
353 bridgeControllerPortNumber = new PortNumber(
354 Integer.valueOf(String.valueOf(targetElement)));
359 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
360 networkInterfacesLoop:
361 while (networkInterfaces.hasMoreElements()) {
362 NetworkInterface networkInterface = networkInterfaces.nextElement();
363 Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
364 while (networkInterfaceAddresses.hasMoreElements()) {
365 InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
366 if (bridgeControllerIpAddress.getIpv4Address().getValue()
367 .equals(networkInterfaceAddress.getHostAddress())) {
368 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
369 getOvsdbConnectionInstance().getInstanceIdentifier());
370 break networkInterfacesLoop;
374 } catch (SocketException e) {
375 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();