383e0c55dee7a2c4ae83318e68bd1891e67ece7c
[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 getTerminationPointsOfBridge(Node node, String portName) {
387         OvsdbTerminationPointAugmentation tpAugmentation = extractTerminationPointAugmentation(node,portName);
388         if(tpAugmentation == null){
389             List<OvsdbTerminationPointAugmentation> tpAugmentations = readTerminationPointAugmentations(node);
390             if(tpAugmentations != null){
391                 for(OvsdbTerminationPointAugmentation ovsdbTpAugmentation : tpAugmentations){
392                     if(ovsdbTpAugmentation.getName().equals(portName)){
393                         return ovsdbTpAugmentation;
394                     }
395                 }
396             }
397         }
398         return tpAugmentation;
399     }
400
401     public OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
402         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
403         if (ovsdbBridgeAugmentation != null) {
404             List<TerminationPoint> terminationPoints = bridgeNode.getTerminationPoint();
405             if(terminationPoints != null){
406                 for(TerminationPoint terminationPoint : terminationPoints) {
407                     OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
408                             terminationPoint.getAugmentation( OvsdbTerminationPointAugmentation.class);
409                     if (ovsdbTerminationPointAugmentation != null
410                             && ovsdbTerminationPointAugmentation.getName().equals(portName)) {
411                         return ovsdbTerminationPointAugmentation;
412                     }
413                 }
414             }
415         }
416         return null;
417     }
418
419     public List<TerminationPoint> extractTerminationPoints(Node node) {
420         List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();
421         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
422         if (ovsdbBridgeAugmentation != null) {
423             terminationPoints.addAll(node.getTerminationPoint());
424         }
425         return terminationPoints;
426     }
427
428     public List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
429         List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<OvsdbTerminationPointAugmentation>();
430         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
431         if(terminationPoints != null && !terminationPoints.isEmpty()){
432             for(TerminationPoint tp : terminationPoints){
433                 tpAugmentations.add(tp.getAugmentation(OvsdbTerminationPointAugmentation.class));
434             }
435         }
436         return tpAugmentations;
437     }
438
439     public List<OvsdbTerminationPointAugmentation> readTerminationPointAugmentations(Node node) {
440         InstanceIdentifier<Node> bridgeNodeIid = MdsalHelper.createInstanceIdentifier(node.getNodeId());
441         Node operNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeNodeIid);
442         if(operNode != null){
443             return extractTerminationPointAugmentations(operNode);
444         }
445         return new ArrayList<OvsdbTerminationPointAugmentation>();
446     }
447
448     public String getInterfaceExternalIdsValue(
449             OvsdbTerminationPointAugmentation terminationPointAugmentation, String key) {
450         String value = null;
451         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
452         if (pairs != null && !pairs.isEmpty()) {
453             for (InterfaceExternalIds pair : pairs) {
454                 if (pair.getExternalIdKey().equals(key)) {
455                     value = pair.getExternalIdValue();
456                     break;
457                 }
458             }
459         }
460         return value;
461     }
462
463     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type) {
464         InstanceIdentifier<TerminationPoint> tpIid =
465                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
466         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
467                 new OvsdbTerminationPointAugmentationBuilder();
468
469         tpAugmentationBuilder.setName(portName);
470         if (type != null) {
471             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
472         }
473         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
474         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
475         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
476         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
477     }
478
479     public Boolean deleteTerminationPoint(Node bridgeNode, String portName) {
480         InstanceIdentifier<TerminationPoint> tpIid =
481                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
482         return mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
483     }
484
485     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName,
486             String type, Map<String, String> options) {
487         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
488                 bridgeNode, portName);
489         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder = new OvsdbTerminationPointAugmentationBuilder();
490
491         tpAugmentationBuilder.setName(portName);
492         if (type != null) {
493             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
494         }
495
496         List<Options> optionsList = new ArrayList<Options>();
497         for (Map.Entry<String, String> entry : options.entrySet()) {
498             OptionsBuilder optionsBuilder = new OptionsBuilder();
499             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
500             optionsBuilder.setOption(entry.getKey());
501             optionsBuilder.setValue(entry.getValue());
502             optionsList.add(optionsBuilder.build());
503         }
504         tpAugmentationBuilder.setOptions(optionsList);
505
506         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
507         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
508         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
509         /* TODO SB_MIGRATION should this be merge or mdsalUtils.put */
510         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
511     }
512
513     public TerminationPoint readTerminationPoint(Node bridgeNode, String bridgeName, String portName) {
514         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
515                 bridgeNode, portName);
516         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, tpIid);
517     }
518
519     public Boolean addTunnelTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type,
520                                         Map<String, String> options) {
521         return addTerminationPoint(bridgeNode, bridgeName, portName, type, options);
522     }
523
524     public Boolean isTunnelTerminationPointExist(Node bridgeNode, String bridgeName, String portName){
525         return readTerminationPoint(bridgeNode, bridgeName, portName) != null;
526     }
527
528     public Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
529         Map<String, String> option = new HashMap<String, String>();
530         option.put("peer", peerPortName);
531         return addTerminationPoint(node, bridgeName, portName, PATCH_PORT_TYPE, option);
532     }
533
534     public String getExternalId(Node node, OvsdbTables table, String key) {
535         switch (table) {
536         case BRIDGE:
537             OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
538             if (bridge != null && bridge.getBridgeExternalIds() != null) {
539                 for (BridgeExternalIds bridgeExternaIds :bridge.getBridgeExternalIds()) {
540                     if (bridgeExternaIds.getBridgeExternalIdKey().equals(key)) {
541                         return bridgeExternaIds.getBridgeExternalIdValue();
542                     }
543                 }
544             }
545             break;
546         case CONTROLLER:
547             LOG.warn("getExternalId: There is no external_id for OvsdbTables: ", table);
548             return null;
549         case OPENVSWITCH:
550             OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
551             if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
552                 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
553                     if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
554                         return openvswitchExternalIds.getExternalIdValue();
555                     }
556                 }
557             }
558             break;
559         case PORT:
560             List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
561             for (OvsdbTerminationPointAugmentation port : ports) {
562                 if (port != null && port.getPortExternalIds() != null) {
563                     for (PortExternalIds portExternalIds :port.getPortExternalIds()) {
564                         if (portExternalIds.getExternalIdKey().equals(key)) {
565                             return portExternalIds.getExternalIdValue();
566                         }
567                     }
568                 }
569             }
570             break;
571         default:
572             LOG.debug("getExternalId: Couldn't find the specified OvsdbTable: ", table);
573             return null;
574         }
575         return null;
576     }
577
578     public String getOtherConfig(Node node, OvsdbTables table, String key) {
579         switch (table) {
580             case BRIDGE:
581                 OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
582                 if (bridge != null && bridge.getBridgeOtherConfigs() != null) {
583                     for (BridgeOtherConfigs bridgeOtherConfigs : bridge.getBridgeOtherConfigs()) {
584                         if (bridgeOtherConfigs.getBridgeOtherConfigKey().equals(key)) {
585                             return bridgeOtherConfigs.getBridgeOtherConfigValue();
586                         }
587                     }
588                 }
589                 break;
590             case CONTROLLER:
591                 LOG.warn("getOtherConfig: There is no other_config for OvsdbTables: ", table);
592                 return null;
593
594             case OPENVSWITCH:
595                 OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
596                 if (ovsdbNode == null){
597                     ovsdbNode = readOvsdbNode(node);
598                 }
599                 if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
600                     for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
601                         if (openvswitchOtherConfigs.getOtherConfigKey().equals(key)) {
602                             return openvswitchOtherConfigs.getOtherConfigValue();
603                         }
604                     }
605                 }
606                 break;
607             case PORT:
608                 List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
609                 for (OvsdbTerminationPointAugmentation port : ports) {
610                     if (port != null && port.getPortOtherConfigs() != null) {
611                         for (PortOtherConfigs portOtherConfigs : port.getPortOtherConfigs()) {
612                             if (portOtherConfigs.getOtherConfigKey().equals(key)) {
613                                 return portOtherConfigs.getOtherConfigValue();
614                             }
615                         }
616                     }
617                 }
618                 break;
619             default:
620                 LOG.debug("getOtherConfig: Couldn't find the specified OvsdbTable: ", table);
621                 return null;
622         }
623         return null;
624     }
625
626     public boolean addVlanToTp(long vlan) {
627         return false;
628     }
629
630     public boolean isTunnel(OvsdbTerminationPointAugmentation port) {
631         LOG.trace("SouthboundImpl#isTunnel: Interface : {}", port);
632
633         if(port.getInterfaceType() == null){
634             LOG.warn("No type found for the interface : {}", port);
635             return false;
636         }
637         return MdsalHelper.createOvsdbInterfaceType(
638                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_VXLAN)
639                 || MdsalHelper.createOvsdbInterfaceType(
640                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_GRE);
641     }
642
643     public String getOptionsValue(List<Options> options, String key) {
644         String value = null;
645         for (Options option : options) {
646             if (option.getKey().equals(key)) {
647                 value = option.getValue();
648             }
649         }
650         return value;
651     }
652
653     public Topology getOvsdbTopology() {
654         InstanceIdentifier<Topology> path = InstanceIdentifier
655                 .create(NetworkTopology.class)
656                 .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID));
657
658         Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
659         return topology;
660     }
661
662     public Long getOFPort(OvsdbTerminationPointAugmentation port) {
663         Long ofPort = 0L;
664         if (port.getOfport() != null) {
665             ofPort = port.getOfport();
666         }
667         return ofPort;
668     }
669
670     public Long getOFPort(Node bridgeNode, String portName) {
671         Long ofPort = 0L;
672         OvsdbTerminationPointAugmentation port = extractTerminationPointAugmentation(bridgeNode, portName);
673         if (port != null) {
674             ofPort = getOFPort(port);
675         } else {
676             TerminationPoint tp = readTerminationPoint(bridgeNode, null, portName);
677             if (tp != null) {
678                 port = tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
679                 if (port != null) {
680                     ofPort = getOFPort(port);
681                 }
682             }
683         }
684         return ofPort;
685     }
686 }