L3: Add eth to br-ex
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbBridgeUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.southbound.transactions.md;
10
11 import java.net.InetAddress;
12 import java.net.NetworkInterface;
13 import java.util.ArrayList;
14 import java.util.Enumeration;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
20 import org.apache.commons.lang3.math.NumberUtils;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
24 import org.opendaylight.ovsdb.lib.message.TableUpdates;
25 import org.opendaylight.ovsdb.lib.notation.UUID;
26 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
27 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
28 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
29 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
30 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
31 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
32 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
33 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
61 import org.opendaylight.yangtools.yang.binding.DataObject;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 import com.google.common.base.Optional;
67 import com.google.common.base.Preconditions;
68 import com.google.common.net.InetAddresses;
69
70 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
71     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
72     private Map<UUID,Bridge> updatedBridgeRows;
73     private Map<UUID, Bridge> oldBridgeRows;
74
75     public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
76             DatabaseSchema dbSchema) {
77         super(key,updates,dbSchema);
78         updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
79         oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
80     }
81
82     @Override
83     public void execute(ReadWriteTransaction transaction) {
84         for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
85             updateBridge(transaction, entry.getValue());
86         }
87     }
88
89     private void updateBridge(ReadWriteTransaction transaction,
90             Bridge bridge) {
91         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
92         Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
93         if (connection.isPresent()) {
94             LOG.debug("Connection {} is present",connection);
95
96             // Update the connection node to let it know it manages this bridge
97             Node connectionNode = buildConnectionNode(bridge);
98             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
99
100             // Update the bridge node with whatever data we are getting
101             InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
102             Node bridgeNode = buildBridgeNode(bridge);
103             transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
104             deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
105             deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
106             deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
107         }
108     }
109
110     private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
111             List<InstanceIdentifier<T>> entryIids) {
112         for (InstanceIdentifier<T> entryIid: entryIids) {
113             transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
114         }
115     }
116
117     private List<InstanceIdentifier<BridgeOtherConfigs>> bridgeOtherConfigsToRemove(
118             InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
119         Preconditions.checkNotNull(bridgeIid);
120         Preconditions.checkNotNull(bridge);
121         List<InstanceIdentifier<BridgeOtherConfigs>> result = new ArrayList<>();
122
123         Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
124
125         if (oldBridge != null && oldBridge.getOtherConfigColumn() != null) {
126             for (Entry<String, String> otherConfig:
127                 oldBridge.getOtherConfigColumn().getData().entrySet()) {
128                 if (bridge.getOtherConfigColumn() == null
129                         || !bridge.getOtherConfigColumn().getData().containsKey(otherConfig.getKey())) {
130                     InstanceIdentifier<BridgeOtherConfigs> iid = bridgeIid
131                             .augmentation(OvsdbBridgeAugmentation.class)
132                             .child(BridgeOtherConfigs.class,
133                                     new BridgeOtherConfigsKey(otherConfig.getKey()));
134                     result.add(iid);
135                 }
136             }
137         }
138         return result;
139     }
140
141     private List<InstanceIdentifier<BridgeExternalIds>> externalIdsToRemove(
142             InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
143         Preconditions.checkNotNull(bridgeIid);
144         Preconditions.checkNotNull(bridge);
145         List<InstanceIdentifier<BridgeExternalIds>> result = new ArrayList<>();
146
147         Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
148
149         if (oldBridge != null && oldBridge.getExternalIdsColumn() != null) {
150             for (Entry<String, String> externalId:
151                 oldBridge.getExternalIdsColumn().getData().entrySet()) {
152                 if (bridge.getExternalIdsColumn() == null
153                         || !bridge.getExternalIdsColumn().getData().containsKey(externalId.getKey())) {
154                     InstanceIdentifier<BridgeExternalIds> iid = bridgeIid
155                             .augmentation(OvsdbBridgeAugmentation.class)
156                             .child(BridgeExternalIds.class,
157                                     new BridgeExternalIdsKey(externalId.getKey()));
158                     result.add(iid);
159                 }
160             }
161         }
162         return result;
163     }
164
165     private List<InstanceIdentifier<ProtocolEntry>> protocolEntriesToRemove(
166             InstanceIdentifier<Node> bridgeIid, Bridge bridge) {
167         Preconditions.checkNotNull(bridgeIid);
168         Preconditions.checkNotNull(bridge);
169         List<InstanceIdentifier<ProtocolEntry>> result = new ArrayList<>();
170         Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
171
172         try {
173             if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
174                 for (String protocol : oldBridge.getProtocolsColumn().getData()) {
175                     if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
176                                 .contains(protocol)) {
177                         Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
178                                 .inverse().get(protocol);
179                         InstanceIdentifier<ProtocolEntry> iid = bridgeIid
180                                 .augmentation(OvsdbBridgeAugmentation.class)
181                                 .child(ProtocolEntry.class,
182                                         new ProtocolEntryKey(proto));
183                         result.add(iid);
184                     }
185                 }
186             }
187         } catch (SchemaVersionMismatchException e) {
188             LOG.warn("protocol not supported by this version of ovsdb", e);
189         }
190         return result;
191     }
192
193     private Node buildConnectionNode(
194             Bridge bridge) {
195         //Update node with managed node reference
196         NodeBuilder connectionNode = new NodeBuilder();
197         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
198
199         OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
200         List<ManagedNodeEntry> managedBridges = new ArrayList<>();
201         InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
202                 bridge);
203         ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
204                 new OvsdbBridgeRef(bridgeIid)).build();
205         managedBridges.add(managedBridge);
206         ovsdbConnectionAugmentationBuilder.setManagedNodeEntry(managedBridges);
207
208         connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build());
209
210         LOG.debug("Update node with bridge node ref {}",
211                 ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next());
212         return connectionNode.build();
213     }
214
215     private Node buildBridgeNode(Bridge bridge) {
216         NodeBuilder bridgeNodeBuilder = new NodeBuilder();
217         NodeId bridgeNodeId = getNodeId(bridge);
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());
231
232         LOG.debug("Built with the intent to store bridge data {}",
233                 ovsdbBridgeAugmentationBuilder.build());
234         return bridgeNodeBuilder.build();
235     }
236
237     private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
238         InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
239         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
240     }
241
242     private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
243             Bridge bridge) {
244         ovsdbBridgeAugmentationBuilder.setDatapathType(
245                 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
246     }
247
248     private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
249             Bridge bridge) {
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]));
257         }
258     }
259
260     private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
261             Bridge bridge) {
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<>();
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)
274                             .build());
275                 }
276             }
277             ovsdbBridgeAugmentationBuilder.setBridgeOtherConfigs(otherConfigList);
278         }
279     }
280
281     private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
282             Bridge bridge) {
283         Map<String, String> externalIds = bridge.getExternalIdsColumn()
284                 .getData();
285         if (externalIds != null && !externalIds.isEmpty()) {
286             Set<String> externalIdKeys = externalIds.keySet();
287             List<BridgeExternalIds> externalIdsList = new ArrayList<>();
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)
295                             .build());
296                 }
297             }
298             ovsdbBridgeAugmentationBuilder.setBridgeExternalIds(externalIdsList);
299         }
300     }
301
302     private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
303             Bridge bridge) {
304         if (SouthboundMapper.createMdsalProtocols(bridge) != null
305                 && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
306             ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
307         }
308     }
309
310     private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
311             Bridge bridge) {
312         DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
313         if (dpid != null) {
314             ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
315         }
316     }
317
318     private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
319             Bridge bridge) {
320         Map<UUID, Controller> updatedControllerRows =
321                 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
322         LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
323         for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
324             if (controllerEntry != null
325                 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
326                 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
327                 IpAddress bridgeControllerIpAddress = null;
328                 PortNumber bridgeControllerPortNumber = null;
329                 for (String targetElement : controllerTarget) {
330                     if (InetAddresses.isInetAddress(targetElement)) {
331                         bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
332                         continue;
333                     }
334                     if (NumberUtils.isNumber(targetElement)) {
335                         bridgeControllerPortNumber = new PortNumber(
336                                 Integer.valueOf(String.valueOf(targetElement)));
337                         continue;
338                     }
339                 }
340                 try {
341                     Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
342                 networkInterfacesLoop:
343                     while (networkInterfaces.hasMoreElements()) {
344                         NetworkInterface networkInterface = networkInterfaces.nextElement();
345                         Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
346                         while (networkInterfaceAddresses.hasMoreElements()) {
347                             InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
348                             if (bridgeControllerIpAddress.getIpv4Address().getValue()
349                                     .equals(networkInterfaceAddress.getHostAddress())) {
350                                 ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
351                                         getOvsdbConnectionInstance().getInstanceIdentifier());
352                                 break networkInterfacesLoop;
353                             }
354                         }
355                     }
356                 } catch (Exception e) {
357                     LOG.warn("Error getting local ip address {}", e);
358                 }
359             }
360         }
361     }
362     private InstanceIdentifier<Node> getInstanceIdentifier(Bridge bridge) {
363         return SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
364                 bridge);
365     }
366
367     private NodeId getNodeId(Bridge bridge) {
368         NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class, NodeKey.class);
369         return nodeKey.getNodeId();
370     }
371 }