Update UNI
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UnimgrUtils.java
1 /*
2  * Copyright (c) 2016 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.ovsdb.southbound.SouthboundMapper;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QueuesKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfigKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueList;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QueueListKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfigKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.Speed;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
99 import org.opendaylight.yangtools.yang.binding.DataObject;
100 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
101 import org.slf4j.Logger;
102 import org.slf4j.LoggerFactory;
103
104 import com.google.common.base.Optional;
105 import com.google.common.collect.ImmutableBiMap;
106 import com.google.common.collect.Lists;
107 import com.google.common.util.concurrent.CheckedFuture;
108
109 public class UnimgrUtils {
110
111     private static final Logger LOG = LoggerFactory.getLogger(UnimgrUtils.class);
112
113     /**
114      * Creates and submit a Bridge Node to the Configuration Data Store.
115      * @param dataBroker The Data Broker Instance to create a transaction
116      * @param ovsdbNode The OVSDB node
117      * @param uni The UNI linked to the OVSDB node
118      * @param bridgeName The bridge name (example: br0)
119      */
120     public static void createBridgeNode(DataBroker dataBroker,
121                                         Node ovsdbNode,
122                                         UniAugmentation uni,
123                                         String bridgeName) {
124         LOG.info("Creating a bridge on node {}", ovsdbNode.getNodeId().getValue());
125         final InstanceIdentifier<Node> ovsdbNodeIid = uni.getOvsdbNodeRef().getValue().firstIdentifierOf(Node.class);
126         if (ovsdbNodeIid != null) {
127             final NodeBuilder bridgeNodeBuilder = new NodeBuilder();
128             final InstanceIdentifier<Node> bridgeIid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode,
129                                                                                        bridgeName);
130             final NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId()
131                                            + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
132                                            + bridgeName);
133             bridgeNodeBuilder.setNodeId(bridgeNodeId);
134             final OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
135             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
136             ovsdbBridgeAugmentationBuilder.setProtocolEntry(UnimgrUtils.createMdsalProtocols());
137             final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
138             ovsdbBridgeAugmentationBuilder.setManagedBy(ovsdbNodeRef);
139             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
140             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
141             transaction.put(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
142             transaction.submit();
143         } else {
144             LOG.info("OvsdbNodeRef is null");
145         }
146     }
147
148     /**
149      * Creates and submit a Bridge Node to the Configuration Data Store.
150      * @param dataBroker The Data Broker Instance to create a transaction
151      * @param ovsdbNodeIid The OVSDB node Instance Identifier
152      * @param uni The UNI linked to the OVSDB node
153      * @param bridgeName The bridge name (example: br0)
154      */
155     public static void createBridgeNode(DataBroker dataBroker,
156                                         InstanceIdentifier<Node> ovsdbNodeIid,
157                                         UniAugmentation uni,
158                                         String bridgeName) {
159         LOG.info("Creating a bridge on node {}", ovsdbNodeIid);
160         if (ovsdbNodeIid != null) {
161             final NodeBuilder bridgeNodeBuilder = new NodeBuilder();
162             final Optional<Node> optionalOvsdbNode = UnimgrUtils.readNode(dataBroker,
163                                                                     LogicalDatastoreType.OPERATIONAL,
164                                                                     ovsdbNodeIid);
165             if (optionalOvsdbNode.isPresent()) {
166                 final Node ovsdbNode = optionalOvsdbNode.get();
167                 final InstanceIdentifier<Node> bridgeIid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode,
168                                                                                            bridgeName);
169                 final NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId().getValue()
170                                                + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
171                                                + bridgeName);
172                 bridgeNodeBuilder.setNodeId(bridgeNodeId);
173                 final OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
174                 ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
175                 ovsdbBridgeAugmentationBuilder.setProtocolEntry(UnimgrUtils.createMdsalProtocols());
176                 final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
177                 ovsdbBridgeAugmentationBuilder.setManagedBy(ovsdbNodeRef);
178                 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class,
179                                                   ovsdbBridgeAugmentationBuilder.build());
180                 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
181                 transaction.put(LogicalDatastoreType.CONFIGURATION,
182                                 bridgeIid,
183                                 bridgeNodeBuilder.build());
184                 transaction.submit();
185             }
186         } else {
187             LOG.info("OvsdbNodeRef is null");
188         }
189     }
190
191     /**
192      * Creates a List of Controller Entry to be used when adding controllers
193      * to a Bridge.
194      * @param targetString The URI in string format of the Controller Entry
195      * @return A List of Controller Entry to be used when adding controllers
196      */
197     public static List<ControllerEntry> createControllerEntries(String targetString) {
198         final List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
199         final ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
200         controllerEntryBuilder.setTarget(new Uri(targetString));
201         controllerEntries.add(controllerEntryBuilder.build());
202         return controllerEntries;
203     }
204
205     /**
206      * Creates a submit a GRE tunnel to the Configuration DataStore.
207      * @param dataBroker An instance of the Data Broker to create a transaction
208      * @param source The source UNI
209      * @param destination The destination UNI
210      * @param bridgeNode The bridge Node
211      * @param bridgeName The bridge name (example br0)
212      * @param portName The Port Name (example: eth0)
213      */
214     public static void createGreTunnel(DataBroker dataBroker,
215                                        Uni source,
216                                        Uni destination,
217                                        Node bridgeNode,
218                                        String bridgeName,
219                                        String portName) {
220         final InstanceIdentifier<TerminationPoint> tpIid =
221                                                  UnimgrMapper.getTerminationPointIid(bridgeNode,
222                                                                                      portName);
223         final OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
224                                                      new OvsdbTerminationPointAugmentationBuilder();
225         tpAugmentationBuilder.setName(portName);
226         final ArrayList<Options> options = Lists.newArrayList();
227         final OptionsKey optionKey = new OptionsKey("remote_ip");
228         final Options destinationIp = new OptionsBuilder()
229                                         .setOption(destination.getIpAddress().getIpv4Address().getValue())
230                                         .setKey(optionKey).setValue(destination.getIpAddress().getIpv4Address().getValue())
231                                         .build();
232         options.add(destinationIp);
233         tpAugmentationBuilder.setOptions(options);
234         tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get("gre"));
235         if (source.getSpeed() != null) {
236             final Uuid qosUuid = getQosUuid(dataBroker, source);
237             tpAugmentationBuilder.setQos(getQosUuid(dataBroker, source));
238             LOG.info("Updating Qos {} to termination point {}", qosUuid , bridgeName);
239         }
240         final TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
241         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
242         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class, tpAugmentationBuilder.build());
243         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
244         transaction.put(LogicalDatastoreType.CONFIGURATION,
245                         tpIid,
246                         tpBuilder.build());
247         transaction.submit();
248     }
249
250     /**
251      * Utility function used to create a protocol entry when creating a bridge node.
252      * @return A List of protocol entry
253      */
254     public static List<ProtocolEntry> createMdsalProtocols() {
255         final List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
256         final ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
257                 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
258         protocolList.add(new ProtocolEntryBuilder().
259                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
260         return protocolList;
261     }
262
263     /**
264      * Creates a Bridge Augmentation by using a UNI
265      * @param uni Contains data used to create the augmentation
266      * @return A Built OvsdbBridgeAugmentation with data.
267      * @throws Exception if the Ovsdb Node Reference cannot be found.
268      */
269     public static OvsdbBridgeAugmentation createOvsdbBridgeAugmentation(Uni uni) throws Exception {
270         final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
271         if (ovsdbNodeRef != null && ovsdbNodeRef.getValue() != null) {
272             final UUID bridgeUuid = UUID.randomUUID();
273             final OvsdbBridgeAugmentation ovsdbBridge = new OvsdbBridgeAugmentationBuilder()
274                                                         .setBridgeName(
275                                                                 new OvsdbBridgeName(UnimgrConstants.DEFAULT_BRIDGE_NAME))
276                                                         .setManagedBy(ovsdbNodeRef)
277                                                         .setBridgeUuid(
278                                                                 new Uuid(bridgeUuid.toString()))
279                                                         .build();
280             return ovsdbBridge;
281         } else {
282             throw new Exception("Ovsdb Node Reference does not exist !");
283         }
284     }
285
286     /**
287      * Creates a submit an OvsdbNode to the Configuration DataStore.
288      * @param dataBroker The instance of the Data Broker to create transactions.
289      * @param ovsdbNodeId The Ovsdb Node Id to use on creation
290      * @param uni The UNI's data
291      */
292     public static void createOvsdbNode(DataBroker dataBroker,
293                                        NodeId ovsdbNodeId,
294                                        Uni uni) {
295         final InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(uni.getIpAddress());
296         try {
297             final NodeKey ovsdbNodeKey = new NodeKey(ovsdbNodeId);
298             final Node nodeData = new NodeBuilder()
299                                     .setNodeId(ovsdbNodeId)
300                                     .setKey(ovsdbNodeKey)
301                                     .addAugmentation(OvsdbNodeAugmentation.class,
302                                                      UnimgrUtils.createOvsdbNodeAugmentation(uni))
303                                     .build();
304             // Submit the node to the datastore
305             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
306             transaction.put(LogicalDatastoreType.CONFIGURATION, ovsdbNodeIid, nodeData);
307             transaction.submit();
308             LOG.info("Created and submitted a new OVSDB node {}", nodeData.getNodeId());
309         } catch (final Exception e) {
310             LOG.error("Exception while creating OvsdbNodeAugmentation, " + "Uni is null. Node Id: {}", ovsdbNodeId);
311         }
312     }
313
314     /**
315      * Creates and submit an OvsdbNode by using the Data contained in the UniAugmentation
316      * @param dataBroker The instance of the DataBroker to create transactions
317      * @param uni The UNI's data
318      * @return The instance of the Node
319      */
320     public static Node createOvsdbNode(DataBroker dataBroker,
321                                        UniAugmentation uni) {
322         final NodeId ovsdbNodeId = new NodeId(createOvsdbNodeId(uni.getIpAddress()));
323         try {
324             final InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
325             final NodeKey ovsdbNodeKey = new NodeKey(ovsdbNodeId);
326             final Node nodeData = new NodeBuilder()
327                                     .setNodeId(ovsdbNodeId)
328                                     .setKey(ovsdbNodeKey)
329                                     .addAugmentation(OvsdbNodeAugmentation.class,
330                                                      UnimgrUtils.createOvsdbNodeAugmentation(uni))
331                                     .build();
332             // Submit the node to the datastore
333             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
334             transaction.put(LogicalDatastoreType.CONFIGURATION, ovsdbNodeIid, nodeData);
335             transaction.submit();
336             LOG.info("Created and submitted a new OVSDB node {}", nodeData.getNodeId());
337             return nodeData;
338         } catch (final Exception e) {
339             LOG.error("Exception while creating OvsdbNodeAugmentation, " + "Uni is null. Node Id: {}", ovsdbNodeId);
340         }
341         return null;
342     }
343
344     /**
345      * Creates and Build the data for an OvsdbNodeAugmentation.
346      * @param uni The UNI"s data
347      * @return The built OsvdbNodeAugmentation
348      */
349     public static OvsdbNodeAugmentation createOvsdbNodeAugmentation(Uni uni) {
350         final ConnectionInfo connectionInfos = new ConnectionInfoBuilder()
351                                                 .setRemoteIp(uni.getIpAddress())
352                                                 .setRemotePort(new PortNumber(UnimgrConstants.OVSDB_PORT))
353                                                 .build();
354         final OvsdbNodeAugmentation ovsdbNode = new OvsdbNodeAugmentationBuilder()
355                                                 .setConnectionInfo(connectionInfos).build();
356         return ovsdbNode;
357     }
358
359     public static OvsdbNodeAugmentation createOvsdbNodeAugmentation(UniAugmentation uni,
360             PortNumber remotePort) {
361         final ConnectionInfo connectionInfos = new ConnectionInfoBuilder()
362                 .setRemoteIp(uni.getIpAddress())
363                 .setRemotePort(remotePort)
364                 .build();
365         final OvsdbNodeAugmentation ovsdbNode = new OvsdbNodeAugmentationBuilder()
366                 .setConnectionInfo(connectionInfos)
367                 .setQosEntries(createQosEntries(uni))
368                 .setQueues(createQueues(uni))
369                 .build();
370         return ovsdbNode;
371     }
372
373     public static Node createQoSForOvsdbNode (DataBroker dataBroker, UniAugmentation uni) {
374         final Optional<Node> optionalNode = findOvsdbNode(dataBroker, uni);
375         if (optionalNode.isPresent()) {
376             final NodeId ovsdbNodeId = optionalNode.get().getNodeId();
377             final InstanceIdentifier<OvsdbNodeAugmentation> ovsdbNodeAugmentationIid = UnimgrMapper
378                     .getOvsdbNodeIid(ovsdbNodeId)
379                     .augmentation(OvsdbNodeAugmentation.class);
380             final OvsdbNodeAugmentation ovsdbNodeAugmentation = createOvsdbNodeAugmentation(uni,
381                     getRemotePort(dataBroker, uni));
382             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
383             transaction.put(LogicalDatastoreType.CONFIGURATION, ovsdbNodeAugmentationIid, ovsdbNodeAugmentation, true);
384             final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
385             try {
386                 Thread.sleep(UnimgrConstants.OVSDB_UPDATE_TIMEOUT);
387             } catch (final InterruptedException e) {
388                 LOG.warn("Interrupted while waiting after OVSDB node augmentation {} {}", ovsdbNodeId, e);
389             }
390             try {
391                 future.checkedGet();
392                 LOG.trace("Update qos and queues to ovsdb for node {} {}", ovsdbNodeId, ovsdbNodeAugmentationIid);
393             } catch (final TransactionCommitFailedException e) {
394                 LOG.warn("Failed to put {} ", ovsdbNodeAugmentationIid, e);
395             }
396             updateQosEntries(dataBroker, uni);
397         }
398         return null;
399     }
400
401     private static PortNumber getRemotePort(DataBroker dataBroker, UniAugmentation uni) {
402         PortNumber remotePort = null;
403         final Optional<Node> optionalNode = findOvsdbNode(dataBroker, uni);
404
405         if (optionalNode.isPresent()) {
406             remotePort = optionalNode.get()
407                     .getAugmentation(OvsdbNodeAugmentation.class)
408                     .getConnectionInfo().getRemotePort();
409         }
410         return remotePort;
411     }
412
413     private static List<QosEntries> createQosEntries(Uni uni) {
414         // Configure queue for best-effort dscp and max rate
415         final List<QosOtherConfig> otherConfig = new ArrayList<>();
416         QosOtherConfig qOtherConfig = new QosOtherConfigBuilder()
417                 .setKey(new QosOtherConfigKey(UnimgrConstants.QOS_DSCP_ATTRIBUTE))
418                 .setOtherConfigKey(UnimgrConstants.QOS_DSCP_ATTRIBUTE)
419                 .setOtherConfigValue(UnimgrConstants.QOS_DSCP_ATTRIBUTE_VALUE)
420                 .build();
421         otherConfig.add(qOtherConfig);
422
423         qOtherConfig = new QosOtherConfigBuilder()
424                 .setKey(new QosOtherConfigKey(UnimgrConstants.QOS_MAX_RATE))
425                 .setOtherConfigKey(UnimgrConstants.QOS_MAX_RATE)
426                 .setOtherConfigValue(getSpeed(uni.getSpeed().getSpeed()))
427                 .build();
428         otherConfig.add(qOtherConfig);
429
430         final Uuid qosUuid = new Uuid(UUID.randomUUID().toString());
431         final QosEntries qosEntry = new QosEntriesBuilder()
432                 .setKey(new QosEntriesKey(new Uri(UnimgrConstants.QOS_PREFIX + qosUuid.getValue())))
433                 .setQosId(new Uri(UnimgrConstants.QOS_PREFIX + qosUuid.getValue()))
434                 .setQosOtherConfig(otherConfig)
435                 .setQosType(SouthboundMapper.createQosType(SouthboundConstants.QOS_LINUX_HTB))
436                 .build();
437
438         final List<QosEntries> qosEntries = new ArrayList<>();
439         qosEntries.add(qosEntry);
440         return qosEntries;
441     }
442
443     private static List<Queues> createQueues(Uni uni) {
444         final List<QueuesOtherConfig> otherConfig = new ArrayList<>();
445         QueuesOtherConfig queuesOtherConfig = new QueuesOtherConfigBuilder()
446                 .setKey(new QueuesOtherConfigKey(UnimgrConstants.QOS_DSCP_ATTRIBUTE))
447                 .setQueueOtherConfigKey(UnimgrConstants.QOS_DSCP_ATTRIBUTE)
448                 .setQueueOtherConfigValue(UnimgrConstants.QOS_DSCP_ATTRIBUTE_VALUE)
449                 .build();
450         otherConfig.add(queuesOtherConfig);
451
452         queuesOtherConfig = new QueuesOtherConfigBuilder()
453                 .setKey(new QueuesOtherConfigKey(UnimgrConstants.QOS_MAX_RATE))
454                 .setQueueOtherConfigKey(UnimgrConstants.QOS_MAX_RATE)
455                 .setQueueOtherConfigValue(getSpeed(uni.getSpeed().getSpeed()))
456                 .build();
457         otherConfig.add(queuesOtherConfig);
458
459         // Configure dscp value for best-effort
460         final Uuid queueUuid = new Uuid(UUID.randomUUID().toString());
461         final Queues queues = new QueuesBuilder()
462                 .setDscp(Short.parseShort(UnimgrConstants.QOS_DSCP_ATTRIBUTE_VALUE))
463                 .setKey(new QueuesKey(new Uri(UnimgrConstants.QUEUE_PREFIX + queueUuid.getValue())))
464                 .setQueueId(new Uri(UnimgrConstants.QUEUE_PREFIX + queueUuid.getValue()))
465                 .setQueuesOtherConfig(otherConfig)
466                 .build();
467
468         final List<Queues> queuesList = new ArrayList<>();
469         queuesList.add(queues);
470         return queuesList;
471     }
472
473     private static String getSpeed(Speed speedObject) {
474         String speed = null;
475         if (speedObject instanceof Speed10M) {
476             // map to 1MB
477             speed = "1000000";
478         }
479         else if (speedObject instanceof Speed100M) {
480             // map to 2MB
481             speed = "2000000";
482         }
483         else if (speedObject instanceof Speed1G) {
484             // map to 3MB
485             speed = "3000000";
486         }
487         else if (speedObject instanceof Speed10G) {
488             // map to 4MB
489             speed = "4000000";
490         }
491         return speed;
492     }
493
494     private static void updateQosEntries(DataBroker dataBroker, UniAugmentation uni) {
495         final Optional<Node> optionalNode = findOvsdbNode(dataBroker, uni);
496         if (optionalNode.isPresent()) {
497             final NodeId ovsdbNodeId = optionalNode.get().getNodeId();
498             final Long queueNumber = 0L;
499             final List<QosEntries> qosList = optionalNode.get()
500                     .getAugmentation(OvsdbNodeAugmentation.class)
501                     .getQosEntries();
502             LOG.trace("QOS entries list {} for node {}", qosList, ovsdbNodeId);
503             QosEntriesKey qosEntryKey = null;
504             for (final QosEntries qosEntry : qosList) {
505                 qosEntryKey = qosEntry.getKey();
506             }
507             final InstanceIdentifier<QueueList> queueIid = UnimgrMapper
508                     .getOvsdbQueueListIid(ovsdbNodeId, qosEntryKey, queueNumber);
509
510             Uuid queueUuid = null;
511             final List<Queues> queuesList = optionalNode.get()
512                     .getAugmentation(OvsdbNodeAugmentation.class).getQueues();
513             for (final Queues queue : queuesList) {
514                 queueUuid = queue.getQueueUuid();
515             }
516             final QueueList queueList = new QueueListBuilder()
517                     .setKey(new QueueListKey(queueNumber))
518                     .setQueueNumber(queueNumber)
519                     .setQueueUuid(queueUuid)
520                     .build();
521
522             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
523             transaction.put(LogicalDatastoreType.CONFIGURATION, queueIid, queueList, true);
524             final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
525             try {
526                 future.checkedGet();
527                 LOG.info("Update qos-entries to ovsdb for node {} {}", ovsdbNodeId, queueIid);
528             } catch (final TransactionCommitFailedException e) {
529                 LOG.warn("Failed to put {} ", queueIid, e);
530             }
531         }
532     }
533
534     public static void updateMaxRate (DataBroker dataBroker,
535             UniAugmentation sourceUniAugmentation,
536             UniAugmentation destinationUniAugmentation,
537             EvcAugmentation evc) {
538         Optional<Node> optionalNode;
539         if (getSpeed(sourceUniAugmentation.getSpeed().getSpeed()).equals(getSpeed(evc.getIngressBw().getSpeed()))) {
540             LOG.info("Source UNI speed matches EVC ingress BW");
541         } else {
542             // update Uni's ovsdbNodeRef qos-entries and queues for max-rate to match EVC ingress BW
543             optionalNode = findOvsdbNode(dataBroker, sourceUniAugmentation);
544             if (optionalNode.isPresent()) {
545                 updateQosMaxRate(dataBroker, optionalNode, evc);
546                 updateQueuesMaxRate(dataBroker, optionalNode, evc);
547             }
548         }
549
550         if (getSpeed(destinationUniAugmentation.getSpeed().getSpeed()).equals(getSpeed(evc.getIngressBw().getSpeed()))) {
551             LOG.info("Destination UNI speed matches EVC ingress BW");
552         } else {
553             // update Uni's ovsdbNodeRef qos-entries and queues for max-rate to match EVC ingress BW
554             optionalNode = findOvsdbNode(dataBroker, destinationUniAugmentation);
555             if (optionalNode.isPresent()) {
556                 updateQosMaxRate(dataBroker, optionalNode, evc);
557                 updateQueuesMaxRate(dataBroker, optionalNode, evc);
558             }
559         }
560     }
561
562     private static void updateQosMaxRate(DataBroker dataBroker,
563             Optional<Node> optionalOvsdbNode,
564             EvcAugmentation evc) {
565         final NodeId ovsdbNodeId = optionalOvsdbNode.get().getNodeId();
566         final List<QosEntries> qosList = optionalOvsdbNode.get()
567                 .getAugmentation(OvsdbNodeAugmentation.class)
568                 .getQosEntries();
569         LOG.trace("QOS entries list {} for node {}", qosList, ovsdbNodeId);
570         QosEntriesKey qosEntryKey = null;
571         for (final QosEntries qosEntry : qosList) {
572             qosEntryKey = qosEntry.getKey();
573         }
574         final InstanceIdentifier<QosOtherConfig> qosOtherConfigIid = UnimgrMapper
575                 .getQosOtherConfigIid(ovsdbNodeId, qosEntryKey);
576         final QosOtherConfig qOtherConfig = new QosOtherConfigBuilder()
577                 .setKey(new QosOtherConfigKey(UnimgrConstants.QOS_MAX_RATE))
578                 .setOtherConfigKey(UnimgrConstants.QOS_MAX_RATE)
579                 .setOtherConfigValue(getSpeed(evc.getIngressBw().getSpeed()))
580                 .build();
581         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
582         transaction.put(LogicalDatastoreType.CONFIGURATION, qosOtherConfigIid, qOtherConfig, true);
583         final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
584         try {
585             future.checkedGet();
586             LOG.info("Update qos-entries max-rate to ovsdb for node {} {}", ovsdbNodeId, qosOtherConfigIid);;
587         } catch (final TransactionCommitFailedException e) {
588             LOG.warn("Failed to put {} ", qosOtherConfigIid, e);
589         }
590     }
591
592     private static void updateQueuesMaxRate(DataBroker dataBroker,
593             Optional<Node> optionalOvsdbNode,
594             EvcAugmentation evc) {
595         final NodeId ovsdbNodeId = optionalOvsdbNode.get().getNodeId();
596         final List<Queues> queues = optionalOvsdbNode.get()
597                 .getAugmentation(OvsdbNodeAugmentation.class)
598                 .getQueues();
599         QueuesKey queuesKey = null;
600         for (final Queues queue: queues) {
601             queuesKey = queue.getKey();
602         }
603         final InstanceIdentifier<QueuesOtherConfig> queuesOtherConfigIid = UnimgrMapper
604                 .getQueuesOtherConfigIid(ovsdbNodeId, queuesKey);
605         final QueuesOtherConfig queuesOtherConfig = new QueuesOtherConfigBuilder()
606                 .setKey(new QueuesOtherConfigKey(UnimgrConstants.QOS_MAX_RATE))
607                 .setQueueOtherConfigKey(UnimgrConstants.QOS_MAX_RATE)
608                 .setQueueOtherConfigValue(getSpeed(evc.getIngressBw().getSpeed()))
609                 .build();
610         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
611         transaction.put(LogicalDatastoreType.CONFIGURATION, queuesOtherConfigIid, queuesOtherConfig, true);
612         final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
613         try {
614             future.checkedGet();
615             LOG.info("Update queues max-rate to ovsdb for node {} {}", ovsdbNodeId, queuesOtherConfigIid);;
616         } catch (final TransactionCommitFailedException e) {
617             LOG.warn("Failed to put {} ", queuesOtherConfigIid, e);
618         }
619     }
620
621     /**
622      * Creates an OVSDB node Id with an IP Address.
623      * @param ipAddress The IP address of the UNI (therefo the OVSDB node)
624      * @return A NodeId for a Specific Ovsdb Node Id
625      */
626     public static NodeId createOvsdbNodeId(IpAddress ipAddress) {
627         final String nodeId = UnimgrConstants.OVSDB_PREFIX
628                         + ipAddress.getIpv4Address().getValue().toString()
629                         + ":"
630                         + UnimgrConstants.OVSDB_PORT;
631         return new NodeId(nodeId);
632     }
633
634     /**
635      * Creates a built OvsdbTerminationAugmentation with data
636      * @param uni The UNI's data
637      * @return A Built OvsdbTerminationPointAugmentation with data
638      */
639     public static OvsdbTerminationPointAugmentation createOvsdbTerminationPointAugmentation(Uni uni) {
640         // we will use nodeId to set interface port id
641         final VlanId vlanID = new VlanId(1);
642         final OvsdbTerminationPointAugmentation terminationPoint = new OvsdbTerminationPointAugmentationBuilder()
643                                                                      .setName(UnimgrConstants.DEFAULT_INTERNAL_IFACE)
644                                                                      .setVlanTag(vlanID)
645                                                                      .setVlanMode(VlanMode.Access)
646                                                                      .build();
647         return terminationPoint;
648     }
649
650     /**
651      * Creates and submit an evc by using the Data contained in the EvcAugmentation
652      * @param dataBroker The instance of the DataBroker to create transactions
653      * @param evc The EVC's data
654      * @return true if evc created
655      */
656     public static boolean createEvc(DataBroker dataBroker, EvcAugmentation evc) {
657          return false;
658     }
659
660     /**
661      * Creates and submit an UNI Node by using the Data contained in the UniAugmentation
662      * @param dataBroker The instance of the DataBroker to create transactions
663      * @param uni The UNI's data
664      * @return true if uni created
665      */
666     public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
667         final NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
668         boolean result = false;
669         try {
670             final InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
671             final NodeKey uniNodeKey = new NodeKey(uniNodeId);
672             final Node nodeData = new NodeBuilder()
673                                     .setNodeId(uniNodeId)
674                                     .setKey(uniNodeKey)
675                                     .addAugmentation(UniAugmentation.class, uni)
676                                     .build();
677             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
678             transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
679             final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
680             future.checkedGet();
681             result = true;
682             LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
683         } catch (final Exception e) {
684             LOG.error("Exception while creating Uni Node" + "Uni Node Id: {}", uniNodeId);
685         }
686         return result;
687     }
688
689     /**
690      * Creates an UNI node Id with an IP Address.
691      * @param ipAddress The IP address of the UNI
692      * @return A NodeId for a Specific UNI Node Id
693      */
694     public static NodeId createUniNodeId(IpAddress ipAddress) {
695         return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
696     }
697
698     /**
699      * Creates and Submit a termination point Node to the configuration DateStore.
700      * @param dataBroker The instance of the data broker to create transactions
701      * @param uni The UNI's data
702      * @param bridgeNode The Bridge node
703      * @param bridgeName The Bridge name (example: br0)
704      * @param portName The Port name (example: eth0)
705      * @param type The type of termination (example: gre) Refer to OVSDB_INTERFACE_TYPE_MAP
706      * to review the list of available Interface Types.
707      */
708     public static void createTerminationPointNode(DataBroker dataBroker,
709                                                   Uni uni,
710                                                   Node bridgeNode,
711                                                   String bridgeName,
712                                                   String portName,
713                                                   String type) {
714         final InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
715                                                         .getTerminationPointIid(bridgeNode,
716                                                                                 portName);
717         final OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
718                                                      new OvsdbTerminationPointAugmentationBuilder();
719         tpAugmentationBuilder.setName(portName);
720         if (type != null) {
721             tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type));
722         }
723         final TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
724         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
725         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
726                                   tpAugmentationBuilder.build());
727         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
728         transaction.put(LogicalDatastoreType.CONFIGURATION,
729                         tpIid,
730                         tpBuilder.build());
731         transaction.submit();
732     }
733
734     /**
735      * Creates and Submit a termination point Node without specifying its interface type.
736      * @param dataBroker The instance of the data broker to create transactions
737      * @param uni The UNI's data
738      * @param bridgeNode The Bridge node
739      * @param bridgeName The Bridge name (example: br0)
740      * @param portName The Port name (example: eth0)
741      */
742     public static void createTerminationPointNode(DataBroker dataBroker,
743                                                   Uni uni,
744                                                   Node bridgeNode,
745                                                   String bridgeName,
746                                                   String portName) {
747         final InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
748                                                         .getTerminationPointIid(bridgeNode,
749                                                                                 portName);
750         final OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
751                                                      new OvsdbTerminationPointAugmentationBuilder();
752         tpAugmentationBuilder.setName(portName);
753         tpAugmentationBuilder.setInterfaceType(null);
754         if (uni.getSpeed() != null) {
755             final Uuid qosUuid = getQosUuid(dataBroker, uni);
756             tpAugmentationBuilder.setQos(getQosUuid(dataBroker, uni));
757             LOG.info("Updating Qos {} to termination point {}", qosUuid , bridgeName);
758         }
759         final TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
760         tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
761         tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
762                                   tpAugmentationBuilder.build());
763         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
764         transaction.put(LogicalDatastoreType.CONFIGURATION,
765                         tpIid,
766                         tpBuilder.build());
767         transaction.submit();
768     }
769
770     private static Uuid getQosUuid(DataBroker dataBroker, Uni uni) {
771         Uuid qosUuid = null;
772         final Optional<Node> optionalNode = findUniNode(dataBroker, uni.getIpAddress());
773
774         if (optionalNode.isPresent()) {
775             final UniAugmentation uniAugmentation = optionalNode.get()
776                     .getAugmentation(UniAugmentation.class);
777             final Optional<Node> ovsdbNode = findOvsdbNode(dataBroker, uniAugmentation);
778             if (ovsdbNode.isPresent()) {
779                 final List<QosEntries> qosEntries = ovsdbNode.get()
780                         .getAugmentation(OvsdbNodeAugmentation.class)
781                         .getQosEntries();
782                 for (final QosEntries qosEntry : qosEntries) {
783                     qosUuid = qosEntry.getQosUuid();
784                 }
785             }
786         }
787         return qosUuid;
788     }
789
790     /**
791      * Deletes a generic node
792      * @param dataBroker The instance of the data broker to create transactions
793      * @param store The DataStore where the delete
794      * @param path The path to delete
795      * @return An instance of a generic Data Object
796      */
797     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
798             DataBroker dataBroker,
799             final LogicalDatastoreType store,
800             final InstanceIdentifier<D> path)  {
801         boolean result = false;
802         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
803         transaction.delete(store, path);
804         final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
805         try {
806             future.checkedGet();
807             result = true;
808         } catch (final TransactionCommitFailedException e) {
809             LOG.warn("Failed to delete {} ", path, e);
810         }
811         return result;
812     }
813
814     public static void deleteEvcData(DataBroker dataBroker, Optional<Node> optionalUni) {
815         if (optionalUni.isPresent()) {
816             final UniAugmentation uniAugmentation =
817                                 optionalUni
818                                     .get()
819                                     .getAugmentation(UniAugmentation.class);
820             final InstanceIdentifier<Node> ovsdbNodeIid =
821                                               uniAugmentation
822                                              .getOvsdbNodeRef()
823                                              .getValue()
824                                              .firstIdentifierOf(Node.class);
825             final Optional<Node> optionalOvsdNode =
826                     UnimgrUtils.readNode(dataBroker,
827                                          LogicalDatastoreType.OPERATIONAL,
828                                          ovsdbNodeIid);
829             if (optionalOvsdNode.isPresent()) {
830                 final Node ovsdbNode = optionalOvsdNode.get();
831                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
832                 for (final ManagedNodeEntry managedNodeEntry: ovsdbNodeAugmentation.getManagedNodeEntry()) {
833                     final InstanceIdentifier<Node> bridgeIid = managedNodeEntry
834                                                              .getBridgeRef()
835                                                              .getValue()
836                                                              .firstIdentifierOf(Node.class);
837                     final Optional<Node> optBridgeNode = UnimgrUtils.readNode(dataBroker, bridgeIid);
838                     if (optBridgeNode.isPresent()) {
839                         final Node bridgeNode = optBridgeNode.get();
840                         final InstanceIdentifier<TerminationPoint> iidGreTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
841                                                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
842                         final InstanceIdentifier<TerminationPoint> iidEthTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
843                                                                                         UnimgrConstants.DEFAULT_TUNNEL_IFACE);
844                         UnimgrUtils.deleteNode(dataBroker, iidGreTermPoint, LogicalDatastoreType.CONFIGURATION);
845                         UnimgrUtils.deleteNode(dataBroker, iidEthTermPoint, LogicalDatastoreType.CONFIGURATION);
846                     }
847                 }
848             }
849         } else {
850             LOG.info("Unable to retrieve UNI from the EVC.");
851         }
852     }
853
854     /**
855      * Deletes a termination Point from the configuration data store.
856      * @param dataBroker The instance of the data broker to create transactions
857      * @param terminationPoint The Termination Point of the OVSDB bridge
858      * @param ovsdbNode The ovsdb Node
859      * @return A checked Future
860      */
861     public static CheckedFuture<Void,
862                                 TransactionCommitFailedException>
863                                 deleteTerminationPoint(DataBroker dataBroker,
864                                                        TerminationPoint terminationPoint,
865                                                        Node ovsdbNode) {
866         final InstanceIdentifier<TerminationPoint> terminationPointPath =
867                                                  InstanceIdentifier
868                                                      .create(NetworkTopology.class)
869                                                      .child(Topology.class,
870                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
871                                                      .child(Node.class,
872                                                             ovsdbNode.getKey())
873                                                      .child(TerminationPoint.class,
874                                                             terminationPoint.getKey());
875         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
876         transaction.delete(LogicalDatastoreType.CONFIGURATION, terminationPointPath);
877         transaction.delete(LogicalDatastoreType.OPERATIONAL, terminationPointPath);
878         transaction.submit();
879         final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
880         return future;
881     }
882
883     /**
884      * Generic function to delete a node on a specific dataStore
885      * @param dataBroker The instance of the data broker to create transactions.
886      * @param genericNode The instance identifier of a generic node
887      * @param store The dataStore where to send and submit the delete call.
888      */
889     public static boolean deleteNode(DataBroker dataBroker,
890                                   InstanceIdentifier<?> genericNode,
891                                   LogicalDatastoreType store) {
892         LOG.info("Received a request to delete node {}", genericNode);
893         boolean result = false;
894         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
895         transaction.delete(store, genericNode);
896         try {
897             transaction.submit().checkedGet();
898             result = true;
899         } catch (final TransactionCommitFailedException e) {
900             LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
901         }
902         return result;
903     }
904
905     /**
906      * Extract a data object by using its instance indentifier and it's class type.
907      * @param changes Data Change object
908      * @param klazz Class type
909      * @return The extracted DataObject as an Object casted as the class type
910      */
911     public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extract(
912             Map<InstanceIdentifier<?>, DataObject> changes, Class<T> klazz) {
913         final Map<InstanceIdentifier<T>,T> result = new HashMap<InstanceIdentifier<T>,T>();
914         if (changes != null && changes.entrySet() != null) {
915             for (final Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
916                 if (klazz.isInstance(created.getValue())) {
917                     @SuppressWarnings("unchecked")
918                     final
919                     T value = (T) created.getValue();
920                     final Class<?> type = created.getKey().getTargetType();
921                     if (type.equals(klazz)) {
922                         @SuppressWarnings("unchecked") // Actually checked above
923                         final
924                         InstanceIdentifier<T> iid = (InstanceIdentifier<T>) created.getKey();
925                         result.put(iid, value);
926                     }
927                 }
928             }
929         }
930         return result;
931     }
932
933     /**
934      * Extract original data from the data store.
935      * @param changes The dataChange object
936      * @param klazz The class type
937      * @return The DataObject casted as a Class type
938      */
939     public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractOriginal(
940             AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
941         return extract(changes.getOriginalData(),klazz);
942     }
943
944     /**
945      * Extracts the removed nodes
946      * @param changes he dataChange object
947      * @param klazz The class type
948      * @return A set to removed nodes as DataObject casted as the class type
949      */
950     public static <T extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
951             AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
952         final Set<InstanceIdentifier<T>> result = new HashSet<InstanceIdentifier<T>>();
953         if (changes != null && changes.getRemovedPaths() != null) {
954             for (final InstanceIdentifier<?> iid : changes.getRemovedPaths()) {
955                 if (iid.getTargetType().equals(klazz)) {
956                     @SuppressWarnings("unchecked") // Actually checked above
957                     final
958                     InstanceIdentifier<T> iidn = (InstanceIdentifier<T>)iid;
959                     result.add(iidn);
960                 }
961             }
962         }
963         return result;
964     }
965
966     /**
967      * Search the Operational Datastore for a specific OvsdbNode.
968      * @param dataBroker The dataBroker instance to create transactions
969      * @param uni The UNI's data
970      * @return The Optional OvsdbNode
971      */
972     public static Optional<Node> findOvsdbNode(DataBroker dataBroker,
973                                                UniAugmentation uni) {
974         final List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
975         Optional<Node> optionalOvsdb;
976         if (!ovsdbNodes.isEmpty()) {
977             for (final Node ovsdbNode : ovsdbNodes) {
978                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
979                                                                   .getAugmentation(OvsdbNodeAugmentation.class);
980                 if (ovsdbNodeAugmentation.getConnectionInfo()
981                                          .getRemoteIp()
982                                          .getIpv4Address()
983                         .equals(uni.getIpAddress().getIpv4Address())) {
984                     LOG.info("Found ovsdb node");
985                     optionalOvsdb = Optional.of(ovsdbNode);
986                     return optionalOvsdb;
987                 }
988             }
989         }
990         return Optional.absent();
991     }
992
993     /**
994      * Search the Operation DataStore for a specific UNI
995      * @param dataBroker The dataBroker instance to create transactions
996      * @param ipAddress The IP address of the UNI
997      * @return An Optional UNI Node
998      */
999     public static Optional<Node> findUniNode(DataBroker dataBroker,
1000                                              IpAddress ipAddress) {
1001         final List<Node> uniNodes = getUniNodes(dataBroker);
1002         if (!uniNodes.isEmpty()) {
1003             for (final Node uniNode : uniNodes) {
1004                 final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
1005                 if (uniAugmentation.getIpAddress().equals(ipAddress)) {
1006                     LOG.info("Found Uni node");
1007                     return Optional.of(uniNode);
1008                 }
1009             }
1010         }
1011         return Optional.absent();
1012     }
1013
1014     /**
1015      * Retrieves the connection information from an Ovsdb Connection by
1016      * using the Ovsdb Node Id
1017      * @param dataBroker The dataBroker instance to create transactions
1018      * @param ovsdbNodeId The NodeId of the OVSDB node
1019      * @return The ConnectionInfo object
1020      */
1021     public static ConnectionInfo getConnectionInfo(DataBroker dataBroker,
1022                                                    NodeId ovsdbNodeId) {
1023         final InstanceIdentifier<Node> nodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
1024         final Optional<Node> node = readNode(dataBroker,
1025                                        LogicalDatastoreType.OPERATIONAL,
1026                                        nodeIid);
1027         if (node.isPresent()) {
1028             final Node ovsdbNode = node.get();
1029             final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
1030                                                               .getAugmentation(OvsdbNodeAugmentation.class);
1031             final ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
1032             return connectionInfo;
1033         } else {
1034             return null;
1035         }
1036     }
1037
1038     /**
1039      * Retrieve the list of links in the Operational DataStore
1040      * @param dataBroker The dataBroker instance to create transactions
1041      * @return A list of Links retrieved from the Operational DataStore
1042      */
1043     public static List<Link> getEvcLinks(DataBroker dataBroker) {
1044         final List<Link> evcLinks = new ArrayList<>();
1045         final InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
1046         final Topology topology = UnimgrUtils.read(dataBroker,
1047                                              LogicalDatastoreType.OPERATIONAL,
1048                                              evcTopology);
1049         if (topology != null && topology.getLink() != null) {
1050             for (final Link link : topology.getLink()) {
1051                 final EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
1052                 if (evcAugmentation != null) {
1053                     evcLinks.add(link);
1054                 }
1055             }
1056         }
1057         return evcLinks;
1058     }
1059
1060     /**
1061      * Retrieve the Local IP of the controller
1062      * @return The LocalIp object of the Controller
1063      */
1064     public static IpAddress getLocalIp() {
1065         String ip;
1066         try {
1067             ip = InetAddress.getLocalHost().getHostAddress();
1068             final Ipv4Address ipv4 = new Ipv4Address(ip);
1069             final IpAddress ipAddress = new IpAddress(ipv4);
1070             return ipAddress;
1071         } catch (final UnknownHostException e) {
1072             LOG.info("Unable to retrieve controller's ip address, using loopback.");
1073         }
1074         return new IpAddress(UnimgrConstants.LOCAL_IP);
1075     }
1076
1077     /**
1078      * Retrieve a list of Ovsdb Nodes from the Operational DataStore
1079      * @param dataBroker The dataBroker instance to create transactions
1080      * @return The Ovsdb Node retrieved from the Operational DataStore
1081      */
1082     public static List<Node> getOvsdbNodes(DataBroker dataBroker) {
1083         final List<Node> ovsdbNodes = new ArrayList<>();
1084         final InstanceIdentifier<Topology> ovsdbTopoIdentifier = UnimgrMapper.getOvsdbTopologyIid();
1085         final Topology topology = UnimgrUtils.read(dataBroker,
1086                                              LogicalDatastoreType.OPERATIONAL,
1087                                              ovsdbTopoIdentifier);
1088         if (topology != null && topology.getNode() != null) {
1089             for (final Node node : topology.getNode()) {
1090                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
1091                 if (ovsdbNodeAugmentation != null) {
1092                     ovsdbNodes.add(node);
1093                 }
1094             }
1095         }
1096         return ovsdbNodes;
1097     }
1098
1099     /**
1100      * Retrieve a list of Uni Nodes from the Configuration DataStore
1101      * @param dataBroker The dataBroker instance to create transactions
1102      * @return A list of Uni Nodes from the Config dataStore
1103      */
1104     public static List<Node> getUniNodes(DataBroker dataBroker) {
1105         final List<Node> uniNodes = new ArrayList<>();
1106         final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
1107         final Topology topology = read(dataBroker,
1108                                  LogicalDatastoreType.CONFIGURATION,
1109                                  topologyInstanceIdentifier);
1110         if (topology != null && topology.getNode() != null) {
1111             for (final Node node : topology.getNode()) {
1112                 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
1113                 if (uniAugmentation != null) {
1114                     uniNodes.add(node);
1115                 }
1116             }
1117         }
1118         return uniNodes;
1119     }
1120
1121     /**
1122      * Retrieve a list of Uni Nodes on a specific DataStore
1123      * @param dataBroker The dataBroker instance to create transactions
1124      * @param store The store to which to send the read request
1125      * @return A List of UNI Nodes.
1126      */
1127     public static List<Node> getUniNodes(DataBroker dataBroker,
1128                                          LogicalDatastoreType store) {
1129         final List<Node> uniNodes = new ArrayList<>();
1130         final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
1131         final Topology topology = read(dataBroker,
1132                                  store,
1133                                  topologyInstanceIdentifier);
1134         if (topology != null && topology.getNode() != null) {
1135             for (final Node node : topology.getNode()) {
1136                 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
1137                 if (uniAugmentation != null) {
1138                     uniNodes.add(node);
1139                 }
1140             }
1141         }
1142         return uniNodes;
1143     }
1144
1145     /**
1146      * Retrieve a list of Unis on a specific DataStore
1147      * @param dataBroker instance to create transactions
1148      * @param store to which send the read request
1149      * @return A List of Unis.
1150      */
1151     public static List<UniAugmentation> getUnis(DataBroker dataBroker,
1152                                          LogicalDatastoreType store) {
1153         final List<UniAugmentation> unis = new ArrayList<>();
1154         final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
1155         final Topology topology = read(dataBroker,
1156                                  store,
1157                                  topologyInstanceIdentifier);
1158         if (topology != null && topology.getNode() != null) {
1159             for (final Node node : topology.getNode()) {
1160                 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
1161                 if (uniAugmentation != null) {
1162                     unis.add(uniAugmentation);
1163                 }
1164             }
1165         }
1166         return unis;
1167     }
1168
1169     /**
1170      * Retrieve a list of Unis on a specific DataStore
1171      * @param dataBroker instance to create transactions
1172      * @param store to which send the read request
1173      * @param ipAddress of the required Uni
1174      * @return uni.
1175      */
1176     public static UniAugmentation getUni(DataBroker dataBroker,
1177                                          LogicalDatastoreType store, IpAddress ipAddress) {
1178         final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
1179         final Topology topology = read(dataBroker,
1180                                  store,
1181                                  topologyInstanceIdentifier);
1182         if (topology != null && topology.getNode() != null) {
1183             for (final Node node : topology.getNode()) {
1184                 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
1185                 if (uniAugmentation != null && uniAugmentation.getIpAddress().getIpv4Address().getValue().equals(ipAddress.getIpv4Address().getValue())) {
1186                     return uniAugmentation;
1187                 }
1188             }
1189         }
1190         return null;
1191     }
1192
1193     /**
1194      * Read a specific datastore type and return a DataObject as a casted
1195      * class type Object.
1196      * @param dataBroker The dataBroker instance to create transactions
1197      * @param store The store type to query
1198      * @param path The generic path to query
1199      * @return The DataObject as a casted Object
1200      */
1201     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
1202             DataBroker dataBroker,
1203             final LogicalDatastoreType store,
1204             final InstanceIdentifier<D> path)  {
1205         D result = null;
1206         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
1207         Optional<D> optionalDataObject;
1208         final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
1209         try {
1210             optionalDataObject = future.checkedGet();
1211             if (optionalDataObject.isPresent()) {
1212                 result = optionalDataObject.get();
1213             } else {
1214                 LOG.debug("{}: Failed to read {}",
1215                         Thread.currentThread().getStackTrace()[1], path);
1216             }
1217         } catch (final ReadFailedException e) {
1218             LOG.warn("Failed to read {} ", path, e);
1219         }
1220         transaction.close();
1221         return result;
1222     }
1223
1224     /**
1225      * Read a specific node from the Operational Data store by default.
1226      * @param dataBroker The dataBroker instance to create transactions
1227      * @param genericNode The Instance Identifier of the Node
1228      * @return The Optional Node instance
1229      */
1230     @Deprecated
1231     public static final Optional<Node> readNode(DataBroker dataBroker,
1232                                                 InstanceIdentifier<?> genericNode) {
1233         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
1234         final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
1235         final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
1236                                                               read.read(LogicalDatastoreType.OPERATIONAL,
1237                                                                         nodeIid);
1238         try {
1239             return nodeFuture.checkedGet();
1240         } catch (final ReadFailedException e) {
1241             LOG.info("Unable to read node with Iid {}", nodeIid);
1242         }
1243         return Optional.absent();
1244     }
1245
1246     /**
1247      * Read a specific Link from a specific datastore
1248      * @param dataBroker The dataBroker instance to create transactions
1249      * @param store The datastore type.
1250      * @param genericNode The Instance Identifier of the Link
1251      * @return An Optional Link instance
1252      */
1253     public static final Optional<Link> readLink(DataBroker dataBroker,
1254                                                 LogicalDatastoreType store,
1255                                                 InstanceIdentifier<?> genericNode) {
1256         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
1257         final InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
1258         final CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
1259         try {
1260             return linkFuture.checkedGet();
1261         } catch (final ReadFailedException e) {
1262             LOG.info("Unable to read node with Iid {}", linkIid);
1263         }
1264         return Optional.absent();
1265     }
1266
1267     /**
1268      * Read a specific node from a specific data store type.
1269      * @param dataBroker The dataBroker instance to create transactions
1270      * @param store The data store type
1271      * @param genericNode The Instance Identifier of a specific Node
1272      * @return An Optional Node instance
1273      */
1274     public static final Optional<Node> readNode(DataBroker dataBroker,
1275                                                 LogicalDatastoreType store,
1276                                                 InstanceIdentifier<?> genericNode) {
1277         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
1278         final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
1279         final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
1280                 .read(store, nodeIid);
1281         try {
1282             return nodeFuture.checkedGet();
1283         } catch (final ReadFailedException e) {
1284             LOG.info("Unable to read node with Iid {}", nodeIid);
1285         }
1286         return Optional.absent();
1287     }
1288
1289     /**
1290      * Updates a specific Uni Node on a specific DataStore type
1291      * @param dataStore The datastore type
1292      * @param uniIID The UNI InstanceIdentifier
1293      * @param uni The Uni's data
1294      * @param ovsdbNode The Ovsdb Node
1295      * @param dataBroker The dataBroker instance to create transactions
1296      * @return true if uni is updated
1297      */
1298     public static boolean updateUniNode(LogicalDatastoreType dataStore,
1299                                      InstanceIdentifier<?> uniIID,
1300                                      UniAugmentation uni,
1301                                      Node ovsdbNode,
1302                                      DataBroker dataBroker) {
1303         final InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
1304         final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
1305         final UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
1306         if (ovsdbNodeRef != null) {
1307             updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
1308         }
1309         final Optional<Node> optionalNode = readNode(dataBroker,
1310                                                LogicalDatastoreType.CONFIGURATION,
1311                                                uniIID);
1312         if (optionalNode.isPresent()) {
1313             final Node node = optionalNode.get();
1314             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
1315             final NodeBuilder nodeBuilder = new NodeBuilder();
1316             nodeBuilder.setKey(node.getKey());
1317             nodeBuilder.setNodeId(node.getNodeId());
1318             nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
1319             transaction.put(dataStore, uniIID.firstIdentifierOf(Node.class), nodeBuilder.build());
1320             transaction.submit();
1321             return true;
1322         }
1323         return false;
1324     }
1325
1326     /**
1327      * Update a specific UNI node on a specific datastore type
1328      * @param dataStore The datastore type
1329      * @param uniKey The UNI key
1330      * @param uni The Uni's data
1331      * @param ovsdbNodeIid The Ovsdb Node Instance Identifier
1332      * @param dataBroker The dataBroker instance to create transactions
1333      * @return true if uni is updated
1334      */
1335     public static boolean updateUniNode(LogicalDatastoreType dataStore,
1336                                      InstanceIdentifier<?> uniKey,
1337                                      UniAugmentation uni,
1338                                      InstanceIdentifier<?> ovsdbNodeIid,
1339                                      DataBroker dataBroker) {
1340         final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
1341         final UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
1342         if (ovsdbNodeRef != null) {
1343             updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
1344         }
1345         final Optional<Node> optionalNode = readNode(dataBroker,
1346                                                LogicalDatastoreType.CONFIGURATION,
1347                                                uniKey);
1348         if (optionalNode.isPresent()) {
1349             final Node node = optionalNode.get();
1350             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
1351             final NodeBuilder nodeBuilder = new NodeBuilder();
1352             nodeBuilder.setKey(node.getKey());
1353             nodeBuilder.setNodeId(node.getNodeId());
1354             nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
1355             transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
1356             transaction.submit();
1357             return true;
1358         }
1359         return false;
1360     }
1361
1362     /**
1363      * Updates a specific EVC into a specific DataStore type
1364      * @param dataStore The datastore type
1365      * @param evcKey The EVC key
1366      * @param evcAugmentation The EVC's data
1367      * @param sourceUniIid The Source Uni Instance Identifier
1368      * @param destinationUniIid The destination Uni Instance Identifier
1369      * @param dataBroker The dataBroker instance to create transactions
1370      * @return true if evc is updated
1371      */
1372     public static boolean updateEvcNode(LogicalDatastoreType dataStore,
1373                                      InstanceIdentifier<?> evcKey,
1374                                      EvcAugmentation evcAugmentation,
1375                                      InstanceIdentifier<?> sourceUniIid,
1376                                      InstanceIdentifier<?> destinationUniIid,
1377                                      DataBroker dataBroker) {
1378         final EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
1379         if (sourceUniIid != null && destinationUniIid != null) {
1380             final List<UniSource> sourceList = new ArrayList<UniSource>();
1381             final UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey();
1382             final short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder();
1383             final IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress();
1384             final UniSource uniSource = new UniSourceBuilder()
1385                                           .setOrder(sourceOrder)
1386                                           .setKey(sourceKey)
1387                                           .setIpAddress(sourceIp)
1388                                           .setUni(sourceUniIid)
1389                                           .build();
1390             sourceList.add(uniSource);
1391             updatedEvcBuilder.setUniSource(sourceList);
1392
1393             final List<UniDest> destinationList = new ArrayList<UniDest>();
1394             final UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey();
1395             final short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder();
1396             final IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress();
1397             final UniDest uniDest = new UniDestBuilder()
1398                                       .setIpAddress(destIp)
1399                                       .setOrder(destOrder)
1400                                       .setKey(destKey)
1401                                       .setUni(destinationUniIid)
1402                                       .build();
1403             destinationList.add(uniDest);
1404             updatedEvcBuilder.setUniDest(destinationList);
1405             final Optional<Link> optionalEvcLink = readLink(dataBroker,
1406                                                       LogicalDatastoreType.CONFIGURATION,
1407                                                       evcKey);
1408             if (optionalEvcLink.isPresent()) {
1409                 final Link link = optionalEvcLink.get();
1410                 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
1411                 final LinkBuilder linkBuilder = new LinkBuilder();
1412                 linkBuilder.setKey(link.getKey());
1413                 linkBuilder.setLinkId(link.getLinkId());
1414                 linkBuilder.setDestination(link.getDestination());
1415                 linkBuilder.setSource(link.getSource());
1416                 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
1417                 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
1418                 transaction.submit();
1419                 return true;
1420             } else {
1421                 LOG.info("EvcLink is not present: " + optionalEvcLink.get().getKey());
1422             }
1423         } else {
1424             LOG.info("Invalid instance identifiers for sourceUni and destUni.");
1425         }
1426         return false;
1427     }
1428 }