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