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