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