Fix gre tunnel connectivity
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UnimgrUtils.java
1 /*
2  * Copyright (c) 2015 CableLabs 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.unimgr.impl;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentationBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
77 import org.opendaylight.yangtools.yang.binding.DataObject;
78 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81
82 import com.google.common.base.Optional;
83 import com.google.common.collect.ImmutableBiMap;
84 import com.google.common.collect.Lists;
85 import com.google.common.util.concurrent.CheckedFuture;
86
87 public class UnimgrUtils {
88
89     private static final Logger LOG = LoggerFactory.getLogger(UnimgrUtils.class);
90
91     /**
92      * Creates and submit a Bridge Node to the Configuration Data Store.
93      * @param dataBroker The Data Broker Instance to create a transaction
94      * @param ovsdbNode The OVSDB node
95      * @param uni The UNI linked to the OVSDB node
96      * @param bridgeName The bridge name (example: br0)
97      */
98     public static void createBridgeNode(DataBroker dataBroker,
99                                         Node ovsdbNode,
100                                         UniAugmentation uni,
101                                         String bridgeName) {
102         LOG.info("Creating a bridge on node {}", ovsdbNode.getNodeId().getValue());
103         InstanceIdentifier<Node> ovsdbNodeIid = uni.getOvsdbNodeRef().getValue().firstIdentifierOf(Node.class);
104         if (ovsdbNodeIid != null) {
105             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
106             InstanceIdentifier<Node> bridgeIid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode,
107                                                                                        bridgeName);
108             NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId()
109                                            + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
110                                            + bridgeName);
111             bridgeNodeBuilder.setNodeId(bridgeNodeId);
112             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
113             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
114             ovsdbBridgeAugmentationBuilder.setProtocolEntry(UnimgrUtils.createMdsalProtocols());
115             OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
116             ovsdbBridgeAugmentationBuilder.setManagedBy(ovsdbNodeRef);
117             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
118             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
119             transaction.put(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
120             transaction.submit();
121         } else {
122             LOG.info("OvsdbNodeRef is null");
123         }
124     }
125
126     /**
127      * Creates and submit a Bridge Node to the Configuration Data Store.
128      * @param dataBroker The Data Broker Instance to create a transaction
129      * @param ovsdbNodeIid The OVSDB node Instance Identifier
130      * @param uni The UNI linked to the OVSDB node
131      * @param bridgeName The bridge name (example: br0)
132      */
133     public static void createBridgeNode(DataBroker dataBroker,
134                                         InstanceIdentifier<Node> ovsdbNodeIid,
135                                         UniAugmentation uni,
136                                         String bridgeName) {
137         LOG.info("Creating a bridge on node {}", ovsdbNodeIid);
138         if (ovsdbNodeIid != null) {
139             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
140             Optional<Node> optionalOvsdbNode = UnimgrUtils.readNode(dataBroker,
141                                                                     LogicalDatastoreType.OPERATIONAL,
142                                                                     ovsdbNodeIid);
143             if (optionalOvsdbNode.isPresent()) {
144                 Node ovsdbNode = optionalOvsdbNode.get();
145                 InstanceIdentifier<Node> bridgeIid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode,
146                                                                                            bridgeName);
147                 NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId().getValue()
148                                                + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
149                                                + bridgeName);
150                 bridgeNodeBuilder.setNodeId(bridgeNodeId);
151                 OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
152                 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
153                 ovsdbBridgeAugmentationBuilder.setProtocolEntry(UnimgrUtils.createMdsalProtocols());
154                 OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
155                 ovsdbBridgeAugmentationBuilder.setManagedBy(ovsdbNodeRef);
156                 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class,
157                                                   ovsdbBridgeAugmentationBuilder.build());
158                 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
159                 transaction.put(LogicalDatastoreType.CONFIGURATION,
160                                 bridgeIid,
161                                 bridgeNodeBuilder.build());
162                 transaction.submit();
163             }
164         } else {
165             LOG.info("OvsdbNodeRef is null");
166         }
167     }
168
169     /**
170      * Creates a List of Controller Entry to be used when adding controllers
171      * to a Bridge.
172      * @param targetString The URI in string format of the Controller Entry
173      * @return A List of Controller Entry to be used when adding controllers
174      */
175     public static List<ControllerEntry> createControllerEntries(String targetString) {
176         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
177         ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
178         controllerEntryBuilder.setTarget(new Uri(targetString));
179         controllerEntries.add(controllerEntryBuilder.build());
180         return controllerEntries;
181     }
182
183     /**
184      * Creates a submit a GRE tunnel to the Configuration DataStore.
185      * @param dataBroker An instance of the Data Broker to create a transaction
186      * @param source The source UNI
187      * @param destination The destination UNI
188      * @param bridgeNode The bridge Node
189      * @param bridgeName The bridge name (example br0)
190      * @param portName The Port Name (example: eth0)
191      */
192     public static void createGreTunnel(DataBroker dataBroker,
193                                        Uni source,
194                                        Uni destination,
195                                        Node bridgeNode,
196                                        String bridgeName,
197                                        String portName) {
198         InstanceIdentifier<TerminationPoint> tpIid =
199                                                  UnimgrMapper.getTerminationPointIid(bridgeNode,
200                                                                                      portName);
201         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
202                                                      new OvsdbTerminationPointAugmentationBuilder();
203         tpAugmentationBuilder.setName(portName);
204         ArrayList<Options> options = Lists.newArrayList();
205         OptionsKey optionKey = new OptionsKey("remote_ip");
206         Options destinationIp = new OptionsBuilder()
207                                         .setOption(destination.getIpAddress().getIpv4Address().getValue())
208                                         .setKey(optionKey).setValue(destination.getIpAddress().getIpv4Address().getValue())
209                                         .build();
210         options.add(destinationIp);
211         tpAugmentationBuilder.setOptions(options);
212         tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get("gre"));
213         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
214         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
215         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
216         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
217         transaction.put(LogicalDatastoreType.CONFIGURATION,
218                         tpIid,
219                         tpBuilder.build());
220         transaction.submit();
221     }
222
223     /**
224      * Utility function used to create a protocol entry when creating a bridge node.
225      * @return A List of protocol entry
226      */
227     public static List<ProtocolEntry> createMdsalProtocols() {
228         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
229         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
230                 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
231         protocolList.add(new ProtocolEntryBuilder().
232                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
233         return protocolList;
234     }
235
236     /**
237      * Creates a Bridge Augmentation by using a UNI
238      * @param uni Contains data used to create the augmentation
239      * @return A Built OvsdbBridgeAugmentation with data.
240      * @throws Exception if the Ovsdb Node Reference cannot be found.
241      */
242     public static OvsdbBridgeAugmentation createOvsdbBridgeAugmentation(Uni uni) throws Exception {
243         OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
244         if (ovsdbNodeRef != null && ovsdbNodeRef.getValue() != null) {
245             UUID bridgeUuid = UUID.randomUUID();
246             OvsdbBridgeAugmentation ovsdbBridge = new OvsdbBridgeAugmentationBuilder()
247                                                         .setBridgeName(
248                                                                 new OvsdbBridgeName(UnimgrConstants.DEFAULT_BRIDGE_NAME))
249                                                         .setManagedBy(ovsdbNodeRef)
250                                                         .setBridgeUuid(
251                                                                 new Uuid(bridgeUuid.toString()))
252                                                         .build();
253             return ovsdbBridge;
254         } else {
255             throw new Exception("Ovsdb Node Reference does not exist !");
256         }
257     }
258
259     /**
260      * Creates a submit an OvsdbNode to the Configuration DataStore.
261      * @param dataBroker The instance of the Data Broker to create transactions.
262      * @param ovsdbNodeId The Ovsdb Node Id to use on creation
263      * @param uni The UNI's data
264      */
265     public static void createOvsdbNode(DataBroker dataBroker,
266                                        NodeId ovsdbNodeId,
267                                        Uni uni) {
268         InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(uni.getIpAddress());
269         try {
270             NodeKey ovsdbNodeKey = new NodeKey(ovsdbNodeId);
271             Node nodeData = new NodeBuilder()
272                                     .setNodeId(ovsdbNodeId)
273                                     .setKey(ovsdbNodeKey)
274                                     .addAugmentation(OvsdbNodeAugmentation.class,
275                                                      UnimgrUtils.createOvsdbNodeAugmentation(uni))
276                                     .build();
277             // Submit the node to the datastore
278             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
279             transaction.put(LogicalDatastoreType.CONFIGURATION, ovsdbNodeIid, nodeData);
280             transaction.submit();
281             LOG.info("Created and submitted a new OVSDB node {}", nodeData.getNodeId());
282         } catch (Exception e) {
283             LOG.error("Exception while creating OvsdbNodeAugmentation, " + "Uni is null. Node Id: {}", ovsdbNodeId);
284         }
285     }
286
287     /**
288      * Creates and submit an OvsdbNode by using the Data contained in the UniAugmentation
289      * @param dataBroker The instance of the DataBroker to create transactions
290      * @param uni The UNI's data
291      * @return The instance of the Node
292      */
293     public static Node createOvsdbNode(DataBroker dataBroker,
294                                        UniAugmentation uni) {
295         NodeId ovsdbNodeId = new NodeId(createOvsdbNodeId(uni.getIpAddress()));
296         try {
297             InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
298             NodeKey ovsdbNodeKey = new NodeKey(ovsdbNodeId);
299             Node nodeData = new NodeBuilder()
300                                     .setNodeId(ovsdbNodeId)
301                                     .setKey(ovsdbNodeKey)
302                                     .addAugmentation(OvsdbNodeAugmentation.class,
303                                                      UnimgrUtils.createOvsdbNodeAugmentation(uni))
304                                     .build();
305             // Submit the node to the datastore
306             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
307             transaction.put(LogicalDatastoreType.CONFIGURATION, ovsdbNodeIid, nodeData);
308             transaction.submit();
309             LOG.info("Created and submitted a new OVSDB node {}", nodeData.getNodeId());
310             return nodeData;
311         } catch (Exception e) {
312             LOG.error("Exception while creating OvsdbNodeAugmentation, " + "Uni is null. Node Id: {}", ovsdbNodeId);
313         }
314         return null;
315     }
316
317     /**
318      * Creates and Build the data for an OvsdbNodeAugmentation.
319      * @param uni The UNI"s data
320      * @return The built OsvdbNodeAugmentation
321      */
322     public static OvsdbNodeAugmentation createOvsdbNodeAugmentation(Uni uni) {
323         ConnectionInfo connectionInfos = new ConnectionInfoBuilder()
324                                                 .setRemoteIp(uni.getIpAddress())
325                                                 .setRemotePort(new PortNumber(UnimgrConstants.OVSDB_PORT))
326                                                 .build();
327         OvsdbNodeAugmentation ovsdbNode = new OvsdbNodeAugmentationBuilder()
328                                                 .setConnectionInfo(connectionInfos).build();
329         return ovsdbNode;
330     }
331
332     /**
333      * Creates an OVSDB node Id with an IP Address.
334      * @param ipAddress The IP address of the UNI (therefo the OVSDB node)
335      * @return A NodeId for a Specific Ovsdb Node Id
336      */
337     public static NodeId createOvsdbNodeId(IpAddress ipAddress) {
338         String nodeId = UnimgrConstants.OVSDB_PREFIX
339                         + ipAddress.getIpv4Address().getValue().toString()
340                         + ":"
341                         + UnimgrConstants.OVSDB_PORT;
342         return new NodeId(nodeId);
343     }
344
345     /**
346      * Creates a built OvsdbTerminationAugmentation with data
347      * @param uni The UNI's data
348      * @return A Built OvsdbTerminationPointAugmentation with data
349      */
350     public static OvsdbTerminationPointAugmentation createOvsdbTerminationPointAugmentation(Uni uni) {
351         // we will use nodeId to set interface port id
352         VlanId vlanID = new VlanId(1);
353         OvsdbTerminationPointAugmentation terminationPoint = new OvsdbTerminationPointAugmentationBuilder()
354                                                                      .setName(UnimgrConstants.DEFAULT_INTERNAL_IFACE)
355                                                                      .setVlanTag(vlanID)
356                                                                      .setVlanMode(VlanMode.Access)
357                                                                      .build();
358         return terminationPoint;
359     }
360
361     /**
362      * Creates and submit an UNI Node by using the Data contained in the UniAugmentation
363      * @param dataBroker The instance of the DataBroker to create transactions
364      * @param uni The UNI's data
365      * @return The instance of the UNI Node
366      */
367     public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
368         NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
369         boolean result = false;
370         try {
371             InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
372             NodeKey uniNodeKey = new NodeKey(uniNodeId);
373             Node nodeData = new NodeBuilder()
374                                 .setNodeId(uniNodeId)
375                                 .setKey(uniNodeKey)
376                                 .addAugmentation(UniAugmentation.class, uni)
377                                 .build();
378             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
379             transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
380             CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
381             future.checkedGet();
382             result = true;
383             LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
384         } catch (Exception e) {
385             LOG.error("Exception while creating Uni Node" + "Uni Node Id: {}", uniNodeId);
386         }
387         return result;
388     }
389
390     /**
391      * Creates an UNI node Id with an IP Address.
392      * @param ipAddress The IP address of the UNI
393      * @return A NodeId for a Specific UNI Node Id
394      */
395     public static NodeId createUniNodeId(IpAddress ipAddress) {
396         return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
397     }
398
399     /**
400      * Creates and Submit a termination point Node to the configuration DateStore.
401      * @param dataBroker The instance of the data broker to create transactions
402      * @param uni The UNI's data
403      * @param bridgeNode The Bridge node
404      * @param bridgeName The Bridge name (example: br0)
405      * @param portName The Port name (example: eth0)
406      * @param type The type of termination (example: gre) Refer to OVSDB_INTERFACE_TYPE_MAP
407      * to review the list of available Interface Types.
408      */
409     public static void createTerminationPointNode(DataBroker dataBroker,
410                                                   Uni uni,
411                                                   Node bridgeNode,
412                                                   String bridgeName,
413                                                   String portName,
414                                                   String type) {
415         InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
416                                                         .getTerminationPointIid(bridgeNode,
417                                                                                 portName);
418         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
419                                                      new OvsdbTerminationPointAugmentationBuilder();
420         tpAugmentationBuilder.setName(portName);
421         if (type != null) {
422             tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type));
423         }
424         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
425         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
426         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
427                                   tpAugmentationBuilder.build());
428         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
429         transaction.put(LogicalDatastoreType.CONFIGURATION,
430                         tpIid,
431                         tpBuilder.build());
432         transaction.submit();
433     }
434
435     /**
436      * Creates and Submit a termination point Node without specifying its interface type.
437      * @param dataBroker The instance of the data broker to create transactions
438      * @param uni The UNI's data
439      * @param bridgeNode The Bridge node
440      * @param bridgeName The Bridge name (example: br0)
441      * @param portName The Port name (example: eth0)
442      */
443     public static void createTerminationPointNode(DataBroker dataBroker,
444                                                   Uni uni,
445                                                   Node bridgeNode,
446                                                   String bridgeName,
447                                                   String portName) {
448         InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
449                                                         .getTerminationPointIid(bridgeNode,
450                                                                                 portName);
451         OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
452                                                      new OvsdbTerminationPointAugmentationBuilder();
453         tpAugmentationBuilder.setName(portName);
454         tpAugmentationBuilder.setInterfaceType(null);
455         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
456         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
457         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
458                                   tpAugmentationBuilder.build());
459         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
460         transaction.put(LogicalDatastoreType.CONFIGURATION,
461                         tpIid,
462                         tpBuilder.build());
463         transaction.submit();
464     }
465
466     /**
467      * Deletes a generic node
468      * @param dataBroker The instance of the data broker to create transactions
469      * @param store The DataStore where the delete
470      * @param path The path to delete
471      * @return An instance of a generic Data Object
472      */
473     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
474             DataBroker dataBroker,
475             final LogicalDatastoreType store,
476             final InstanceIdentifier<D> path)  {
477         boolean result = false;
478         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
479         transaction.delete(store, path);
480         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
481         try {
482             future.checkedGet();
483             result = true;
484         } catch (TransactionCommitFailedException e) {
485             LOG.warn("Failed to delete {} ", path, e);
486         }
487         return result;
488     }
489
490     /**
491      * Deletes a termination Point from the configuration data store.
492      * @param dataBroker The instance of the data broker to create transactions
493      * @param terminationPoint The Termination Point of the OVSDB bridge
494      * @param ovsdbNode The ovsdb Node
495      * @return A checked Future
496      */
497     public static CheckedFuture<Void,
498                                 TransactionCommitFailedException>
499                                 deleteTerminationPoint(DataBroker dataBroker,
500                                                        TerminationPoint terminationPoint,
501                                                        Node ovsdbNode) {
502         InstanceIdentifier<TerminationPoint> terminationPointPath =
503                                                  InstanceIdentifier
504                                                      .create(NetworkTopology.class)
505                                                      .child(Topology.class,
506                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
507                                                      .child(Node.class,
508                                                             ovsdbNode.getKey())
509                                                      .child(TerminationPoint.class,
510                                                             terminationPoint.getKey());
511         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
512         transaction.delete(LogicalDatastoreType.CONFIGURATION, terminationPointPath);
513         transaction.delete(LogicalDatastoreType.OPERATIONAL, terminationPointPath);
514         transaction.submit();
515         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
516         return future;
517     }
518
519     /**
520      * Generic function to delete a node on a specific dataStore
521      * @param dataBroker The instance of the data broker to create transactions.
522      * @param genericNode The instance identifier of a generic node
523      * @param store The dataStore where to send and submit the delete call.
524      */
525     public static boolean deleteNode(DataBroker dataBroker,
526                                   InstanceIdentifier<?> genericNode,
527                                   LogicalDatastoreType store) {
528         LOG.info("Received a request to delete node {}", genericNode);
529         boolean result = false;
530         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
531         transaction.delete(store, genericNode);
532         try {
533             transaction.submit().checkedGet();
534             return !result;
535         } catch (TransactionCommitFailedException e) {
536             LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
537         }
538         return result;
539     }
540
541     /**
542      * Extract a data object by using its instance indentifier and it's class type.
543      * @param changes Data Change object
544      * @param klazz Class type
545      * @return The extracted DataObject as an Object casted as the class type
546      */
547     public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extract(
548             Map<InstanceIdentifier<?>, DataObject> changes, Class<T> klazz) {
549         Map<InstanceIdentifier<T>,T> result = new HashMap<InstanceIdentifier<T>,T>();
550         if (changes != null && changes.entrySet() != null) {
551             for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
552                 if (klazz.isInstance(created.getValue())) {
553                     @SuppressWarnings("unchecked")
554                     T value = (T) created.getValue();
555                     Class<?> type = created.getKey().getTargetType();
556                     if (type.equals(klazz)) {
557                         @SuppressWarnings("unchecked") // Actually checked above
558                         InstanceIdentifier<T> iid = (InstanceIdentifier<T>) created.getKey();
559                         result.put(iid, value);
560                     }
561                 }
562             }
563         }
564         return result;
565     }
566
567     /**
568      * Extract original data from the data store.
569      * @param changes The dataChange object
570      * @param klazz The class type
571      * @return The DataObject casted as a Class type
572      */
573     public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractOriginal(
574             AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
575         return extract(changes.getOriginalData(),klazz);
576     }
577
578     /**
579      * Extracts the removed nodes
580      * @param changes he dataChange object
581      * @param klazz The class type
582      * @return A set to removed nodes as DataObject casted as the class type
583      */
584     public static <T extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
585             AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
586         Set<InstanceIdentifier<T>> result = new HashSet<InstanceIdentifier<T>>();
587         if (changes != null && changes.getRemovedPaths() != null) {
588             for (InstanceIdentifier<?> iid : changes.getRemovedPaths()) {
589                 if (iid.getTargetType().equals(klazz)) {
590                     @SuppressWarnings("unchecked") // Actually checked above
591                     InstanceIdentifier<T> iidn = (InstanceIdentifier<T>)iid;
592                     result.add(iidn);
593                 }
594             }
595         }
596         return result;
597     }
598
599     /**
600      * Search the Operational Datastore for a specific OvsdbNode.
601      * @param dataBroker The dataBroker instance to create transactions
602      * @param uni The UNI's data
603      * @return The Optional OvsdbNode
604      */
605     public static Optional<Node> findOvsdbNode(DataBroker dataBroker,
606                                                UniAugmentation uni) {
607         List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
608         Optional<Node> optionalOvsdb;
609         if (!ovsdbNodes.isEmpty()) {
610             for (Node ovsdbNode : ovsdbNodes) {
611                 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
612                                                                   .getAugmentation(OvsdbNodeAugmentation.class);
613                 if (ovsdbNodeAugmentation.getConnectionInfo()
614                                          .getRemoteIp()
615                                          .getIpv4Address()
616                         .equals(uni.getIpAddress().getIpv4Address())) {
617                     LOG.info("Found ovsdb node");
618                     optionalOvsdb = Optional.of(ovsdbNode);
619                     return optionalOvsdb;
620                 }
621             }
622         }
623         return Optional.absent();
624     }
625
626     /**
627      * Search the Operation DataStore for a specific UNI
628      * @param dataBroker The dataBroker instance to create transactions
629      * @param ipAddress The IP address of the UNI
630      * @return An Optional UNI Node
631      */
632     public static Optional<Node> findUniNode(DataBroker dataBroker,
633                                              IpAddress ipAddress) {
634         List<Node> uniNodes = getUniNodes(dataBroker);
635         if (!uniNodes.isEmpty()) {
636             for (Node uniNode : uniNodes) {
637                 UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
638                 if (uniAugmentation.getIpAddress().equals(ipAddress)) {
639                     LOG.info("Found Uni node");
640                     return Optional.of(uniNode);
641                 }
642             }
643         }
644         return Optional.absent();
645     }
646
647     /**
648      * Retrieves the connection information from an Ovsdb Connection by
649      * using the Ovsdb Node Id
650      * @param dataBroker The dataBroker instance to create transactions
651      * @param ovsdbNodeId The NodeId of the OVSDB node
652      * @return The ConnectionInfo object
653      */
654     public static ConnectionInfo getConnectionInfo(DataBroker dataBroker,
655                                                    NodeId ovsdbNodeId) {
656         InstanceIdentifier<Node> nodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
657         Optional<Node> node = readNode(dataBroker,
658                                        LogicalDatastoreType.OPERATIONAL,
659                                        nodeIid);
660         if (node.isPresent()) {
661             Node ovsdbNode = node.get();
662             OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
663                                                               .getAugmentation(OvsdbNodeAugmentation.class);
664             ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
665             return connectionInfo;
666         } else {
667             return null;
668         }
669     }
670
671     /**
672      * Retrieve the list of links in the Operational DataStore
673      * @param dataBroker The dataBroker instance to create transactions
674      * @return A list of Links retrieved from the Operational DataStore
675      */
676     public static List<Link> getEvcLinks(DataBroker dataBroker) {
677         List<Link> evcLinks = new ArrayList<>();
678         InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
679         Topology topology = UnimgrUtils.read(dataBroker,
680                                              LogicalDatastoreType.OPERATIONAL,
681                                              evcTopology);
682         if (topology != null && topology.getLink() != null) {
683             for (Link link : topology.getLink()) {
684                 EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
685                 if (evcAugmentation != null) {
686                     evcLinks.add(link);
687                 }
688             }
689         }
690         return evcLinks;
691     }
692
693     /**
694      * Retrieve the Local IP of the controller
695      * @return The LocalIp object of the Controller
696      */
697     public static IpAddress getLocalIp() {
698         String ip;
699         try {
700             ip = InetAddress.getLocalHost().getHostAddress();
701             Ipv4Address ipv4 = new Ipv4Address(ip);
702             IpAddress ipAddress = new IpAddress(ipv4);
703             return ipAddress;
704         } catch (UnknownHostException e) {
705             LOG.info("Unable to retrieve controller's ip address, using loopback.");
706         }
707         return new IpAddress(UnimgrConstants.LOCAL_IP);
708     }
709
710     /**
711      * Retrieve a list of Ovsdb Nodes from the Operational DataStore
712      * @param dataBroker The dataBroker instance to create transactions
713      * @return The Ovsdb Node retrieved from the Operational DataStore
714      */
715     public static List<Node> getOvsdbNodes(DataBroker dataBroker) {
716         List<Node> ovsdbNodes = new ArrayList<>();
717         InstanceIdentifier<Topology> ovsdbTopoIdentifier = UnimgrMapper.getOvsdbTopologyIid();
718         Topology topology = UnimgrUtils.read(dataBroker,
719                                              LogicalDatastoreType.OPERATIONAL,
720                                              ovsdbTopoIdentifier);
721         if (topology != null && topology.getNode() != null) {
722             for (Node node : topology.getNode()) {
723                 OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
724                 if (ovsdbNodeAugmentation != null) {
725                     ovsdbNodes.add(node);
726                 }
727             }
728         }
729         return ovsdbNodes;
730     }
731
732     /**
733      * Retrieve a list of Uni Nodes from the Configuration DataStore
734      * @param dataBroker The dataBroker instance to create transactions
735      * @return A list of Uni Nodes from the Config dataStore
736      */
737     public static List<Node> getUniNodes(DataBroker dataBroker) {
738         List<Node> uniNodes = new ArrayList<>();
739         InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
740         Topology topology = read(dataBroker,
741                                  LogicalDatastoreType.CONFIGURATION,
742                                  topologyInstanceIdentifier);
743         if (topology != null && topology.getNode() != null) {
744             for (Node node : topology.getNode()) {
745                 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
746                 if (uniAugmentation != null) {
747                     uniNodes.add(node);
748                 }
749             }
750         }
751         return uniNodes;
752     }
753
754     /**
755      * Retrieve a list of Uni Nodes on a specific DataStore
756      * @param dataBroker The dataBroker instance to create transactions
757      * @param store The store to which to send the read request
758      * @return A List of UNI Nodes.
759      */
760     public static List<Node> getUniNodes(DataBroker dataBroker,
761                                          LogicalDatastoreType store) {
762         List<Node> uniNodes = new ArrayList<>();
763         InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
764         Topology topology = read(dataBroker,
765                                  store,
766                                  topologyInstanceIdentifier);
767         if (topology != null && topology.getNode() != null) {
768             for (Node node : topology.getNode()) {
769                 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
770                 if (uniAugmentation != null) {
771                     uniNodes.add(node);
772                 }
773             }
774         }
775         return uniNodes;
776     }
777
778     /**
779      * Retrieve a list of Unis on a specific DataStore
780      * @param dataBroker instance to create transactions
781      * @param store to which send the read request
782      * @return A List of Unis.
783      */
784     public static List<UniAugmentation> getUnis(DataBroker dataBroker,
785                                          LogicalDatastoreType store) {
786         List<UniAugmentation> unis = new ArrayList<>();
787         InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
788         Topology topology = read(dataBroker,
789                                  store,
790                                  topologyInstanceIdentifier);
791         if (topology != null && topology.getNode() != null) {
792             for (Node node : topology.getNode()) {
793                 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
794                 if (uniAugmentation != null) {
795                     unis.add(uniAugmentation);
796                 }
797             }
798         }
799         return unis;
800     }
801
802     /**
803      * Retrieve a list of Unis on a specific DataStore
804      * @param dataBroker instance to create transactions
805      * @param store to which send the read request
806      * @param ipAddress of the required Uni
807      * @return uni.
808      */
809     public static UniAugmentation getUni(DataBroker dataBroker,
810                                          LogicalDatastoreType store, IpAddress ipAddress) {
811         InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
812         Topology topology = read(dataBroker,
813                                  store,
814                                  topologyInstanceIdentifier);
815         if (topology != null && topology.getNode() != null) {
816             for (Node node : topology.getNode()) {
817                 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
818                 if (uniAugmentation != null && uniAugmentation.getIpAddress().getIpv4Address().getValue().equals(ipAddress.getIpv4Address().getValue())) {
819                     return uniAugmentation;
820                 }
821             }
822         }
823         return null;
824     }
825
826     /**
827      * Read a specific datastore type and return a DataObject as a casted
828      * class type Object.
829      * @param dataBroker The dataBroker instance to create transactions
830      * @param store The store type to query
831      * @param path The generic path to query
832      * @return The DataObject as a casted Object
833      */
834     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
835             DataBroker dataBroker,
836             final LogicalDatastoreType store,
837             final InstanceIdentifier<D> path)  {
838         D result = null;
839         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
840         Optional<D> optionalDataObject;
841         CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
842         try {
843             optionalDataObject = future.checkedGet();
844             if (optionalDataObject.isPresent()) {
845                 result = optionalDataObject.get();
846             } else {
847                 LOG.debug("{}: Failed to read {}",
848                         Thread.currentThread().getStackTrace()[1], path);
849             }
850         } catch (ReadFailedException e) {
851             LOG.warn("Failed to read {} ", path, e);
852         }
853         transaction.close();
854         return result;
855     }
856
857     /**
858      * Read a specific node from the Operational Data store by default.
859      * @param dataBroker The dataBroker instance to create transactions
860      * @param genericNode The Instance Identifier of the Node
861      * @return The Optional Node instance
862      */
863     @Deprecated
864     public static final Optional<Node> readNode(DataBroker dataBroker,
865                                                 InstanceIdentifier<?> genericNode) {
866         ReadTransaction read = dataBroker.newReadOnlyTransaction();
867         InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
868         CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
869                                                               read.read(LogicalDatastoreType.OPERATIONAL,
870                                                                         nodeIid);
871         try {
872             return nodeFuture.checkedGet();
873         } catch (ReadFailedException e) {
874             LOG.info("Unable to read node with Iid {}", nodeIid);
875         }
876         return Optional.absent();
877     }
878
879     /**
880      * Read a specific Link from a specific datastore
881      * @param dataBroker The dataBroker instance to create transactions
882      * @param store The datastore type.
883      * @param genericNode The Instance Identifier of the Link
884      * @return An Optional Link instance
885      */
886     public static final Optional<Link> readLink(DataBroker dataBroker,
887                                                 LogicalDatastoreType store,
888                                                 InstanceIdentifier<?> genericNode) {
889         ReadTransaction read = dataBroker.newReadOnlyTransaction();
890         InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
891         CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
892         try {
893             return linkFuture.checkedGet();
894         } catch (ReadFailedException e) {
895             LOG.info("Unable to read node with Iid {}", linkIid);
896         }
897         return Optional.absent();
898     }
899
900     /**
901      * Read a specific node from a specific data store type.
902      * @param dataBroker The dataBroker instance to create transactions
903      * @param store The data store type
904      * @param genericNode The Instance Identifier of a specific Node
905      * @return An Optional Node instance
906      */
907     public static final Optional<Node> readNode(DataBroker dataBroker,
908                                                 LogicalDatastoreType store,
909                                                 InstanceIdentifier<?> genericNode) {
910         ReadTransaction read = dataBroker.newReadOnlyTransaction();
911         InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
912         CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
913                 .read(store, nodeIid);
914         try {
915             return nodeFuture.checkedGet();
916         } catch (ReadFailedException e) {
917             LOG.info("Unable to read node with Iid {}", nodeIid);
918         }
919         return Optional.absent();
920     }
921
922     /**
923      * Updates a specific Uni Node on a specific DataStore type
924      * @param dataStore The datastore type
925      * @param uniKey The UNI key
926      * @param uni The Uni's data
927      * @param ovsdbNode The Ovsdb Node
928      * @param dataBroker The dataBroker instance to create transactions
929      */
930     public static void updateUniNode(LogicalDatastoreType dataStore,
931                                      InstanceIdentifier<?> uniKey,
932                                      UniAugmentation uni,
933                                      Node ovsdbNode,
934                                      DataBroker dataBroker) {
935         InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
936         OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
937         UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
938         if (ovsdbNodeRef != null) {
939             updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
940         }
941         Optional<Node> optionalNode = readNode(dataBroker,
942                                                LogicalDatastoreType.CONFIGURATION,
943                                                uniKey);
944         if (optionalNode.isPresent()) {
945             Node node = optionalNode.get();
946             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
947             NodeBuilder nodeBuilder = new NodeBuilder();
948             nodeBuilder.setKey(node.getKey());
949             nodeBuilder.setNodeId(node.getNodeId());
950             nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
951             transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
952             transaction.submit();
953         }
954     }
955
956     /**
957      * Update a specific UNI node on a specific datastore type
958      * @param dataStore The datastore type
959      * @param uniKey The UNI key
960      * @param uni The Uni's data
961      * @param ovsdbNodeIid The Ovsdb Node Instance Identifier
962      * @param dataBroker The dataBroker instance to create transactions
963      */
964     public static void updateUniNode(LogicalDatastoreType dataStore,
965                                      InstanceIdentifier<?> uniKey,
966                                      UniAugmentation uni,
967                                      InstanceIdentifier<?> ovsdbNodeIid,
968                                      DataBroker dataBroker) {
969         OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
970         UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
971         if (ovsdbNodeRef != null) {
972             updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
973         }
974         Optional<Node> optionalNode = readNode(dataBroker,
975                                                LogicalDatastoreType.CONFIGURATION,
976                                                uniKey);
977         if (optionalNode.isPresent()) {
978             Node node = optionalNode.get();
979             WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
980             NodeBuilder nodeBuilder = new NodeBuilder();
981             nodeBuilder.setKey(node.getKey());
982             nodeBuilder.setNodeId(node.getNodeId());
983             nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
984             transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
985             transaction.submit();
986         }
987     }
988
989     /**
990      * Updates a specific EVC into a specific DataStore type
991      * @param dataStore The datastore type
992      * @param evcKey The EVC key
993      * @param evcAugmentation The EVC's data
994      * @param sourceUniIid The Source Uni Instance Identifier
995      * @param destinationUniIid The destination Uni Instance Identifier
996      * @param dataBroker The dataBroker instance to create transactions
997      */
998     public static void updateEvcNode(LogicalDatastoreType dataStore,
999                                      InstanceIdentifier<?> evcKey,
1000                                      EvcAugmentation evcAugmentation,
1001                                      InstanceIdentifier<?> sourceUniIid,
1002                                      InstanceIdentifier<?> destinationUniIid,
1003                                      DataBroker dataBroker) {
1004         EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
1005         if (sourceUniIid != null && destinationUniIid != null) {
1006             List<UniSource> sourceList = new ArrayList<UniSource>();
1007             UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey();
1008             short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder();
1009             IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress();
1010             UniSource uniSource = new UniSourceBuilder()
1011                                           .setOrder(sourceOrder)
1012                                           .setKey(sourceKey)
1013                                           .setIpAddress(sourceIp)
1014                                           .setUni(sourceUniIid)
1015                                           .build();
1016             sourceList.add(uniSource);
1017             updatedEvcBuilder.setUniSource(sourceList);
1018
1019             List<UniDest> destinationList = new ArrayList<UniDest>();
1020             UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey();
1021             short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder();
1022             IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress();
1023             UniDest uniDest = new UniDestBuilder()
1024                                       .setIpAddress(destIp)
1025                                       .setOrder(destOrder)
1026                                       .setKey(destKey)
1027                                       .setUni(destinationUniIid)
1028                                       .build();
1029             destinationList.add(uniDest);
1030             updatedEvcBuilder.setUniDest(destinationList);
1031             Optional<Link> optionalEvcLink = readLink(dataBroker,
1032                                                       LogicalDatastoreType.CONFIGURATION,
1033                                                       evcKey);
1034             if (optionalEvcLink.isPresent()) {
1035                 Link link = optionalEvcLink.get();
1036                 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
1037                 LinkBuilder linkBuilder = new LinkBuilder();
1038                 linkBuilder.setKey(link.getKey());
1039                 linkBuilder.setLinkId(link.getLinkId());
1040                 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
1041                 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
1042                 transaction.submit();
1043             }
1044         } else {
1045             LOG.info("Invalid instance identifiers for sourceUni and destUni.");
1046         }
1047     }
1048 }