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