1 package org.opendaylight.ovsdb.southbound.transactions.md;
3 import java.net.InetAddress;
4 import java.net.NetworkInterface;
5 import java.util.ArrayList;
6 import java.util.Enumeration;
9 import java.util.Map.Entry;
12 import org.apache.commons.lang3.math.NumberUtils;
13 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.ovsdb.lib.message.TableUpdates;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
21 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
22 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
23 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
51 import org.opendaylight.yangtools.yang.binding.DataObject;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.common.base.Optional;
57 import com.google.common.base.Preconditions;
58 import com.google.common.net.InetAddresses;
60 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
61 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
62 private Map<UUID,Bridge> updatedBridgeRows;
63 private Map<UUID, Bridge> oldBridgeRows;
65 public OvsdbBridgeUpdateCommand(ConnectionInfo key, TableUpdates updates,
66 DatabaseSchema dbSchema) {
67 super(key,updates,dbSchema);
68 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
69 oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
73 public void execute(ReadWriteTransaction transaction) {
74 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
75 updateBridge(transaction, entry.getValue());
79 private void updateBridge(ReadWriteTransaction transaction,
81 final InstanceIdentifier<Node> connectionIId = SouthboundMapper.createInstanceIdentifier(getConnectionInfo());
82 Optional<Node> connection = readNode(transaction, connectionIId);
83 if (connection.isPresent()) {
84 LOG.debug("Connection {} is present",connection);
86 // Update the connection node to let it know it manages this bridge
87 Node connectionNode = buildConnectionNode(bridge);
88 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
90 // Update the bridge node with whatever data we are getting
91 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
92 Node bridgeNode = buildBridgeNode(bridge);
93 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
94 deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
95 deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
96 deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
100 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
101 List<InstanceIdentifier<T>> entryIids) {
102 for (InstanceIdentifier<T> entryIid: entryIids) {
103 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
107 private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
108 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
109 Preconditions.checkNotNull(bridgeIid);
110 Preconditions.checkNotNull(bridge);
111 List<InstanceIdentifier<BridgeOtherConfigs>> result =
112 new ArrayList<InstanceIdentifier<BridgeOtherConfigs>>();
114 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
116 if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
117 for (Entry<String, String> otherConfig:
118 oldBridge.getOtherConfigColumn().getData().entrySet()) {
119 if (bridge.getOtherConfigColumn() == null
120 || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
121 InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
122 .augmentation(OvsdbBridgeAugmentation.class)
123 .child(BridgeOtherConfigs.class,
124 new BridgeOtherConfigsKey(otherConfig.getKey()));
132 private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
133 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
134 Preconditions.checkNotNull(bridgeIid);
135 Preconditions.checkNotNull(bridge);
136 List<InstanceIdentifier<BridgeExternalIds>> result =
137 new ArrayList<InstanceIdentifier<BridgeExternalIds>>();
139 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
141 if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
142 for (Entry<String, String> externalId:
143 oldBridge.getExternalIdsColumn().getData().entrySet()) {
144 if (bridge.getExternalIdsColumn() == null
145 || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
146 InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
147 .augmentation(OvsdbBridgeAugmentation.class)
148 .child(BridgeExternalIds.class,
149 new BridgeExternalIdsKey(externalId.getKey()));
157 private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
158 InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
159 Preconditions.checkNotNull(bridgeIid);
160 Preconditions.checkNotNull(bridge);
161 List<InstanceIdentifier<ProtocolEntry>> result =
162 new ArrayList<InstanceIdentifier<ProtocolEntry>>();
163 Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
165 if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
166 for (String protocol: oldBridge.getProtocolsColumn().getData()) {
167 if (bridge.getProtocolsColumn() == null
168 || !bridge.getProtocolsColumn().getData().contains(protocol)) {
169 Class<? extends OvsdbBridgeProtocolBase> proto =
170 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse().get(protocol);
171 InstanceIdentifier<ProtocolEntry> iid = bridgeIid
172 .augmentation(OvsdbBridgeAugmentation.class)
173 .child(ProtocolEntry.class,
174 new ProtocolEntryKey(proto));
182 private Optional<Node> readNode(ReadWriteTransaction transaction,
183 final InstanceIdentifier<Node> connectionIid) {
184 Optional<Node> node = Optional.absent();
186 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
187 } catch (final ReadFailedException e) {
188 LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e);
193 private Node buildConnectionNode(
195 //Update node with managed node reference
196 NodeBuilder connectionNode = new NodeBuilder();
197 connectionNode.setNodeId(SouthboundMapper.createNodeId(getConnectionInfo().getRemoteIp(),
198 getConnectionInfo().getRemotePort()));
200 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
201 List<ManagedNodeEntry> managedBridges = new ArrayList<ManagedNodeEntry>();
202 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
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 {}",ovsdbConnectionAugmentationBuilder.toString());
211 return connectionNode.build();
214 private Node buildBridgeNode(Bridge bridge) {
215 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
216 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
217 NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
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 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
232 LOG.debug("Built with the intent to store bridge data {}",
233 ovsdbBridgeAugmentationBuilder.toString());
234 return bridgeNodeBuilder.build();
237 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
238 InstanceIdentifier<Node> connectionNodePath = SouthboundMapper.createInstanceIdentifier(getConnectionInfo());
239 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
242 private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
244 ovsdbBridgeAugmentationBuilder.setDatapathType(
245 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
248 private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
250 if (bridge.getFailModeColumn() != null
251 && bridge.getFailModeColumn().getData() != null
252 && !bridge.getFailModeColumn().getData().isEmpty()) {
253 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
254 bridge.getFailModeColumn().getData().toArray(failmodeArray);
255 ovsdbBridgeAugmentationBuilder.setFailMode(
256 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
260 private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
262 Map<String, String> otherConfigs = bridge
263 .getOtherConfigColumn().getData();
264 if (otherConfigs != null && !otherConfigs.isEmpty()) {
265 Set<String> otherConfigKeys = otherConfigs.keySet();
266 List<BridgeOtherConfigs> otherConfigList = new ArrayList<BridgeOtherConfigs>();
267 String otherConfigValue;
268 for (String otherConfigKey : otherConfigKeys) {
269 otherConfigValue = otherConfigs.get(otherConfigKey);
270 if (otherConfigKey != null && otherConfigValue != null) {
271 otherConfigList.add(new BridgeOtherConfigsBuilder()
272 .setBridgeOtherConfigKey(otherConfigKey)
273 .setBridgeOtherConfigValue(otherConfigValue)
277 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
281 private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
283 Map<String, String> externalIds = bridge.getExternalIdsColumn()
285 if (externalIds != null && !externalIds.isEmpty()) {
286 Set<String> externalIdKeys = externalIds.keySet();
287 List<BridgeExternalIds> externalIdsList = new ArrayList<BridgeExternalIds>();
288 String externalIdValue;
289 for (String externalIdKey : externalIdKeys) {
290 externalIdValue = externalIds.get(externalIdKey);
291 if (externalIdKey != null && externalIdValue != null) {
292 externalIdsList.add(new BridgeExternalIdsBuilder()
293 .setBridgeExternalIdKey(externalIdKey)
294 .setBridgeExternalIdValue(externalIdValue)
298 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
302 private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
304 if (SouthboundMapper.createMdsalProtocols(bridge) != null
305 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
306 ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
310 private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
312 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
314 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
318 private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
320 Map<UUID, Controller> updatedControllerRows =
321 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
322 for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
323 if (controllerEntry != null && controllerEntry.isIsConnected()) {
324 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
325 IpAddress bridgeControllerIpAddress = null;
326 PortNumber bridgeControllerPortNumber = null;
327 for (String targetElement : controllerTarget) {
328 if (InetAddresses.isInetAddress(targetElement)) {
329 bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
332 if (NumberUtils.isNumber(targetElement)) {
333 bridgeControllerPortNumber = new PortNumber(
334 Integer.valueOf(String.valueOf(targetElement)));
339 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
340 networkInterfacesLoop:
341 while (networkInterfaces.hasMoreElements()) {
342 NetworkInterface networkInterface = networkInterfaces.nextElement();
343 Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
344 while (networkInterfaceAddresses.hasMoreElements()) {
345 InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
346 if (bridgeControllerIpAddress.getIpv4Address().getValue()
347 .equals(networkInterfaceAddress.getHostAddress())) {
348 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
349 SouthboundMapper.createInstanceIdentifier(bridgeControllerIpAddress,
350 bridgeControllerPortNumber));
351 break networkInterfacesLoop;
355 } catch (Exception e) {
356 LOG.debug("Error getting local ip address {}", e);