Fix so that operational store correctly removes controller entries
[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.southbound.OvsdbClientKey;
18 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
19 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.base.Optional;
43
44 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
45     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
46     private Map<UUID,Bridge> updatedBridgeRows;
47
48     public OvsdbBridgeUpdateCommand(OvsdbClientKey key, TableUpdates updates,
49             DatabaseSchema dbSchema) {
50         super(key,updates,dbSchema);
51         updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
52     }
53
54     @Override
55     public void execute(ReadWriteTransaction transaction) {
56         for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
57             updateBridge(transaction, entry.getValue());
58         }
59     }
60
61     private void updateBridge(ReadWriteTransaction transaction,
62             Bridge bridge) {
63         final InstanceIdentifier<Node> connectionIId = getKey().toInstanceIndentifier();
64         Optional<Node> connection = readNode(transaction, getKey().toInstanceIndentifier());
65         if (connection.isPresent()) {
66             LOG.debug("Connection {} is present",connection);
67
68             // Update the connection node to let it know it manages this bridge
69             Node connectionNode = buildConnectionNode(bridge);
70             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
71
72             // Update the bridge node with whatever data we are getting
73             InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
74             Node bridgeNode = buildBridgeNode(bridge);
75             transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
76         }
77     }
78
79     private Optional<Node> readNode(ReadWriteTransaction transaction,
80             final InstanceIdentifier<Node> connectionIid) {
81         Optional<Node> node = Optional.absent();
82         try {
83             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
84         } catch (final ReadFailedException e) {
85             LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e);
86         }
87         return node;
88     }
89
90     private Node buildConnectionNode(
91             Bridge bridge) {
92         //Update node with managed node reference
93         NodeBuilder connectionNode = new NodeBuilder();
94         connectionNode.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
95
96         OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
97         List<ManagedNodeEntry> managedBridges = new ArrayList<ManagedNodeEntry>();
98         InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
99         ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
100                 new OvsdbBridgeRef(bridgeIid)).build();
101         managedBridges.add(managedBridge);
102         ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
103
104         connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
105
106         LOG.debug("Update node with bridge node ref {}",ovsdbConnectionAugmentationBuilder.toString());
107         return connectionNode.build();
108     }
109
110     private Node buildBridgeNode(Bridge bridge) {
111         NodeBuilder bridgeNodeBuilder = new NodeBuilder();
112         InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
113         NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
114         bridgeNodeBuilder.setNodeId(bridgeNodeId);
115         OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
116         ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
117         ovsdbBridgeAugmentationBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
118         setDataPath(ovsdbBridgeAugmentationBuilder, bridge);
119         setDataPathType(ovsdbBridgeAugmentationBuilder, bridge);
120         setProtocol(ovsdbBridgeAugmentationBuilder, bridge);
121         setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
122         setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
123         setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
124         setManagedBy(ovsdbBridgeAugmentationBuilder);
125         bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
126
127         LOG.debug("Built with the intent to store bridge data {}",
128                 ovsdbBridgeAugmentationBuilder.toString());
129         return bridgeNodeBuilder.build();
130     }
131
132     private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
133         InstanceIdentifier<Node> connectionNodePath = getKey().toInstanceIndentifier();
134         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
135     }
136
137     private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
138             Bridge bridge) {
139         ovsdbBridgeAugmentationBuilder.setDatapathType(
140                 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
141     }
142
143     private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
144             Bridge bridge) {
145         if (bridge.getFailModeColumn() != null
146                 && bridge.getFailModeColumn().getData() != null
147                 && !bridge.getFailModeColumn().getData().isEmpty()) {
148             String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
149             bridge.getFailModeColumn().getData().toArray(failmodeArray);
150             ovsdbBridgeAugmentationBuilder.setFailMode(
151                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
152         }
153     }
154
155     private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
156             Bridge bridge) {
157         Map<String, String> otherConfigs = bridge
158                 .getOtherConfigColumn().getData();
159         if (otherConfigs != null && !otherConfigs.isEmpty()) {
160             Set<String> otherConfigKeys = otherConfigs.keySet();
161             List<BridgeOtherConfigs> otherConfigList = new ArrayList<BridgeOtherConfigs>();
162             String otherConfigValue;
163             for (String otherConfigKey : otherConfigKeys) {
164                 otherConfigValue = otherConfigs.get(otherConfigKey);
165                 if (otherConfigKey != null && otherConfigValue != null) {
166                     otherConfigList.add(new BridgeOtherConfigsBuilder()
167                             .setBridgeOtherConfigKey(otherConfigKey)
168                             .setBridgeOtherConfigValue(otherConfigValue)
169                             .build());
170                 }
171             }
172             ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
173         }
174     }
175
176     private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
177             Bridge bridge) {
178         Map<String, String> externalIds = bridge.getExternalIdsColumn()
179                 .getData();
180         if (externalIds != null && !externalIds.isEmpty()) {
181             Set<String> externalIdKeys = externalIds.keySet();
182             List<BridgeExternalIds> externalIdsList = new ArrayList<BridgeExternalIds>();
183             String externalIdValue;
184             for (String externalIdKey : externalIdKeys) {
185                 externalIdValue = externalIds.get(externalIdKey);
186                 if (externalIdKey != null && externalIdValue != null) {
187                     externalIdsList.add(new BridgeExternalIdsBuilder()
188                             .setBridgeExternalIdKey(externalIdKey)
189                             .setBridgeExternalIdValue(externalIdValue)
190                             .build());
191                 }
192             }
193             ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
194         }
195     }
196
197     private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
198             Bridge bridge) {
199         if (SouthboundMapper.createMdsalProtocols(bridge) != null
200                 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
201             ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
202         }
203     }
204
205     private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
206             Bridge bridge) {
207         DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
208         if (dpid != null) {
209             ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
210         }
211     }
212 }