1 package org.opendaylight.ovsdb.southbound.transactions.md;
3 import java.util.ArrayList;
6 import java.util.Map.Entry;
9 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
10 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
11 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
12 import org.opendaylight.ovsdb.lib.message.TableUpdates;
13 import org.opendaylight.ovsdb.lib.notation.UUID;
14 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
15 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
16 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
17 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
18 import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
19 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
20 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
45 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
46 private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
47 private Map<UUID,Controller> updatedControllerRows;
48 private Map<UUID,Bridge> updatedBridgeRows;
50 public OvsdbBridgeUpdateCommand(OvsdbClientKey key, TableUpdates updates,
51 DatabaseSchema dbSchema) {
52 super(key,updates,dbSchema);
53 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
54 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
55 getUpdates(), getDbSchema());
59 public void execute(ReadWriteTransaction transaction) {
60 for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
61 updateBridge(transaction, entry.getValue());
65 private void updateBridge(ReadWriteTransaction transaction,
67 final InstanceIdentifier<Node> connectionIId = getKey().toInstanceIndentifier();
68 Optional<Node> connection = readNode(transaction, getKey().toInstanceIndentifier());
69 if (connection.isPresent()) {
70 LOG.debug("Connection {} is present",connection);
72 // Update the connection node to let it know it manages this bridge
73 Node connectionNode = buildConnectionNode(bridge);
74 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
76 // Update the bridge node with whatever data we are getting
77 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
78 Node bridgeNode = buildBridgeNode(bridge);
79 transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
83 private Optional<Node> readNode(ReadWriteTransaction transaction,
84 final InstanceIdentifier<Node> connectionIid) {
85 Optional<Node> node = Optional.absent();
87 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
88 } catch (final ReadFailedException e) {
89 LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e);
94 private Node buildConnectionNode(
96 //Update node with managed node reference
97 NodeBuilder connectionNode = new NodeBuilder();
98 connectionNode.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
100 OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
101 List<ManagedNodeEntry> managedBridges = new ArrayList<ManagedNodeEntry>();
102 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
103 ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
104 new OvsdbBridgeRef(bridgeIid)).build();
105 managedBridges.add(managedBridge);
106 ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
108 connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
110 LOG.debug("Update node with bridge node ref {}",ovsdbConnectionAugmentationBuilder.toString());
111 return connectionNode.build();
114 private Node buildBridgeNode(Bridge bridge) {
115 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
116 InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
117 NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
118 bridgeNodeBuilder.setNodeId(bridgeNodeId);
119 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
120 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
121 ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
122 setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
123 setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
124 setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
125 setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
126 setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
127 setController(ovsdbBridgeAugmentationBuilder, bridge);
128 setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
129 setManagedBy(ovsdbBridgeAugmentationBuilder);
130 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
132 LOG.debug("Built with the intent to store bridge data {}",
133 ovsdbBridgeAugmentationBuilder.toString());
134 return bridgeNodeBuilder.build();
137 private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
138 InstanceIdentifier<Node> connectionNodePath = getKey().toInstanceIndentifier();
139 ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
142 private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
144 ovsdbBridgeAugmentationBuilder.setDatapathType(
145 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
148 private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
150 if (bridge.getFailModeColumn() != null
151 && bridge.getFailModeColumn().getData() != null
152 && !bridge.getFailModeColumn().getData().isEmpty()) {
153 String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
154 bridge.getFailModeColumn().getData().toArray(failmodeArray);
155 ovsdbBridgeAugmentationBuilder.setFailMode(
156 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
160 private void setController(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,Bridge bridge) {
161 if (!SouthboundMapper.createControllerEntries(bridge, updatedControllerRows).isEmpty()) {
162 ovsdbBridgeAugmentationBuilder.setControllerEntry(
163 SouthboundMapper.createControllerEntries(bridge, updatedControllerRows));
167 private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
169 Map<String, String> otherConfigs = bridge
170 .getOtherConfigColumn().getData();
171 if (otherConfigs != null && !otherConfigs.isEmpty()) {
172 Set<String> otherConfigKeys = otherConfigs.keySet();
173 List<BridgeOtherConfigs> otherConfigList = new ArrayList<BridgeOtherConfigs>();
174 String otherConfigValue;
175 for (String otherConfigKey : otherConfigKeys) {
176 otherConfigValue = otherConfigs.get(otherConfigKey);
177 if (otherConfigKey != null && otherConfigValue != null) {
178 otherConfigList.add(new BridgeOtherConfigsBuilder()
179 .setBridgeOtherConfigKey(otherConfigKey)
180 .setBridgeOtherConfigValue(otherConfigValue)
184 ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
188 private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
190 Map<String, String> externalIds = bridge.getExternalIdsColumn()
192 if (externalIds != null && !externalIds.isEmpty()) {
193 Set<String> externalIdKeys = externalIds.keySet();
194 List<BridgeExternalIds> externalIdsList = new ArrayList<BridgeExternalIds>();
195 String externalIdValue;
196 for (String externalIdKey : externalIdKeys) {
197 externalIdValue = externalIds.get(externalIdKey);
198 if (externalIdKey != null && externalIdValue != null) {
199 externalIdsList.add(new BridgeExternalIdsBuilder()
200 .setBridgeExternalIdKey(externalIdKey)
201 .setBridgeExternalIdValue(externalIdValue)
205 ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
209 private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
211 if (SouthboundMapper.createMdsalProtocols(bridge) != null
212 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
213 ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
217 private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
219 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
221 ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);