fb7f8e1224e599a0b2ceca4ab9aa35763812a7df
[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.ovsdb.utils.mdsal.utils.MdsalUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
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 final DataBroker databroker;
67     private static final String PATCH_PORT_TYPE = "patch";
68     private final MdsalUtils mdsalUtils;
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;
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         InstanceIdentifier<Node> bridgeIid =
220                 MdsalHelper.createInstanceIdentifier(ovsdbNode.getNodeId());
221
222         boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, bridgeIid);
223         LOG.info("deleteBridge node: {}, bridgeName: {} result : {}", ovsdbNode, ovsdbNode.getNodeId(),result);
224         return result;
225     }
226
227     public OvsdbBridgeAugmentation readBridge(Node node, String name) {
228         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
229         ConnectionInfo connectionInfo = getConnectionInfo(node);
230         if (connectionInfo != null) {
231             InstanceIdentifier<Node> bridgeIid =
232             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
233             Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
234             if (bridgeNode != null) {
235                 ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
236             }
237         }
238         return ovsdbBridgeAugmentation;
239     }
240
241     public Node readBridgeNode(Node node, String name) {
242         Node ovsdbNode = node;
243         if (extractNodeAugmentation(ovsdbNode) == null) {
244             ovsdbNode = readOvsdbNode(node);
245             if (ovsdbNode == null) {
246                 return null;
247             }
248         }
249         Node bridgeNode = null;
250         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
251         if (connectionInfo != null) {
252             InstanceIdentifier<Node> bridgeIid =
253             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
254             bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
255         }
256         return bridgeNode;
257     }
258
259     public Node getBridgeNode(Node node, String bridgeName) {
260         OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
261         if (bridge != null && bridge.getBridgeName().getValue().equals(bridgeName)) {
262             return node;
263         } else {
264             return readBridgeNode(node, bridgeName);
265         }
266     }
267
268     public String getBridgeUuid(Node node, String name) {
269         String uuid = null;
270         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = readBridge(node, name);
271         if (ovsdbBridgeAugmentation != null) {
272             uuid = ovsdbBridgeAugmentation.getBridgeUuid().getValue();
273         }
274         return uuid;
275     }
276
277     private void setManagedByForBridge(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
278                 NodeKey ovsdbNodeKey) {
279             InstanceIdentifier<Node> connectionNodePath = MdsalHelper.createInstanceIdentifier(ovsdbNodeKey.getNodeId());
280         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
281     }
282
283     private List<ControllerEntry> createControllerEntries(List<String> controllersStr) {
284         List<ControllerEntry> controllerEntries = new ArrayList<>();
285         if (controllersStr != null) {
286             for (String controllerStr : controllersStr) {
287                 ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
288                 controllerEntryBuilder.setTarget(new Uri(controllerStr));
289                 controllerEntries.add(controllerEntryBuilder.build());
290             }
291         }
292         return controllerEntries;
293     }
294
295     private List<ProtocolEntry> createMdsalProtocols() {
296         List<ProtocolEntry> protocolList = new ArrayList<>();
297         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
298                 MdsalHelper.OVSDB_PROTOCOL_MAP.inverse();
299         protocolList.add(new ProtocolEntryBuilder().
300                 setProtocol(mapper.get("OpenFlow13")).build());
301         return protocolList;
302     }
303
304     public long getDataPathId(Node node) {
305         long dpid = 0L;
306         String datapathId = getDatapathId(node);
307         if (datapathId != null) {
308             dpid = new BigInteger(datapathId.replaceAll(":", ""), 16).longValue();
309         }
310         return dpid;
311     }
312
313     public String getDatapathId(Node node) {
314         String datapathId = null;
315         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
316         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
317             datapathId = node.getAugmentation(OvsdbBridgeAugmentation.class).getDatapathId().getValue();
318         }
319         return datapathId;
320     }
321
322     public String getDatapathId(OvsdbBridgeAugmentation ovsdbBridgeAugmentation) {
323         String datapathId = null;
324         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
325             datapathId = ovsdbBridgeAugmentation.getDatapathId().getValue();
326         }
327         return datapathId;
328     }
329
330     public OvsdbBridgeAugmentation getBridge(Node node, String name) {
331         OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
332         if ((bridge != null) && (!bridge.getBridgeName().getValue().equals(name))) {
333             bridge = null;
334         }
335         return bridge;
336     }
337
338     public OvsdbBridgeAugmentation getBridge(Node node) {
339         return node.getAugmentation(OvsdbBridgeAugmentation.class);
340     }
341
342     public String getBridgeName(Node node) {
343         String bridgeName = null;
344         OvsdbBridgeAugmentation bridge = getBridge(node);
345         if (bridge != null) {
346             bridgeName = bridge.getBridgeName().getValue();
347         }
348         return bridgeName;
349     }
350
351     public String extractBridgeName(Node node) {
352         return node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue();
353     }
354
355     public OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
356         if (node == null) {
357             return null;
358         }
359         return node.getAugmentation(OvsdbBridgeAugmentation.class);
360     }
361
362     public List<Node> getAllBridgesOnOvsdbNode(Node node) {
363         List<Node> nodes = new ArrayList<>();
364         List<ManagedNodeEntry> managedNodes = node.getAugmentation(OvsdbNodeAugmentation.class).getManagedNodeEntry();
365         for (ManagedNodeEntry managedNode : managedNodes) {
366             InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
367             Node bridgeNode = (Node) mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
368             if (bridgeNode != null) {
369                 nodes.add(bridgeNode);
370             }
371         }
372         return nodes;
373     }
374
375     public boolean isBridgeOnOvsdbNode(Node ovsdbNode, String bridgeName) {
376         boolean found = false;
377         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
378         if (ovsdbNodeAugmentation != null) {
379             List<ManagedNodeEntry> managedNodes = ovsdbNodeAugmentation.getManagedNodeEntry();
380             if (managedNodes != null) {
381                 for (ManagedNodeEntry managedNode : managedNodes) {
382                     InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
383                     if (bridgeIid.toString().contains(bridgeName)) {
384                         found = true;
385                         break;
386                     }
387                 }
388             }
389         }
390         return found;
391     }
392
393     public boolean isOvsdbNodeDpdk(Node ovsdbNode) {
394         boolean found = false;
395         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
396         if (ovsdbNodeAugmentation != null) {
397             List<InterfaceTypeEntry> ifTypes = ovsdbNodeAugmentation.getInterfaceTypeEntry();
398             if (ifTypes != null) {
399                 for (InterfaceTypeEntry ifType : ifTypes) {
400                     if (ifType.getInterfaceType().equals(InterfaceTypeDpdk.class)) {
401                         found = true;
402                         break;
403                     }
404                 }
405             }
406         }
407         return found;
408     }
409
410     public OvsdbNodeAugmentation extractNodeAugmentation(Node node) {
411         return node.getAugmentation(OvsdbNodeAugmentation.class);
412     }
413
414     /**
415      * Method read ports from bridge node. Method will check if the provided node
416      * has the ports details, if not, it will read from Operational data store.
417      * @param node Target bridge to getch termination points from.
418      * @return List of termination points on the given bridge
419      */
420     public List<OvsdbTerminationPointAugmentation> getTerminationPointsOfBridge(Node node) {
421         List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(node);
422         if(tpAugmentations.isEmpty()){
423             tpAugmentations = readTerminationPointAugmentations(node);
424         }
425         return tpAugmentations;
426     }
427
428     public OvsdbTerminationPointAugmentation getTerminationPointOfBridge(Node node, String portName) {
429         OvsdbTerminationPointAugmentation tpAugmentation = extractTerminationPointAugmentation(node,portName);
430         if(tpAugmentation == null){
431             List<OvsdbTerminationPointAugmentation> tpAugmentations = readTerminationPointAugmentations(node);
432             if(tpAugmentations != null){
433                 for(OvsdbTerminationPointAugmentation ovsdbTpAugmentation : tpAugmentations){
434                     if(ovsdbTpAugmentation.getName().equals(portName)){
435                         return ovsdbTpAugmentation;
436                     }
437                 }
438             }
439         }
440         return tpAugmentation;
441     }
442
443     public OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
444         if (bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class) != null) {
445             List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(bridgeNode);
446             for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation : tpAugmentations) {
447                 if (ovsdbTerminationPointAugmentation.getName().equals(portName)) {
448                     return ovsdbTerminationPointAugmentation;
449                 }
450             }
451         }
452         return null;
453     }
454
455     public List<TerminationPoint> extractTerminationPoints(Node node) {
456         List<TerminationPoint> terminationPoints = new ArrayList<>();
457         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
458         if (ovsdbBridgeAugmentation != null) {
459             terminationPoints.addAll(node.getTerminationPoint());
460         }
461         return terminationPoints;
462     }
463
464     public List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
465         List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<>();
466         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
467         if(terminationPoints != null && !terminationPoints.isEmpty()){
468             for(TerminationPoint tp : terminationPoints){
469                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
470                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
471                 if (ovsdbTerminationPointAugmentation != null) {
472                     tpAugmentations.add(ovsdbTerminationPointAugmentation);
473                 }
474             }
475         }
476         return tpAugmentations;
477     }
478
479     public List<OvsdbTerminationPointAugmentation> readTerminationPointAugmentations(Node node) {
480         InstanceIdentifier<Node> bridgeNodeIid = MdsalHelper.createInstanceIdentifier(node.getNodeId());
481         Node operNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeNodeIid);
482         if(operNode != null){
483             return extractTerminationPointAugmentations(operNode);
484         }
485         return new ArrayList<>();
486     }
487
488     public String getInterfaceExternalIdsValue(
489             OvsdbTerminationPointAugmentation terminationPointAugmentation, String key) {
490         String value = null;
491         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
492         if (pairs != null && !pairs.isEmpty()) {
493             for (InterfaceExternalIds pair : pairs) {
494                 if (pair.getExternalIdKey().equals(key)) {
495                     value = pair.getExternalIdValue();
496                     break;
497                 }
498             }
499         }
500         return value;
501     }
502
503     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type) {
504         InstanceIdentifier<TerminationPoint> tpIid =
505                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
506         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
507                 new OvsdbTerminationPointAugmentationBuilder();
508
509         tpAugmentationBuilder.setName(portName);
510         if (type != null) {
511             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
512         }
513         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
514         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
515         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
516         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
517     }
518
519     public Boolean deleteTerminationPoint(Node bridgeNode, String portName) {
520         InstanceIdentifier<TerminationPoint> tpIid =
521                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
522         return mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
523     }
524
525     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName,
526             String type, Map<String, String> options) {
527         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
528                 bridgeNode, portName);
529         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder = new OvsdbTerminationPointAugmentationBuilder();
530
531         tpAugmentationBuilder.setName(portName);
532         if (type != null) {
533             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
534         }
535
536         List<Options> optionsList = new ArrayList<>();
537         for (Map.Entry<String, String> entry : options.entrySet()) {
538             OptionsBuilder optionsBuilder = new OptionsBuilder();
539             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
540             optionsBuilder.setOption(entry.getKey());
541             optionsBuilder.setValue(entry.getValue());
542             optionsList.add(optionsBuilder.build());
543         }
544         tpAugmentationBuilder.setOptions(optionsList);
545
546         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
547         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
548         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
549         /* TODO SB_MIGRATION should this be merge or mdsalUtils.put */
550         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
551     }
552
553     public TerminationPoint readTerminationPoint(Node bridgeNode, String bridgeName, String portName) {
554         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
555                 bridgeNode, portName);
556         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, tpIid);
557     }
558
559     public Boolean addTunnelTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type,
560                                         Map<String, String> options) {
561         return addTerminationPoint(bridgeNode, bridgeName, portName, type, options);
562     }
563
564     public Boolean isTunnelTerminationPointExist(Node bridgeNode, String bridgeName, String portName){
565         return readTerminationPoint(bridgeNode, bridgeName, portName) != null;
566     }
567
568     public Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
569         Map<String, String> option = new HashMap<>();
570         option.put("peer", peerPortName);
571         return addTerminationPoint(node, bridgeName, portName, PATCH_PORT_TYPE, option);
572     }
573
574     public String getExternalId(Node node, OvsdbTables table, String key) {
575         switch (table) {
576         case BRIDGE:
577             OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
578             if (bridge != null && bridge.getBridgeExternalIds() != null) {
579                 for (BridgeExternalIds bridgeExternaIds :bridge.getBridgeExternalIds()) {
580                     if (bridgeExternaIds.getBridgeExternalIdKey().equals(key)) {
581                         return bridgeExternaIds.getBridgeExternalIdValue();
582                     }
583                 }
584             }
585             break;
586         case CONTROLLER:
587             LOG.warn("getExternalId: There is no external_id for OvsdbTables: ", table);
588             return null;
589         case OPENVSWITCH:
590             OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
591             if (ovsdbNode == null) {
592                 Node nodeFromReadOvsdbNode = readOvsdbNode(node);
593                 if (nodeFromReadOvsdbNode != null) {
594                     ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
595                 }
596             }
597             if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
598                 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
599                     if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
600                         return openvswitchExternalIds.getExternalIdValue();
601                     }
602                 }
603             }
604             break;
605         case PORT:
606             List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
607             for (OvsdbTerminationPointAugmentation port : ports) {
608                 if (port != null && port.getPortExternalIds() != null) {
609                     for (PortExternalIds portExternalIds :port.getPortExternalIds()) {
610                         if (portExternalIds.getExternalIdKey().equals(key)) {
611                             return portExternalIds.getExternalIdValue();
612                         }
613                     }
614                 }
615             }
616             break;
617         default:
618             LOG.debug("getExternalId: Couldn't find the specified OvsdbTable: ", table);
619             return null;
620         }
621         return null;
622     }
623
624     public String getOtherConfig(Node node, OvsdbTables table, String key) {
625         switch (table) {
626             case BRIDGE:
627                 OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
628                 if (bridge != null && bridge.getBridgeOtherConfigs() != null) {
629                     for (BridgeOtherConfigs bridgeOtherConfigs : bridge.getBridgeOtherConfigs()) {
630                         if (bridgeOtherConfigs.getBridgeOtherConfigKey().equals(key)) {
631                             return bridgeOtherConfigs.getBridgeOtherConfigValue();
632                         }
633                     }
634                 }
635                 break;
636             case CONTROLLER:
637                 LOG.warn("getOtherConfig: There is no other_config for OvsdbTables: ", table);
638                 return null;
639
640             case OPENVSWITCH:
641                 OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
642                 if (ovsdbNode == null) {
643                     Node nodeFromReadOvsdbNode = readOvsdbNode(node);
644                     if (nodeFromReadOvsdbNode != null) {
645                         ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
646                     }
647                 }
648                 if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
649                     for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
650                         if (openvswitchOtherConfigs.getOtherConfigKey().equals(key)) {
651                             return openvswitchOtherConfigs.getOtherConfigValue();
652                         }
653                     }
654                 }
655                 break;
656             case PORT:
657                 List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
658                 for (OvsdbTerminationPointAugmentation port : ports) {
659                     if (port != null && port.getPortOtherConfigs() != null) {
660                         for (PortOtherConfigs portOtherConfigs : port.getPortOtherConfigs()) {
661                             if (portOtherConfigs.getOtherConfigKey().equals(key)) {
662                                 return portOtherConfigs.getOtherConfigValue();
663                             }
664                         }
665                     }
666                 }
667                 break;
668             default:
669                 LOG.debug("getOtherConfig: Couldn't find the specified OvsdbTable: ", table);
670                 return null;
671         }
672         return null;
673     }
674
675     public boolean addVlanToTp(long vlan) {
676         return false;
677     }
678
679     public boolean isTunnel(OvsdbTerminationPointAugmentation port) {
680         LOG.trace("SouthboundImpl#isTunnel: Interface : {}", port);
681
682         if(port.getInterfaceType() == null){
683             LOG.warn("No type found for the interface : {}", port);
684             return false;
685         }
686         return MdsalHelper.createOvsdbInterfaceType(
687                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_VXLAN)
688                 || MdsalHelper.createOvsdbInterfaceType(
689                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_GRE);
690     }
691
692     public String getOptionsValue(List<Options> options, String key) {
693         String value = null;
694         for (Options option : options) {
695             if (option.getOption().equals(key)) {
696                 value = option.getValue();
697             }
698         }
699         return value;
700     }
701
702     public Topology getOvsdbTopology() {
703         InstanceIdentifier<Topology> path = InstanceIdentifier
704                 .create(NetworkTopology.class)
705                 .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID));
706
707         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
708     }
709
710     public Long getOFPort(OvsdbTerminationPointAugmentation port) {
711         Long ofPort = 0L;
712         if (port.getOfport() != null) {
713             ofPort = port.getOfport();
714         }
715         return ofPort;
716     }
717
718     public Long getOFPort(Node bridgeNode, String portName) {
719         Long ofPort = 0L;
720         OvsdbTerminationPointAugmentation port = extractTerminationPointAugmentation(bridgeNode, portName);
721         if (port != null) {
722             ofPort = getOFPort(port);
723         } else {
724             TerminationPoint tp = readTerminationPoint(bridgeNode, null, portName);
725             if (tp != null) {
726                 port = tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
727                 if (port != null) {
728                     ofPort = getOFPort(port);
729                 }
730             }
731         }
732         return ofPort;
733     }
734     public OvsdbBridgeAugmentation getBridgeFromConfig(Node node, String bridge) {
735         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
736         InstanceIdentifier<Node> bridgeIid =
737                 MdsalHelper.createInstanceIdentifier(node.getKey(), bridge);
738         Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, bridgeIid);
739         if (bridgeNode != null) {
740             ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
741         }
742         return ovsdbBridgeAugmentation;
743     }
744 }