Merge "Cleaned up OvsdbBridgeUpdateCommand"
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbBridgeUpdateCommand.java
1 package org.opendaylight.ovsdb.southbound.transactions.md;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
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;
42
43 import com.google.common.base.Optional;
44
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;
49
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());
56     }
57
58     @Override
59     public void execute(ReadWriteTransaction transaction) {
60         for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
61             updateBridge(transaction, entry.getValue());
62         }
63     }
64
65     private void updateBridge(ReadWriteTransaction transaction,
66             Bridge bridge) {
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);
71
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);
75
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);
80         }
81     }
82
83     private Optional<Node> readNode(ReadWriteTransaction transaction,
84             final InstanceIdentifier<Node> connectionIid) {
85         Optional<Node> node = Optional.absent();
86         try {
87             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
88         } catch (final ReadFailedException e) {
89             LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e);
90         }
91         return node;
92     }
93
94     private Node buildConnectionNode(
95             Bridge bridge) {
96         //Update node with managed node reference
97         NodeBuilder connectionNode = new NodeBuilder();
98         connectionNode.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
99
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);
107
108         connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
109
110         LOG.debug("Update node with bridge node ref {}",ovsdbConnectionAugmentationBuilder.toString());
111         return connectionNode.build();
112     }
113
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());
131
132         LOG.debug("Built with the intent to store bridge data {}",
133                 ovsdbBridgeAugmentationBuilder.toString());
134         return bridgeNodeBuilder.build();
135     }
136
137     private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
138         InstanceIdentifier<Node> connectionNodePath = getKey().toInstanceIndentifier();
139         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
140     }
141
142     private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
143             Bridge bridge) {
144         ovsdbBridgeAugmentationBuilder.setDatapathType(
145                 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
146     }
147
148     private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
149             Bridge bridge) {
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]));
157         }
158     }
159
160     private void setController(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,Bridge bridge) {
161         if (!SouthboundMapper.createControllerEntries(bridge, updatedControllerRows).isEmpty()) {
162             ovsdbBridgeAugmentationBuilder.setControllerEntry(
163                     SouthboundMapper.createControllerEntries(bridge, updatedControllerRows));
164         }
165     }
166
167     private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
168             Bridge bridge) {
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)
181                             .build());
182                 }
183             }
184             ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
185         }
186     }
187
188     private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
189             Bridge bridge) {
190         Map<String, String> externalIds = bridge.getExternalIdsColumn()
191                 .getData();
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)
202                             .build());
203                 }
204             }
205             ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
206         }
207     }
208
209     private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
210             Bridge bridge) {
211         if (SouthboundMapper.createMdsalProtocols(bridge) != null
212                 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
213             ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
214         }
215     }
216
217     private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
218             Bridge bridge) {
219         DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
220         if (dpid != null) {
221             ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
222         }
223     }
224 }