MdsalUtils#getOtherConfig() & MdsalUtils#getExternalIds().
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / MdsalUtils.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;
9
10 import java.math.BigInteger;
11 import java.security.InvalidParameterException;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
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.OpenvswitchExternalIds;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 import com.google.common.base.Optional;
64 import com.google.common.collect.ImmutableBiMap;
65 import com.google.common.util.concurrent.CheckedFuture;
66
67 /**
68  * Utility class to wrap mdsal transactions.
69  *
70  * @author Sam Hague (shague@redhat.com)
71  */
72 public class MdsalUtils {
73     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
74     private static DataBroker databroker = null;
75     private static final int OVSDB_UPDATE_TIMEOUT = 500;
76
77     /**
78      * Class constructor setting the data broker.
79      *
80      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
81      */
82     public MdsalUtils(DataBroker dataBroker) {
83         this.databroker = dataBroker;
84     }
85
86     /**
87      * Executes delete as a blocking transaction.
88      *
89      * @param store {@link LogicalDatastoreType} which should be modified
90      * @param path {@link InstanceIdentifier} to read from
91      * @param <D> the data object type
92      * @return the result of the request
93      */
94     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
95             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
96         boolean result = false;
97         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
98         transaction.delete(store, path);
99         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
100         try {
101             future.checkedGet();
102             result = true;
103         } catch (TransactionCommitFailedException e) {
104             LOG.warn("Failed to delete {} ", path, e);
105         }
106         return result;
107     }
108
109     /**
110      * Executes merge as a blocking transaction.
111      *
112      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
113      * @param path {@link InstanceIdentifier} for path to read
114      * @param <D> the data object type
115      * @return the result of the request
116      */
117     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
118             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
119         boolean result = false;
120         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
121         transaction.merge(logicalDatastoreType, path, data, true);
122         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
123         try {
124             future.checkedGet();
125             result = true;
126         } catch (TransactionCommitFailedException e) {
127             LOG.warn("Failed to merge {} ", path, e);
128         }
129         return result;
130     }
131
132     /**
133      * Executes put as a blocking transaction.
134      *
135      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
136      * @param path {@link InstanceIdentifier} for path to read
137      * @param <D> the data object type
138      * @return the result of the request
139      */
140     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
141             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
142         boolean result = false;
143         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
144         transaction.put(logicalDatastoreType, path, data, true);
145         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
146         try {
147             future.checkedGet();
148             result = true;
149         } catch (TransactionCommitFailedException e) {
150             LOG.warn("Failed to put {} ", path, e);
151         }
152         return result;
153     }
154
155     /**
156      * Executes read as a blocking transaction.
157      *
158      * @param store {@link LogicalDatastoreType} to read
159      * @param path {@link InstanceIdentifier} for path to read
160      * @param <D> the data object type
161      * @return the result as the data object requested
162      */
163     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
164             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
165         D result = null;
166         final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
167         Optional<D> optionalDataObject;
168         CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
169         try {
170             optionalDataObject = future.checkedGet();
171             if (optionalDataObject.isPresent()) {
172                 result = optionalDataObject.get();
173             } else {
174                 LOG.debug("{}: Failed to read {}",
175                         Thread.currentThread().getStackTrace()[1], path);
176             }
177         } catch (ReadFailedException e) {
178             LOG.warn("Failed to read {} ", path, e);
179         }
180         transaction.close();
181         return result;
182     }
183
184     public static ConnectionInfo getConnectionInfo(Node node) {
185         ConnectionInfo connectionInfo = null;
186         OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
187         if (ovsdbNodeAugmentation != null) {
188             connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
189         }
190         return connectionInfo;
191     }
192
193     public static String getOvsdbNodeUUID(Node node) {
194         String nodeUUID = null;
195         OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
196         if (ovsdbNodeAugmentation != null) {
197             // TODO replace with proper uuid and not the system-id
198             nodeUUID = getOsdbNodeExternalIdsValue(ovsdbNodeAugmentation, "system-id");
199         }
200         return nodeUUID;
201     }
202
203     public static String getOsdbNodeExternalIdsValue(OvsdbNodeAugmentation ovsdbNodeAugmentation, String key) {
204         String value = null;
205         List<OpenvswitchExternalIds> pairs = ovsdbNodeAugmentation.getOpenvswitchExternalIds();
206         for (OpenvswitchExternalIds pair : pairs) {
207             if (pair.getKey().equals(key)) {
208                 value = pair.getExternalIdValue();
209             }
210         }
211         return value;
212     }
213
214     public static boolean addBridge(Node ovsdbNode, String bridgeName, String target)
215             throws InterruptedException, InvalidParameterException {
216         boolean result = false;
217
218         LOG.info("addBridge: node: {}, bridgeName: {}, target: {}", ovsdbNode, bridgeName, target);
219         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
220         if (connectionInfo != null) {
221             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
222             InstanceIdentifier<Node> bridgeIid =
223                     SouthboundMapper.createInstanceIdentifier(connectionInfo, new OvsdbBridgeName(bridgeName));
224             NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
225             bridgeNodeBuilder.setNodeId(bridgeNodeId);
226             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
227             //ovsdbBridgeAugmentationBuilder.setControllerEntry(setControllerEntries(target));
228             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
229             ovsdbBridgeAugmentationBuilder.setProtocolEntry(createMdsalProtocols());
230             ovsdbBridgeAugmentationBuilder.setFailMode(
231                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get("secure"));
232             setManagedByForBridge(ovsdbBridgeAugmentationBuilder, connectionInfo);
233             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
234
235             result = merge(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
236             LOG.info("addBridge: result: {}", result);
237             Thread.sleep(OVSDB_UPDATE_TIMEOUT);
238             setControllerForBridge(ovsdbNode, bridgeName, target);
239             Thread.sleep(OVSDB_UPDATE_TIMEOUT);
240         } else {
241             throw new InvalidParameterException("Could not find ConnectionInfo");
242         }
243         return result;
244     }
245
246     public static OvsdbBridgeAugmentation readBridge(Node node, String name) {
247         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
248         ConnectionInfo connectionInfo = getConnectionInfo(node);
249         if (connectionInfo != null) {
250             InstanceIdentifier<Node> bridgeIid =
251                     SouthboundMapper.createInstanceIdentifier(connectionInfo,
252                             new OvsdbBridgeName(name));
253             Node bridgeNode = read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
254             if (bridgeNode != null) {
255                 ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
256             }
257         }
258         return ovsdbBridgeAugmentation;
259     }
260
261     public static Uuid getBridgeUuid(Node node, String name) {
262         Uuid uuid = null;
263         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = readBridge(node, name);
264         if (ovsdbBridgeAugmentation != null) {
265             uuid = ovsdbBridgeAugmentation.getBridgeUuid();
266         }
267         return uuid;
268     }
269
270     private static void setManagedByForBridge(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
271                                      ConnectionInfo connectionInfo) {
272         InstanceIdentifier<Node> connectionNodePath = SouthboundMapper.createInstanceIdentifier(connectionInfo);
273         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
274     }
275
276     private static void setControllerForBridge(Node ovsdbNode, String bridgeName, String targetString) {
277         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
278         if (connectionInfo != null) {
279             for (ControllerEntry controllerEntry: createControllerEntries(targetString)) {
280                 InstanceIdentifier<ControllerEntry> iid =
281                         SouthboundMapper.createInstanceIdentifier(connectionInfo, new OvsdbBridgeName(bridgeName))
282                                 .augmentation(OvsdbBridgeAugmentation.class)
283                                 .child(ControllerEntry.class, controllerEntry.getKey());
284
285                 boolean result = put(LogicalDatastoreType.CONFIGURATION, iid, controllerEntry);
286                 LOG.info("addController: result: {}", result);
287             }
288         }
289     }
290
291     private static List<ControllerEntry> createControllerEntries(String targetString) {
292         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
293         ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
294         controllerEntryBuilder.setTarget(new Uri(targetString));
295         controllerEntries.add(controllerEntryBuilder.build());
296         return controllerEntries;
297     }
298
299     private static List<ProtocolEntry> createMdsalProtocols() {
300         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
301         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
302                 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
303         protocolList.add(new ProtocolEntryBuilder().
304                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
305         return protocolList;
306     }
307
308     public static long getDataPathId(Node node) {
309         long dpid = 0L;
310         String datapathId = getDatapathId(node);
311         if (datapathId != null) {
312             dpid = new BigInteger(datapathId.replaceAll(":", ""), 16).longValue();
313         }
314         return dpid;
315     }
316
317     public static String getDatapathId(Node node) {
318         String datapathId = null;
319         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
320         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
321             datapathId = node.getAugmentation(OvsdbBridgeAugmentation.class).getDatapathId().getValue();
322         }
323         return datapathId;
324     }
325
326     public static String getDatapathId(OvsdbBridgeAugmentation ovsdbBridgeAugmentation) {
327         String datapathId = null;
328         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
329             datapathId = ovsdbBridgeAugmentation.getDatapathId().getValue();
330         }
331         return datapathId;
332     }
333
334     public static OvsdbBridgeAugmentation getBridge(Node node, String name) {
335         OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
336         if (bridge != null) {
337             if (!bridge.getBridgeName().equals(name)) {
338                 bridge = null;
339             }
340         }
341         return bridge;
342     }
343
344     public static String extractBridgeName(Node node) {
345         return (node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue());
346     }
347
348     public static OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
349         return node.getAugmentation(OvsdbBridgeAugmentation.class);
350     }
351
352     public static List<Node> getAllBridgesOnOvsdbNode(Node node) {
353         return null;
354     }
355
356     public static OvsdbNodeAugmentation extractOvsdbNodeAugmentation(Node node) {
357         return node.getAugmentation(OvsdbNodeAugmentation.class);
358     }
359
360     /**
361      * Method read ports from bridge node. Method will check if the provided node
362      * has the ports details, if not, it will read from Operational data store.
363      * @param node
364      * @return
365      */
366     public static List<OvsdbTerminationPointAugmentation> getTerminationPointsOfBridge(Node node) {
367         List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations( node);
368         if(tpAugmentations.isEmpty()){
369             tpAugmentations = readTerminationPointAugmentationFromDataStore(node);
370         }
371         return tpAugmentations;
372     }
373
374     public static OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
375         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
376         if (ovsdbBridgeAugmentation != null) {
377             List<TerminationPoint> terminationPoints = bridgeNode.getTerminationPoint();
378             for(TerminationPoint terminationPoint : terminationPoints) {
379                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
380                         terminationPoint.getAugmentation( OvsdbTerminationPointAugmentation.class);
381                 if (ovsdbTerminationPointAugmentation != null
382                         && ovsdbTerminationPointAugmentation.getName().equals(portName)) {
383                     return ovsdbTerminationPointAugmentation;
384                 }
385             }
386         }
387         return null;
388     }
389
390     public static List<TerminationPoint> extractTerminationPoints(Node node) {
391         List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();
392         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
393         if (ovsdbBridgeAugmentation != null) {
394             terminationPoints.addAll(node.getTerminationPoint());
395         }
396         return terminationPoints;
397     }
398
399     public static List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
400         List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<OvsdbTerminationPointAugmentation>();
401         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
402         if(terminationPoints != null && !terminationPoints.isEmpty()){
403             for(TerminationPoint tp : terminationPoints){
404                 tpAugmentations.add(tp.getAugmentation(OvsdbTerminationPointAugmentation.class));
405             }
406         }
407         return tpAugmentations;
408     }
409
410     public static List<OvsdbTerminationPointAugmentation> readTerminationPointAugmentationFromDataStore( Node node ) {
411         InstanceIdentifier<Node> bridgeNodeIid = SouthboundMapper.createInstanceIdentifier(node.getNodeId());
412         Node operNode = read(LogicalDatastoreType.OPERATIONAL, bridgeNodeIid);
413         if(operNode != null){
414             return extractTerminationPointAugmentations(operNode);
415         }
416         return new ArrayList<OvsdbTerminationPointAugmentation>();
417     }
418
419     public static String getInterfaceExternalIdsValue(
420             OvsdbTerminationPointAugmentation terminationPointAugmentation,String key) {
421         String value = null;
422         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
423         if (pairs != null) {
424             for (InterfaceExternalIds pair : pairs) {
425                 if (pair.getKey().equals(key)) {
426                     value = pair.getExternalIdValue();
427                 }
428             }
429         }
430         return value;
431     }
432
433     public static Boolean addTerminationPoint(Node bridgeNode, String bridgeName, String portName) {
434         InstanceIdentifier<TerminationPoint> tpIid =
435                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
436         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
437                 new OvsdbTerminationPointAugmentationBuilder();
438
439         tpAugmentationBuilder.setName(portName);
440         tpAugmentationBuilder.setInterfaceType(InterfaceTypeInternal.class);
441         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
442         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
443         return put(LogicalDatastoreType.CONFIGURATION,tpIid,tpBuilder.build());
444     }
445
446     public static Boolean deleteTerminationPoint(Node bridgeNode, String portName) {
447         InstanceIdentifier<TerminationPoint> tpIid =
448                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
449         return delete(LogicalDatastoreType.CONFIGURATION,tpIid);
450     }
451
452     public static Boolean addTunnelTerminationPoint(Node bridgeNode, String bridgeName, String portName, String type,
453                                         Map<String, String> options) {
454         InstanceIdentifier<TerminationPoint> tpIid =
455                 MdsalHelper.createTerminationPointInstanceIdentifier(bridgeNode, portName);
456         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
457                 new OvsdbTerminationPointAugmentationBuilder();
458
459         tpAugmentationBuilder.setName(portName);
460         tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
461
462         List<Options> optionsList = new ArrayList<Options>();
463         for(Map.Entry<String,String> entry : options.entrySet()){
464             OptionsBuilder optionsBuilder = new OptionsBuilder();
465             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
466             optionsBuilder.setOption(entry.getKey());
467             optionsBuilder.setValue(entry.getValue());
468             optionsList.add(optionsBuilder.build());
469         }
470         tpAugmentationBuilder.setOptions(optionsList);
471
472         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
473         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
474         return put(LogicalDatastoreType.CONFIGURATION,tpIid,tpBuilder.build());
475     }
476
477     public static Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
478         return false;
479     }
480
481     public static String getExternalId(Node node, OvsdbTables table, String key) {
482         switch (table) {
483         case BRIDGE:
484             for (BridgeExternalIds bridgeExternaIds :extractBridgeAugmentation(node).getBridgeExternalIds()) {
485                 if (bridgeExternaIds.getBridgeExternalIdKey().equals(key)) {
486                     return bridgeExternaIds.getBridgeExternalIdValue();
487                 }
488             }
489         case CONTROLLER:
490             LOG.debug("There is no external_id for OvsdbTables: ", table);
491             return null;
492         case OPENVSWITCH:
493             for ( OpenvswitchExternalIds openvswitchExternalIds :extractOvsdbNodeAugmentation(node).getOpenvswitchExternalIds()) {
494                 if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
495                     return openvswitchExternalIds.getExternalIdValue();
496                 }
497             }
498         case PORT:
499             for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation :extractTerminationPointAugmentations(node)) {
500                 for (PortExternalIds portExternalIds :ovsdbTerminationPointAugmentation.getPortExternalIds()) {
501                     if (portExternalIds.getExternalIdKey().equals(key)) {
502                         return portExternalIds.getExternalIdValue();
503                     }
504                 }
505             }
506         default:
507             LOG.debug("Couldn't find the specified OvsdbTables: ", table);
508             return null;
509         }
510     }
511
512     public static String getOtherConfig(Node node, OvsdbTables table, String key) {
513         switch (table) {
514             case BRIDGE:
515                 for (BridgeOtherConfigs bridgeOtherConfigs :extractBridgeAugmentation(node).getBridgeOtherConfigs()) {
516                     if (bridgeOtherConfigs.getBridgeOtherConfigKey().equals(key)) {
517                         return bridgeOtherConfigs.getBridgeOtherConfigValue();
518                     }
519                 }
520             case CONTROLLER:
521                 LOG.debug("There is no other_config for OvsdbTables: ", table);
522                 return null;
523             case OPENVSWITCH:
524                 for ( OpenvswitchOtherConfigs openvswitchOtherConfigs :extractOvsdbNodeAugmentation(node).getOpenvswitchOtherConfigs()) {
525                     if (openvswitchOtherConfigs.getOtherConfigKey().equals(key)) {
526                         return openvswitchOtherConfigs.getOtherConfigValue();
527                     }
528                 }
529             case PORT:
530                 for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation :extractTerminationPointAugmentations(node)) {
531                     for (PortOtherConfigs portOtherConfigs :ovsdbTerminationPointAugmentation.getPortOtherConfigs()) {
532                         if (portOtherConfigs.getOtherConfigKey().equals(key)) {
533                             return portOtherConfigs.getOtherConfigValue();
534                         }
535                     }
536                 }
537             default:
538                 LOG.debug("Couldn't find the specified OvsdbTables: ", table);
539                 return null;
540         }
541     }
542
543     public static boolean addVlanToTp(long vlan) {
544         return false;
545     }
546
547     public static boolean isTunnel(OvsdbTerminationPointAugmentation port) {
548         return SouthboundMapper.createOvsdbInterfaceType(
549                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_VXLAN)
550                 || SouthboundMapper.createOvsdbInterfaceType(
551                 port.getInterfaceType()).equals(NetworkHandler.NETWORK_TYPE_GRE);
552     }
553
554     public static String getOptionsValue(List<Options> options, String key) {
555         String value = null;
556         for (Options option : options) {
557             if (option.getKey().equals(key)) {
558                 value = option.getValue();
559             }
560         }
561         return value;
562     }
563
564     private static Topology getOvsdbTopology() {
565         InstanceIdentifier<Topology> path = InstanceIdentifier
566                 .create(NetworkTopology.class)
567                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
568
569         Topology topology = read(LogicalDatastoreType.OPERATIONAL, path);
570         return topology;
571     }
572
573 }