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