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