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