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