Merge "Clean up logging"
[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 ovsdbNode) {
89         ConnectionInfo connectionInfo = null;
90         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractOvsdbNode(ovsdbNode);
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 Node readOvsdbNode(Node bridgeNode) {
129         Node ovsdbNode = null;
130         OvsdbBridgeAugmentation bridgeAugmentation = extractBridgeAugmentation(bridgeNode);
131         if (bridgeAugmentation != null) {
132             InstanceIdentifier<Node> ovsdbNodeIid =
133                     (InstanceIdentifier<Node>) bridgeAugmentation.getManagedBy().getValue();
134             ovsdbNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
135         }else{
136             LOG.debug("readOvsdbNode: Provided node is not a bridge node : {}",bridgeNode);
137         }
138         return ovsdbNode;
139     }
140
141     public String getOvsdbNodeUUID(Node node) {
142         String nodeUUID = null;
143         OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
144         if (ovsdbNodeAugmentation != null) {
145             // TODO replace with proper uuid and not the system-id
146             nodeUUID = getOsdbNodeExternalIdsValue(ovsdbNodeAugmentation, "system-id");
147         }
148         return nodeUUID;
149     }
150
151     public String getOsdbNodeExternalIdsValue(OvsdbNodeAugmentation ovsdbNodeAugmentation, String key) {
152         String value = null;
153         List<OpenvswitchExternalIds> pairs = ovsdbNodeAugmentation.getOpenvswitchExternalIds();
154         if (pairs != null && !pairs.isEmpty()) {
155             for (OpenvswitchExternalIds pair : pairs) {
156                 if (pair.getExternalIdKey().equals(key)) {
157                     value = pair.getExternalIdValue();
158                     break;
159                 }
160             }
161         }
162         return value;
163     }
164
165     public boolean addBridge(Node ovsdbNode, String bridgeName, String target) {
166         boolean result = false;
167
168         LOG.info("addBridge: node: {}, bridgeName: {}, target: {}", ovsdbNode, bridgeName, target);
169         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
170         if (connectionInfo != null) {
171             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
172             InstanceIdentifier<Node> bridgeIid =
173                     MdsalHelper.createInstanceIdentifier(ovsdbNode.getKey(), bridgeName);
174             NodeId bridgeNodeId = MdsalHelper.createManagedNodeId(bridgeIid);
175             bridgeNodeBuilder.setNodeId(bridgeNodeId);
176             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
177             ovsdbBridgeAugmentationBuilder.setControllerEntry(createControllerEntries(target));
178             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
179             ovsdbBridgeAugmentationBuilder.setProtocolEntry(createMdsalProtocols());
180             ovsdbBridgeAugmentationBuilder.setFailMode(
181                     MdsalHelper.OVSDB_FAIL_MODE_MAP.inverse().get("secure"));
182             setManagedByForBridge(ovsdbBridgeAugmentationBuilder, ovsdbNode.getKey());
183             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
184
185             result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
186             LOG.info("addBridge: result: {}", result);
187         } else {
188             throw new InvalidParameterException("Could not find ConnectionInfo");
189         }
190         return result;
191     }
192
193     public boolean deleteBridge(Node ovsdbNode) {
194         boolean result = false;
195         InstanceIdentifier<Node> bridgeIid =
196                 MdsalHelper.createInstanceIdentifier(ovsdbNode.getNodeId());
197
198         result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, bridgeIid);
199         LOG.info("deleteBridge node: {}, bridgeName: {} result : {}", ovsdbNode, ovsdbNode.getNodeId(),result);
200         return result;
201     }
202
203     public OvsdbBridgeAugmentation readBridge(Node node, String name) {
204         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
205         ConnectionInfo connectionInfo = getConnectionInfo(node);
206         if (connectionInfo != null) {
207             InstanceIdentifier<Node> bridgeIid =
208             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
209             Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
210             if (bridgeNode != null) {
211                 ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
212             }
213         }
214         return ovsdbBridgeAugmentation;
215     }
216
217     public Node readBridgeNode(Node node, String name) {
218         Node ovsdbNode = node;
219         if (extractNodeAugmentation(ovsdbNode) == null) {
220             ovsdbNode = readOvsdbNode(node);
221         }
222         Node bridgeNode = null;
223         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
224         if (connectionInfo != null) {
225             InstanceIdentifier<Node> bridgeIid =
226             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
227             bridgeNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
228         }
229         return bridgeNode;
230     }
231
232     public Node getBridgeNode(Node node, String bridgeName) {
233         Node bridgeNode = null;
234         OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
235         if (bridge != null && bridge.getBridgeName().getValue().equals(bridgeName)) {
236                 bridgeNode = node;
237         } else {
238             bridgeNode = readBridgeNode(node, bridgeName);
239         }
240
241         return bridgeNode;
242     }
243
244     public String getBridgeUuid(Node node, String name) {
245         String uuid = null;
246         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = readBridge(node, name);
247         if (ovsdbBridgeAugmentation != null) {
248             uuid = ovsdbBridgeAugmentation.getBridgeUuid().getValue();
249         }
250         return uuid;
251     }
252
253     private void setManagedByForBridge(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
254                 NodeKey ovsdbNodeKey) {
255             InstanceIdentifier<Node> connectionNodePath = MdsalHelper.createInstanceIdentifier(ovsdbNodeKey.getNodeId());
256         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
257     }
258
259     private void setControllerForBridge(Node ovsdbNode, String bridgeName, String targetString) {
260         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
261         if (connectionInfo != null) {
262             for (ControllerEntry controllerEntry: createControllerEntries(targetString)) {
263                 InstanceIdentifier<ControllerEntry> iid =
264                         MdsalHelper.createInstanceIdentifier(ovsdbNode.getKey(), bridgeName)
265                                 .augmentation(OvsdbBridgeAugmentation.class)
266                                 .child(ControllerEntry.class, controllerEntry.getKey());
267
268                 boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, iid, controllerEntry);
269                 LOG.info("addController: result: {}", result);
270             }
271         }
272     }
273
274     private List<ControllerEntry> createControllerEntries(String targetString) {
275         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
276         ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
277         controllerEntryBuilder.setTarget(new Uri(targetString));
278         controllerEntries.add(controllerEntryBuilder.build());
279         return controllerEntries;
280     }
281
282     private List<ProtocolEntry> createMdsalProtocols() {
283         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
284         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
285                 MdsalHelper.OVSDB_PROTOCOL_MAP.inverse();
286         protocolList.add(new ProtocolEntryBuilder().
287                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
288         return protocolList;
289     }
290
291     public long getDataPathId(Node node) {
292         long dpid = 0L;
293         String datapathId = getDatapathId(node);
294         if (datapathId != null) {
295             dpid = new BigInteger(datapathId.replaceAll(":", ""), 16).longValue();
296         }
297         return dpid;
298     }
299
300     public String getDatapathId(Node node) {
301         String datapathId = null;
302         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
303         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
304             datapathId = node.getAugmentation(OvsdbBridgeAugmentation.class).getDatapathId().getValue();
305         }
306         return datapathId;
307     }
308
309     public String getDatapathId(OvsdbBridgeAugmentation ovsdbBridgeAugmentation) {
310         String datapathId = null;
311         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
312             datapathId = ovsdbBridgeAugmentation.getDatapathId().getValue();
313         }
314         return datapathId;
315     }
316
317     public OvsdbBridgeAugmentation getBridge(Node node, String name) {
318         OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
319         if ((bridge != null) && (!bridge.getBridgeName().getValue().equals(name))) {
320             bridge = null;
321         }
322         return bridge;
323     }
324
325     public OvsdbBridgeAugmentation getBridge(Node node) {
326         return node.getAugmentation(OvsdbBridgeAugmentation.class);
327     }
328
329     public String getBridgeName(Node node) {
330         String bridgeName = null;
331         OvsdbBridgeAugmentation bridge = getBridge(node);
332         if (bridge != null) {
333             bridgeName = bridge.getBridgeName().getValue();
334         }
335         return bridgeName;
336     }
337
338     public String extractBridgeName(Node node) {
339         return node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue();
340     }
341
342     public OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
343         if (node == null) {
344             return null;
345         }
346         return node.getAugmentation(OvsdbBridgeAugmentation.class);
347     }
348
349     public List<Node> getAllBridgesOnOvsdbNode(Node node) {
350         List<Node> nodes = new ArrayList<Node>();
351         List<ManagedNodeEntry> managedNodes = node.getAugmentation(OvsdbNodeAugmentation.class).getManagedNodeEntry();
352         for (ManagedNodeEntry managedNode : managedNodes) {
353             InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
354             Node bridgeNode = (Node) mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
355             if (bridgeNode != null) {
356                 nodes.add(bridgeNode);
357             }
358         }
359         return nodes;
360     }
361
362     public boolean isBridgeOnOvsdbNode(Node ovsdbNode, String bridgeName) {
363         boolean found = false;
364         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
365         if (ovsdbNodeAugmentation != null) {
366             List<ManagedNodeEntry> managedNodes = ovsdbNodeAugmentation.getManagedNodeEntry();
367             if (managedNodes != null) {
368                 for (ManagedNodeEntry managedNode : managedNodes) {
369                     InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
370                     if (bridgeIid.toString().contains(bridgeName)) {
371                         found = true;
372                         break;
373                     }
374                 }
375             }
376         }
377         return found;
378     }
379
380     public OvsdbNodeAugmentation extractNodeAugmentation(Node node) {
381         return node.getAugmentation(OvsdbNodeAugmentation.class);
382     }
383
384     /**
385      * Method read ports from bridge node. Method will check if the provided node
386      * has the ports details, if not, it will read from Operational data store.
387      * @param node
388      * @return
389      */
390     public List<OvsdbTerminationPointAugmentation> getTerminationPointsOfBridge(Node node) {
391         List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(node);
392         if(tpAugmentations.isEmpty()){
393             tpAugmentations = readTerminationPointAugmentations(node);
394         }
395         return tpAugmentations;
396     }
397
398     public OvsdbTerminationPointAugmentation getTerminationPointOfBridge(Node node, String portName) {
399         OvsdbTerminationPointAugmentation tpAugmentation = extractTerminationPointAugmentation(node,portName);
400         if(tpAugmentation == null){
401             List<OvsdbTerminationPointAugmentation> tpAugmentations = readTerminationPointAugmentations(node);
402             if(tpAugmentations != null){
403                 for(OvsdbTerminationPointAugmentation ovsdbTpAugmentation : tpAugmentations){
404                     if(ovsdbTpAugmentation.getName().equals(portName)){
405                         return ovsdbTpAugmentation;
406                     }
407                 }
408             }
409         }
410         return tpAugmentation;
411     }
412
413     public OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
414         if (bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class) != null) {
415             List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(bridgeNode);
416             for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation : tpAugmentations) {
417                 if (ovsdbTerminationPointAugmentation.getName().equals(portName)) {
418                     return ovsdbTerminationPointAugmentation;
419                 }
420             }
421         }
422         return null;
423     }
424
425     public List<TerminationPoint> extractTerminationPoints(Node node) {
426         List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();
427         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
428         if (ovsdbBridgeAugmentation != null) {
429             terminationPoints.addAll(node.getTerminationPoint());
430         }
431         return terminationPoints;
432     }
433
434     public List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
435         List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<OvsdbTerminationPointAugmentation>();
436         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
437         if(terminationPoints != null && !terminationPoints.isEmpty()){
438             for(TerminationPoint tp : terminationPoints){
439                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
440                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
441                 if (ovsdbTerminationPointAugmentation != null) {
442                     tpAugmentations.add(ovsdbTerminationPointAugmentation);
443                 }
444             }
445         }
446         return tpAugmentations;
447     }
448
449     public List<OvsdbTerminationPointAugmentation> readTerminationPointAugmentations(Node node) {
450         InstanceIdentifier<Node> bridgeNodeIid = MdsalHelper.createInstanceIdentifier(node.getNodeId());
451         Node operNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeNodeIid);
452         if(operNode != null){
453             return extractTerminationPointAugmentations(operNode);
454         }
455         return new ArrayList<OvsdbTerminationPointAugmentation>();
456     }
457
458     public String getInterfaceExternalIdsValue(
459             OvsdbTerminationPointAugmentation terminationPointAugmentation, String key) {
460         String value = null;
461         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
462         if (pairs != null && !pairs.isEmpty()) {
463             for (InterfaceExternalIds pair : pairs) {
464                 if (pair.getExternalIdKey().equals(key)) {
465                     value = pair.getExternalIdValue();
466                     break;
467                 }
468             }
469         }
470         return value;
471     }
472
473     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type) {
474         InstanceIdentifier<TerminationPoint> tpIid =
475                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
476         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
477                 new OvsdbTerminationPointAugmentationBuilder();
478
479         tpAugmentationBuilder.setName(portName);
480         if (type != null) {
481             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
482         }
483         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
484         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
485         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
486         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
487     }
488
489     public Boolean deleteTerminationPoint(Node bridgeNode, String portName) {
490         InstanceIdentifier<TerminationPoint> tpIid =
491                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
492         return mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
493     }
494
495     public Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName,
496             String type, Map<String, String> options) {
497         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
498                 bridgeNode, portName);
499         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder = new OvsdbTerminationPointAugmentationBuilder();
500
501         tpAugmentationBuilder.setName(portName);
502         if (type != null) {
503             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
504         }
505
506         List<Options> optionsList = new ArrayList<Options>();
507         for (Map.Entry<String, String> entry : options.entrySet()) {
508             OptionsBuilder optionsBuilder = new OptionsBuilder();
509             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
510             optionsBuilder.setOption(entry.getKey());
511             optionsBuilder.setValue(entry.getValue());
512             optionsList.add(optionsBuilder.build());
513         }
514         tpAugmentationBuilder.setOptions(optionsList);
515
516         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
517         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
518         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
519         /* TODO SB_MIGRATION should this be merge or mdsalUtils.put */
520         return mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, tpIid, tpBuilder.build());
521     }
522
523     public TerminationPoint readTerminationPoint(Node bridgeNode, String bridgeName, String portName) {
524         InstanceIdentifier<TerminationPoint> tpIid = MdsalHelper.createTerminationPointInstanceIdentifier(
525                 bridgeNode, portName);
526         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, tpIid);
527     }
528
529     public Boolean addTunnelTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type,
530                                         Map<String, String> options) {
531         return addTerminationPoint(bridgeNode, bridgeName, portName, type, options);
532     }
533
534     public Boolean isTunnelTerminationPointExist(Node bridgeNode, String bridgeName, String portName){
535         return readTerminationPoint(bridgeNode, bridgeName, portName) != null;
536     }
537
538     public Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
539         Map<String, String> option = new HashMap<String, String>();
540         option.put("peer", peerPortName);
541         return addTerminationPoint(node, bridgeName, portName, PATCH_PORT_TYPE, option);
542     }
543
544     public String getExternalId(Node node, OvsdbTables table, String key) {
545         switch (table) {
546         case BRIDGE:
547             OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
548             if (bridge != null && bridge.getBridgeExternalIds() != null) {
549                 for (BridgeExternalIds bridgeExternaIds :bridge.getBridgeExternalIds()) {
550                     if (bridgeExternaIds.getBridgeExternalIdKey().equals(key)) {
551                         return bridgeExternaIds.getBridgeExternalIdValue();
552                     }
553                 }
554             }
555             break;
556         case CONTROLLER:
557             LOG.warn("getExternalId: There is no external_id for OvsdbTables: ", table);
558             return null;
559         case OPENVSWITCH:
560             OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
561             if (ovsdbNode == null) {
562                 Node nodeFromReadOvsdbNode = readOvsdbNode(node);
563                 ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
564             }
565             if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
566                 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
567                     if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
568                         return openvswitchExternalIds.getExternalIdValue();
569                     }
570                 }
571             }
572             break;
573         case PORT:
574             List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
575             for (OvsdbTerminationPointAugmentation port : ports) {
576                 if (port != null && port.getPortExternalIds() != null) {
577                     for (PortExternalIds portExternalIds :port.getPortExternalIds()) {
578                         if (portExternalIds.getExternalIdKey().equals(key)) {
579                             return portExternalIds.getExternalIdValue();
580                         }
581                     }
582                 }
583             }
584             break;
585         default:
586             LOG.debug("getExternalId: Couldn't find the specified OvsdbTable: ", table);
587             return null;
588         }
589         return null;
590     }
591
592     public String getOtherConfig(Node node, OvsdbTables table, String key) {
593         switch (table) {
594             case BRIDGE:
595                 OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
596                 if (bridge != null && bridge.getBridgeOtherConfigs() != null) {
597                     for (BridgeOtherConfigs bridgeOtherConfigs : bridge.getBridgeOtherConfigs()) {
598                         if (bridgeOtherConfigs.getBridgeOtherConfigKey().equals(key)) {
599                             return bridgeOtherConfigs.getBridgeOtherConfigValue();
600                         }
601                     }
602                 }
603                 break;
604             case CONTROLLER:
605                 LOG.warn("getOtherConfig: There is no other_config for OvsdbTables: ", table);
606                 return null;
607
608             case OPENVSWITCH:
609                 OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
610                 if (ovsdbNode == null) {
611                     Node nodeFromReadOvsdbNode = readOvsdbNode(node);
612                     ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
613                 }
614                 if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
615                     for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
616                         if (openvswitchOtherConfigs.getOtherConfigKey().equals(key)) {
617                             return openvswitchOtherConfigs.getOtherConfigValue();
618                         }
619                     }
620                 }
621                 break;
622             case PORT:
623                 List <OvsdbTerminationPointAugmentation> ports = extractTerminationPointAugmentations(node);
624                 for (OvsdbTerminationPointAugmentation port : ports) {
625                     if (port != null && port.getPortOtherConfigs() != null) {
626                         for (PortOtherConfigs portOtherConfigs : port.getPortOtherConfigs()) {
627                             if (portOtherConfigs.getOtherConfigKey().equals(key)) {
628                                 return portOtherConfigs.getOtherConfigValue();
629                             }
630                         }
631                     }
632                 }
633                 break;
634             default:
635                 LOG.debug("getOtherConfig: Couldn't find the specified OvsdbTable: ", table);
636                 return null;
637         }
638         return null;
639     }
640
641     public boolean addVlanToTp(long vlan) {
642         return false;
643     }
644
645     public boolean isTunnel(OvsdbTerminationPointAugmentation port) {
646         LOG.trace("SouthboundImpl#isTunnel: Interface : {}", port);
647
648         if(port.getInterfaceType() == null){
649             LOG.warn("No type found for the interface : {}", port);
650             return false;
651         }
652         return MdsalHelper.createOvsdbInterfaceType(
653                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_VXLAN)
654                 || MdsalHelper.createOvsdbInterfaceType(
655                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_GRE);
656     }
657
658     public String getOptionsValue(List<Options> options, String key) {
659         String value = null;
660         for (Options option : options) {
661             if (option.getKey().equals(key)) {
662                 value = option.getValue();
663             }
664         }
665         return value;
666     }
667
668     public Topology getOvsdbTopology() {
669         InstanceIdentifier<Topology> path = InstanceIdentifier
670                 .create(NetworkTopology.class)
671                 .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID));
672
673         Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
674         return topology;
675     }
676
677     public Long getOFPort(OvsdbTerminationPointAugmentation port) {
678         Long ofPort = 0L;
679         if (port.getOfport() != null) {
680             ofPort = port.getOfport();
681         }
682         return ofPort;
683     }
684
685     public Long getOFPort(Node bridgeNode, String portName) {
686         Long ofPort = 0L;
687         OvsdbTerminationPointAugmentation port = extractTerminationPointAugmentation(bridgeNode, portName);
688         if (port != null) {
689             ofPort = getOFPort(port);
690         } else {
691             TerminationPoint tp = readTerminationPoint(bridgeNode, null, portName);
692             if (tp != null) {
693                 port = tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
694                 if (port != null) {
695                     ofPort = getOFPort(port);
696                 }
697             }
698         }
699         return ofPort;
700     }
701 }