Merge branch 'topic/master/neutron-yang-migration' to branch 'master'
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SouthboundImpl.java
1 /*
2  * Copyright (c) 2015 Red Hat, 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 package org.opendaylight.ovsdb.openstack.netvirt.impl;
9
10 import java.math.BigInteger;
11 import java.security.InvalidParameterException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.ovsdb.openstack.netvirt.MdsalHelper;
20 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeDpdk;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 import com.google.common.collect.ImmutableBiMap;
65
66 /**
67  * Utility class to wrap mdsal transactions.
68  *
69  * @author Sam Hague (shague@redhat.com)
70  */
71 public class SouthboundImpl implements Southbound {
72     private static final Logger LOG = LoggerFactory.getLogger(SouthboundImpl.class);
73     private DataBroker databroker = null;
74     private static final String PATCH_PORT_TYPE = "patch";
75     private MdsalUtils mdsalUtils = null;
76
77     /**
78      * Class constructor setting the data broker.
79      *
80      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
81      */
82     public SouthboundImpl(DataBroker dataBroker) {
83         this.databroker = dataBroker;
84         mdsalUtils = new MdsalUtils(dataBroker);
85     }
86
87     public DataBroker getDatabroker() {
88         return databroker;
89     }
90
91     public ConnectionInfo getConnectionInfo(Node ovsdbNode) {
92         ConnectionInfo connectionInfo = null;
93         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractOvsdbNode(ovsdbNode);
94         if (ovsdbNodeAugmentation != null) {
95             connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
96         }
97         return connectionInfo;
98     }
99
100     public OvsdbNodeAugmentation extractOvsdbNode(Node node) {
101         return node.getAugmentation(OvsdbNodeAugmentation.class);
102     }
103
104     public NodeId extractBridgeOvsdbNodeId(Node bridgeNode) {
105         NodeId ovsdbNodeId = null;
106         OvsdbBridgeAugmentation bridgeAugmentation = extractBridgeAugmentation(bridgeNode);
107         if (bridgeAugmentation != null) {
108             @SuppressWarnings("unchecked")
109             InstanceIdentifier<Node> ovsdbNodeIid =
110                     (InstanceIdentifier<Node>) (bridgeAugmentation.getManagedBy().getValue());
111             ovsdbNodeId = InstanceIdentifier.keyOf(ovsdbNodeIid).getNodeId();
112         }
113         return ovsdbNodeId;
114     }
115
116     public List<Node> readOvsdbTopologyNodes() {
117         List<Node> ovsdbNodes = new ArrayList<>();
118         InstanceIdentifier<Topology> topologyInstanceIdentifier = MdsalHelper.createInstanceIdentifier();
119         Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, topologyInstanceIdentifier);
120         if (topology != null && topology.getNode() != null) {
121             for (Node node : topology.getNode()) {
122                 OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
123                 if (ovsdbNodeAugmentation != null) {
124                     ovsdbNodes.add(node);
125                 }
126             }
127         }
128         return ovsdbNodes;
129     }
130
131     public Node readOvsdbNode(Node bridgeNode) {
132         Node ovsdbNode = null;
133         OvsdbBridgeAugmentation bridgeAugmentation = extractBridgeAugmentation(bridgeNode);
134         if (bridgeAugmentation != null) {
135             InstanceIdentifier<Node> ovsdbNodeIid =
136                     (InstanceIdentifier<Node>) bridgeAugmentation.getManagedBy().getValue();
137             ovsdbNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
138         }else{
139             LOG.debug("readOvsdbNode: Provided node is not a bridge node : {}",bridgeNode);
140         }
141         return ovsdbNode;
142     }
143
144     public String getOvsdbNodeUUID(Node node) {
145         String nodeUUID = null;
146         OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
147         if (ovsdbNodeAugmentation != null) {
148             // TODO replace with proper uuid and not the system-id
149             nodeUUID = getOsdbNodeExternalIdsValue(ovsdbNodeAugmentation, "system-id");
150         }
151         return nodeUUID;
152     }
153
154     public String getOsdbNodeExternalIdsValue(OvsdbNodeAugmentation ovsdbNodeAugmentation, String key) {
155         String value = null;
156         List<OpenvswitchExternalIds> pairs = ovsdbNodeAugmentation.getOpenvswitchExternalIds();
157         if (pairs != null && !pairs.isEmpty()) {
158             for (OpenvswitchExternalIds pair : pairs) {
159                 if (pair.getExternalIdKey().equals(key)) {
160                     value = pair.getExternalIdValue();
161                     break;
162                 }
163             }
164         }
165         return value;
166     }
167
168     public boolean addBridge(Node ovsdbNode, String bridgeName, List<String> controllersStr) {
169         boolean result = false;
170
171         LOG.info("addBridge: node: {}, bridgeName: {}, controller(s): {}", ovsdbNode, bridgeName, controllersStr);
172         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
173         if (connectionInfo != null) {
174             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
175             InstanceIdentifier<Node> bridgeIid =
176                     MdsalHelper.createInstanceIdentifier(ovsdbNode.getKey(), bridgeName);
177             NodeId bridgeNodeId = MdsalHelper.createManagedNodeId(bridgeIid);
178             bridgeNodeBuilder.setNodeId(bridgeNodeId);
179             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
180             ovsdbBridgeAugmentationBuilder.setControllerEntry(createControllerEntries(controllersStr));
181             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
182             ovsdbBridgeAugmentationBuilder.setProtocolEntry(createMdsalProtocols());
183             ovsdbBridgeAugmentationBuilder.setFailMode(
184                     MdsalHelper.OVSDB_FAIL_MODE_MAP.inverse().get("secure"));
185             setManagedByForBridge(ovsdbBridgeAugmentationBuilder, ovsdbNode.getKey());
186             if (isOvsdbNodeDpdk(ovsdbNode)) {
187                 ovsdbBridgeAugmentationBuilder.setDatapathType(DatapathTypeNetdev.class);
188             }
189             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
190
191             result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
192             LOG.info("addBridge: result: {}", result);
193         } else {
194             throw new InvalidParameterException("Could not find ConnectionInfo");
195         }
196         return result;
197     }
198
199     public boolean deleteBridge(Node ovsdbNode) {
200         boolean result = false;
201         InstanceIdentifier<Node> bridgeIid =
202                 MdsalHelper.createInstanceIdentifier(ovsdbNode.getNodeId());
203
204         result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, bridgeIid);
205         LOG.info("deleteBridge node: {}, bridgeName: {} result : {}", ovsdbNode, ovsdbNode.getNodeId(),result);
206         return result;
207     }
208
209     public OvsdbBridgeAugmentation readBridge(Node node, String name) {
210         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
211         ConnectionInfo connectionInfo = getConnectionInfo(node);
212         if (connectionInfo != null) {
213             InstanceIdentifier<Node> bridgeIid =
214             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
215             Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
216             if (bridgeNode != null) {
217                 ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
218             }
219         }
220         return ovsdbBridgeAugmentation;
221     }
222
223     public Node readBridgeNode(Node node, String name) {
224         Node ovsdbNode = node;
225         if (extractNodeAugmentation(ovsdbNode) == null) {
226             ovsdbNode = readOvsdbNode(node);
227         }
228         Node bridgeNode = null;
229         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
230         if (connectionInfo != null) {
231             InstanceIdentifier<Node> bridgeIid =
232             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
233             bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
234         }
235         return bridgeNode;
236     }
237
238     public Node getBridgeNode(Node node, String bridgeName) {
239         Node bridgeNode = null;
240         OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
241         if (bridge != null && bridge.getBridgeName().getValue().equals(bridgeName)) {
242                 bridgeNode = node;
243         } else {
244             bridgeNode = readBridgeNode(node, bridgeName);
245         }
246
247         return bridgeNode;
248     }
249
250     public String getBridgeUuid(Node node, String name) {
251         String uuid = null;
252         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = readBridge(node, name);
253         if (ovsdbBridgeAugmentation != null) {
254             uuid = ovsdbBridgeAugmentation.getBridgeUuid().getValue();
255         }
256         return uuid;
257     }
258
259     private void setManagedByForBridge(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
260                 NodeKey ovsdbNodeKey) {
261             InstanceIdentifier<Node> connectionNodePath = MdsalHelper.createInstanceIdentifier(ovsdbNodeKey.getNodeId());
262         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
263     }
264
265     private void setControllersForBridge(Node ovsdbNode, String bridgeName, List<String> controllersString) {
266         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
267         if (connectionInfo != null) {
268             for (ControllerEntry controllerEntry : createControllerEntries(controllersString)) {
269                 InstanceIdentifier<ControllerEntry> iid =
270                         MdsalHelper.createInstanceIdentifier(ovsdbNode.getKey(), bridgeName)
271                                 .augmentation(OvsdbBridgeAugmentation.class)
272                                 .child(ControllerEntry.class, controllerEntry.getKey());
273
274                 boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, iid, controllerEntry);
275                 LOG.info("addController: result: {}", result);
276             }
277         }
278     }
279
280     private List<ControllerEntry> createControllerEntries(List<String> controllersStr) {
281         List<ControllerEntry> controllerEntries = new ArrayList<>();
282         if (controllersStr != null) {
283             for (String controllerStr : controllersStr) {
284                 ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
285                 controllerEntryBuilder.setTarget(new Uri(controllerStr));
286                 controllerEntries.add(controllerEntryBuilder.build());
287             }
288         }
289         return controllerEntries;
290     }
291
292     private List<ProtocolEntry> createMdsalProtocols() {
293         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
294         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
295                 MdsalHelper.OVSDB_PROTOCOL_MAP.inverse();
296         protocolList.add(new ProtocolEntryBuilder().
297                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
298         return protocolList;
299     }
300
301     public long getDataPathId(Node node) {
302         long dpid = 0L;
303         String datapathId = getDatapathId(node);
304         if (datapathId != null) {
305             dpid = new BigInteger(datapathId.replaceAll(":", ""), 16).longValue();
306         }
307         return dpid;
308     }
309
310     public String getDatapathId(Node node) {
311         String datapathId = null;
312         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
313         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
314             datapathId = node.getAugmentation(OvsdbBridgeAugmentation.class).getDatapathId().getValue();
315         }
316         return datapathId;
317     }
318
319     public String getDatapathId(OvsdbBridgeAugmentation ovsdbBridgeAugmentation) {
320         String datapathId = null;
321         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
322             datapathId = ovsdbBridgeAugmentation.getDatapathId().getValue();
323         }
324         return datapathId;
325     }
326
327     public OvsdbBridgeAugmentation getBridge(Node node, String name) {
328         OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
329         if ((bridge != null) && (!bridge.getBridgeName().getValue().equals(name))) {
330             bridge = null;
331         }
332         return bridge;
333     }
334
335     public OvsdbBridgeAugmentation getBridge(Node node) {
336         return node.getAugmentation(OvsdbBridgeAugmentation.class);
337     }
338
339     public String getBridgeName(Node node) {
340         String bridgeName = null;
341         OvsdbBridgeAugmentation bridge = getBridge(node);
342         if (bridge != null) {
343             bridgeName = bridge.getBridgeName().getValue();
344         }
345         return bridgeName;
346     }
347
348     public String extractBridgeName(Node node) {
349         return node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue();
350     }
351
352     public OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
353         if (node == null) {
354             return null;
355         }
356         return node.getAugmentation(OvsdbBridgeAugmentation.class);
357     }
358
359     public List<Node> getAllBridgesOnOvsdbNode(Node node) {
360         List<Node> nodes = new ArrayList<Node>();
361         List<ManagedNodeEntry> managedNodes = node.getAugmentation(OvsdbNodeAugmentation.class).getManagedNodeEntry();
362         for (ManagedNodeEntry managedNode : managedNodes) {
363             InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
364             Node bridgeNode = (Node) mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
365             if (bridgeNode != null) {
366                 nodes.add(bridgeNode);
367             }
368         }
369         return nodes;
370     }
371
372     public boolean isBridgeOnOvsdbNode(Node ovsdbNode, String bridgeName) {
373         boolean found = false;
374         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
375         if (ovsdbNodeAugmentation != null) {
376             List<ManagedNodeEntry> managedNodes = ovsdbNodeAugmentation.getManagedNodeEntry();
377             if (managedNodes != null) {
378                 for (ManagedNodeEntry managedNode : managedNodes) {
379                     InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
380                     if (bridgeIid.toString().contains(bridgeName)) {
381                         found = true;
382                         break;
383                     }
384                 }
385             }
386         }
387         return found;
388     }
389
390     public boolean isOvsdbNodeDpdk(Node ovsdbNode) {
391         boolean found = false;
392         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
393         if (ovsdbNodeAugmentation != null) {
394             List<InterfaceTypeEntry> ifTypes = ovsdbNodeAugmentation.getInterfaceTypeEntry();
395             if (ifTypes != null) {
396                 for (InterfaceTypeEntry ifType : ifTypes) {
397                     if (ifType.getInterfaceType().equals(InterfaceTypeDpdk.class)) {
398                         found = true;
399                         break;
400                     }
401                 }
402             }
403         }
404         return found;
405     }
406
407     public OvsdbNodeAugmentation extractNodeAugmentation(Node node) {
408         return node.getAugmentation(OvsdbNodeAugmentation.class);
409     }
410
411     /**
412      * Method read ports from bridge node. Method will check if the provided node
413      * has the ports details, if not, it will read from Operational data store.
414      * @param node Target bridge to getch termination points from.
415      * @return List of termination points on the given bridge
416      */
417     public List<OvsdbTerminationPointAugmentation> getTerminationPointsOfBridge(Node node) {
418         List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(node);
419         if(tpAugmentations.isEmpty()){
420             tpAugmentations = readTerminationPointAugmentations(node);
421         }
422         return tpAugmentations;
423     }
424
425     public OvsdbTerminationPointAugmentation getTerminationPointOfBridge(Node node, String portName) {
426         OvsdbTerminationPointAugmentation tpAugmentation = extractTerminationPointAugmentation(node,portName);
427         if(tpAugmentation == null){
428             List<OvsdbTerminationPointAugmentation> tpAugmentations = readTerminationPointAugmentations(node);
429             if(tpAugmentations != null){
430                 for(OvsdbTerminationPointAugmentation ovsdbTpAugmentation : tpAugmentations){
431                     if(ovsdbTpAugmentation.getName().equals(portName)){
432                         return ovsdbTpAugmentation;
433                     }
434                 }
435             }
436         }
437         return tpAugmentation;
438     }
439
440     public OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
441         if (bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class) != null) {
442             List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(bridgeNode);
443             for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation : tpAugmentations) {
444                 if (ovsdbTerminationPointAugmentation.getName().equals(portName)) {
445                     return ovsdbTerminationPointAugmentation;
446                 }
447             }
448         }
449         return null;
450     }
451
452     public List<TerminationPoint> extractTerminationPoints(Node node) {
453         List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();
454         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
455         if (ovsdbBridgeAugmentation != null) {
456             terminationPoints.addAll(node.getTerminationPoint());
457         }
458         return terminationPoints;
459     }
460
461     public List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
462         List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<OvsdbTerminationPointAugmentation>();
463         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
464         if(terminationPoints != null && !terminationPoints.isEmpty()){
465             for(TerminationPoint tp : terminationPoints){
466                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
467                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
468                 if (ovsdbTerminationPointAugmentation != null) {
469                     tpAugmentations.add(ovsdbTerminationPointAugmentation);
470                 }
471             }
472         }
473         return tpAugmentations;
474     }
475
476     public List<OvsdbTerminationPointAugmentation> readTerminationPointAugmentations(Node node) {
477         InstanceIdentifier<Node> bridgeNodeIid = MdsalHelper.createInstanceIdentifier(node.getNodeId());
478         Node operNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeNodeIid);
479         if(operNode != null){
480             return extractTerminationPointAugmentations(operNode);
481         }
482         return new ArrayList<OvsdbTerminationPointAugmentation>();
483     }
484
485     public String getInterfaceExternalIdsValue(
486             OvsdbTerminationPointAugmentation terminationPointAugmentation, String key) {
487         String value = null;
488         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
489         if (pairs != null && !pairs.isEmpty()) {
490             for (InterfaceExternalIds pair : pairs) {
491                 if (pair.getExternalIdKey().equals(key)) {
492                     value = pair.getExternalIdValue();
493                     break;
494                 }
495             }
496         }
497         return value;
498     }
499
500     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type) {
501         InstanceIdentifier<TerminationPoint> tpIid =
502                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
503         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
504                 new OvsdbTerminationPointAugmentationBuilder();
505
506         tpAugmentationBuilder.setName(portName);
507         if (type != null) {
508             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
509         }
510         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
511         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
512         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
513         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
514     }
515
516     public Boolean deleteTerminationPoint(Node bridgeNode, String portName) {
517         InstanceIdentifier<TerminationPoint> tpIid =
518                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
519         return mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
520     }
521
522     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName,
523             String type, Map<String, String> options) {
524         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
525                 bridgeNode, portName);
526         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder = new OvsdbTerminationPointAugmentationBuilder();
527
528         tpAugmentationBuilder.setName(portName);
529         if (type != null) {
530             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
531         }
532
533         List<Options> optionsList = new ArrayList<Options>();
534         for (Map.Entry<String, String> entry : options.entrySet()) {
535             OptionsBuilder optionsBuilder = new OptionsBuilder();
536             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
537             optionsBuilder.setOption(entry.getKey());
538             optionsBuilder.setValue(entry.getValue());
539             optionsList.add(optionsBuilder.build());
540         }
541         tpAugmentationBuilder.setOptions(optionsList);
542
543         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
544         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
545         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
546         /* TODO SB_MIGRATION should this be merge or mdsalUtils.put */
547         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
548     }
549
550     public TerminationPoint readTerminationPoint(Node bridgeNode, String bridgeName, String portName) {
551         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
552                 bridgeNode, portName);
553         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, tpIid);
554     }
555
556     public Boolean addTunnelTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type,
557                                         Map<String, String> options) {
558         return addTerminationPoint(bridgeNode, bridgeName, portName, type, options);
559     }
560
561     public Boolean isTunnelTerminationPointExist(Node bridgeNode, String bridgeName, String portName){
562         return readTerminationPoint(bridgeNode, bridgeName, portName) != null;
563     }
564
565     public Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
566         Map<String, String> option = new HashMap<String, String>();
567         option.put("peer", peerPortName);
568         return addTerminationPoint(node, bridgeName, portName, PATCH_PORT_TYPE, option);
569     }
570
571     public String getExternalId(Node node, OvsdbTables table, String key) {
572         switch (table) {
573         case BRIDGE:
574             OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
575             if (bridge != null && bridge.getBridgeExternalIds() != null) {
576                 for (BridgeExternalIds bridgeExternaIds :bridge.getBridgeExternalIds()) {
577                     if (bridgeExternaIds.getBridgeExternalIdKey().equals(key)) {
578                         return bridgeExternaIds.getBridgeExternalIdValue();
579                     }
580                 }
581             }
582             break;
583         case CONTROLLER:
584             LOG.warn("getExternalId: There is no external_id for OvsdbTables: ", table);
585             return null;
586         case OPENVSWITCH:
587             OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
588             if (ovsdbNode == null) {
589                 Node nodeFromReadOvsdbNode = readOvsdbNode(node);
590                 ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
591             }
592             if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
593                 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
594                     if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
595                         return openvswitchExternalIds.getExternalIdValue();
596                     }
597                 }
598             }
599             break;
600         case PORT:
601             List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
602             for (OvsdbTerminationPointAugmentation port : ports) {
603                 if (port != null && port.getPortExternalIds() != null) {
604                     for (PortExternalIds portExternalIds :port.getPortExternalIds()) {
605                         if (portExternalIds.getExternalIdKey().equals(key)) {
606                             return portExternalIds.getExternalIdValue();
607                         }
608                     }
609                 }
610             }
611             break;
612         default:
613             LOG.debug("getExternalId: Couldn't find the specified OvsdbTable: ", table);
614             return null;
615         }
616         return null;
617     }
618
619     public String getOtherConfig(Node node, OvsdbTables table, String key) {
620         switch (table) {
621             case BRIDGE:
622                 OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
623                 if (bridge != null && bridge.getBridgeOtherConfigs() != null) {
624                     for (BridgeOtherConfigs bridgeOtherConfigs : bridge.getBridgeOtherConfigs()) {
625                         if (bridgeOtherConfigs.getBridgeOtherConfigKey().equals(key)) {
626                             return bridgeOtherConfigs.getBridgeOtherConfigValue();
627                         }
628                     }
629                 }
630                 break;
631             case CONTROLLER:
632                 LOG.warn("getOtherConfig: There is no other_config for OvsdbTables: ", table);
633                 return null;
634
635             case OPENVSWITCH:
636                 OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
637                 if (ovsdbNode == null) {
638                     Node nodeFromReadOvsdbNode = readOvsdbNode(node);
639                     ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
640                 }
641                 if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
642                     for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
643                         if (openvswitchOtherConfigs.getOtherConfigKey().equals(key)) {
644                             return openvswitchOtherConfigs.getOtherConfigValue();
645                         }
646                     }
647                 }
648                 break;
649             case PORT:
650                 List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
651                 for (OvsdbTerminationPointAugmentation port : ports) {
652                     if (port != null && port.getPortOtherConfigs() != null) {
653                         for (PortOtherConfigs portOtherConfigs : port.getPortOtherConfigs()) {
654                             if (portOtherConfigs.getOtherConfigKey().equals(key)) {
655                                 return portOtherConfigs.getOtherConfigValue();
656                             }
657                         }
658                     }
659                 }
660                 break;
661             default:
662                 LOG.debug("getOtherConfig: Couldn't find the specified OvsdbTable: ", table);
663                 return null;
664         }
665         return null;
666     }
667
668     public boolean addVlanToTp(long vlan) {
669         return false;
670     }
671
672     public boolean isTunnel(OvsdbTerminationPointAugmentation port) {
673         LOG.trace("SouthboundImpl#isTunnel: Interface : {}", port);
674
675         if(port.getInterfaceType() == null){
676             LOG.warn("No type found for the interface : {}", port);
677             return false;
678         }
679         return MdsalHelper.createOvsdbInterfaceType(
680                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_VXLAN)
681                 || MdsalHelper.createOvsdbInterfaceType(
682                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_GRE);
683     }
684
685     public String getOptionsValue(List<Options> options, String key) {
686         String value = null;
687         for (Options option : options) {
688             if (option.getKey().equals(key)) {
689                 value = option.getValue();
690             }
691         }
692         return value;
693     }
694
695     public Topology getOvsdbTopology() {
696         InstanceIdentifier<Topology> path = InstanceIdentifier
697                 .create(NetworkTopology.class)
698                 .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID));
699
700         Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
701         return topology;
702     }
703
704     public Long getOFPort(OvsdbTerminationPointAugmentation port) {
705         Long ofPort = 0L;
706         if (port.getOfport() != null) {
707             ofPort = port.getOfport();
708         }
709         return ofPort;
710     }
711
712     public Long getOFPort(Node bridgeNode, String portName) {
713         Long ofPort = 0L;
714         OvsdbTerminationPointAugmentation port = extractTerminationPointAugmentation(bridgeNode, portName);
715         if (port != null) {
716             ofPort = getOFPort(port);
717         } else {
718             TerminationPoint tp = readTerminationPoint(bridgeNode, null, portName);
719             if (tp != null) {
720                 port = tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
721                 if (port != null) {
722                     ofPort = getOFPort(port);
723                 }
724             }
725         }
726         return ofPort;
727     }
728     public OvsdbBridgeAugmentation getBridgeFromConfig(Node node, String bridge) {
729         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
730         InstanceIdentifier<Node> bridgeIid =
731                 MdsalHelper.createInstanceIdentifier(node.getKey(), bridge);
732         Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, bridgeIid);
733         if (bridgeNode != null) {
734             ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
735         }
736         return ovsdbBridgeAugmentation;
737     }
738 }