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