2 * Copyright (c) 2015 CableLabs and others. All rights reserved.
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
8 package org.opendaylight.unimgr.impl;
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;
17 import java.util.Map.Entry;
19 import java.util.UUID;
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;
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;
87 public class UnimgrUtils {
89 private static final Logger LOG = LoggerFactory.getLogger(UnimgrUtils.class);
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)
98 public static void createBridgeNode(DataBroker dataBroker,
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,
108 NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId()
109 + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
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();
122 LOG.info("OvsdbNodeRef is null");
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)
133 public static void createBridgeNode(DataBroker dataBroker,
134 InstanceIdentifier<Node> ovsdbNodeIid,
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,
143 if (optionalOvsdbNode.isPresent()) {
144 Node ovsdbNode = optionalOvsdbNode.get();
145 InstanceIdentifier<Node> bridgeIid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode,
147 NodeId bridgeNodeId = new NodeId(ovsdbNode.getNodeId().getValue()
148 + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
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,
161 bridgeNodeBuilder.build());
162 transaction.submit();
165 LOG.info("OvsdbNodeRef is null");
170 * Creates a List of Controller Entry to be used when adding controllers
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
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;
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)
192 public static void createGreTunnel(DataBroker dataBroker,
198 InstanceIdentifier<TerminationPoint> tpIid =
199 UnimgrMapper.getTerminationPointIid(bridgeNode,
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())
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,
220 transaction.submit();
224 * Utility function used to create a protocol entry when creating a bridge node.
225 * @return A List of protocol entry
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());
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.
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()
248 new OvsdbBridgeName(UnimgrConstants.DEFAULT_BRIDGE_NAME))
249 .setManagedBy(ovsdbNodeRef)
251 new Uuid(bridgeUuid.toString()))
255 throw new Exception("Ovsdb Node Reference does not exist !");
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
265 public static void createOvsdbNode(DataBroker dataBroker,
268 InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(uni.getIpAddress());
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))
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);
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
293 public static Node createOvsdbNode(DataBroker dataBroker,
294 UniAugmentation uni) {
295 NodeId ovsdbNodeId = new NodeId(createOvsdbNodeId(uni.getIpAddress()));
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))
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());
311 } catch (Exception e) {
312 LOG.error("Exception while creating OvsdbNodeAugmentation, " + "Uni is null. Node Id: {}", ovsdbNodeId);
318 * Creates and Build the data for an OvsdbNodeAugmentation.
319 * @param uni The UNI"s data
320 * @return The built OsvdbNodeAugmentation
322 public static OvsdbNodeAugmentation createOvsdbNodeAugmentation(Uni uni) {
323 ConnectionInfo connectionInfos = new ConnectionInfoBuilder()
324 .setRemoteIp(uni.getIpAddress())
325 .setRemotePort(new PortNumber(UnimgrConstants.OVSDB_PORT))
327 OvsdbNodeAugmentation ovsdbNode = new OvsdbNodeAugmentationBuilder()
328 .setConnectionInfo(connectionInfos).build();
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
337 public static NodeId createOvsdbNodeId(IpAddress ipAddress) {
338 String nodeId = UnimgrConstants.OVSDB_PREFIX
339 + ipAddress.getIpv4Address().getValue().toString()
341 + UnimgrConstants.OVSDB_PORT;
342 return new NodeId(nodeId);
346 * Creates a built OvsdbTerminationAugmentation with data
347 * @param uni The UNI's data
348 * @return A Built OvsdbTerminationPointAugmentation with data
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)
356 .setVlanMode(VlanMode.Access)
358 return terminationPoint;
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
367 public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
368 NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
369 boolean result = false;
371 InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
372 NodeKey uniNodeKey = new NodeKey(uniNodeId);
373 Node nodeData = new NodeBuilder()
374 .setNodeId(uniNodeId)
376 .addAugmentation(UniAugmentation.class, uni)
378 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
379 transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
380 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
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);
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
395 public static NodeId createUniNodeId(IpAddress ipAddress) {
396 return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
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.
409 public static void createTerminationPointNode(DataBroker dataBroker,
415 InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
416 .getTerminationPointIid(bridgeNode,
418 OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
419 new OvsdbTerminationPointAugmentationBuilder();
420 tpAugmentationBuilder.setName(portName);
422 tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type));
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,
432 transaction.submit();
436 * Deletes a generic node
437 * @param dataBroker The instance of the data broker to create transactions
438 * @param store The DataStore where the delete
439 * @param path The path to delete
440 * @return An instance of a generic Data Object
442 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
443 DataBroker dataBroker,
444 final LogicalDatastoreType store,
445 final InstanceIdentifier<D> path) {
446 boolean result = false;
447 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
448 transaction.delete(store, path);
449 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
453 } catch (TransactionCommitFailedException e) {
454 LOG.warn("Failed to delete {} ", path, e);
460 * Deletes a termination Point from the configuration data store.
461 * @param dataBroker The instance of the data broker to create transactions
462 * @param terminationPoint The Termination Point of the OVSDB bridge
463 * @param ovsdbNode The ovsdb Node
464 * @return A checked Future
466 public static CheckedFuture<Void,
467 TransactionCommitFailedException>
468 deleteTerminationPoint(DataBroker dataBroker,
469 TerminationPoint terminationPoint,
471 InstanceIdentifier<TerminationPoint> terminationPointPath =
473 .create(NetworkTopology.class)
474 .child(Topology.class,
475 new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
478 .child(TerminationPoint.class,
479 terminationPoint.getKey());
480 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
481 transaction.delete(LogicalDatastoreType.CONFIGURATION, terminationPointPath);
482 transaction.delete(LogicalDatastoreType.OPERATIONAL, terminationPointPath);
483 transaction.submit();
484 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
489 * Generic function to delete a node on a specific dataStore
490 * @param dataBroker The instance of the data broker to create transactions.
491 * @param genericNode The instance identifier of a generic node
492 * @param store The dataStore where to send and submit the delete call.
494 public static void deleteNode(DataBroker dataBroker,
495 InstanceIdentifier<?> genericNode,
496 LogicalDatastoreType store) {
497 LOG.info("Received a request to delete node {}", genericNode);
498 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
499 transaction.delete(store, genericNode);
501 transaction.submit().checkedGet();
502 } catch (TransactionCommitFailedException e) {
503 LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
508 * Extract a data object by using its instance indentifier and it's class type.
509 * @param changes Data Change object
510 * @param klazz Class type
511 * @return The extracted DataObject as an Object casted as the class type
513 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extract(
514 Map<InstanceIdentifier<?>, DataObject> changes, Class<T> klazz) {
515 Map<InstanceIdentifier<T>,T> result = new HashMap<InstanceIdentifier<T>,T>();
516 if (changes != null && changes.entrySet() != null) {
517 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
518 if (klazz.isInstance(created.getValue())) {
519 @SuppressWarnings("unchecked")
520 T value = (T) created.getValue();
521 Class<?> type = created.getKey().getTargetType();
522 if (type.equals(klazz)) {
523 @SuppressWarnings("unchecked") // Actually checked above
524 InstanceIdentifier<T> iid = (InstanceIdentifier<T>) created.getKey();
525 result.put(iid, value);
534 * Extract original data from the data store.
535 * @param changes The dataChange object
536 * @param klazz The class type
537 * @return The DataObject casted as a Class type
539 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractOriginal(
540 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
541 return extract(changes.getOriginalData(),klazz);
545 * Extracts the removed nodes
546 * @param changes he dataChange object
547 * @param klazz The class type
548 * @return A set to removed nodes as DataObject casted as the class type
550 public static <T extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
551 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
552 Set<InstanceIdentifier<T>> result = new HashSet<InstanceIdentifier<T>>();
553 if (changes != null && changes.getRemovedPaths() != null) {
554 for (InstanceIdentifier<?> iid : changes.getRemovedPaths()) {
555 if (iid.getTargetType().equals(klazz)) {
556 @SuppressWarnings("unchecked") // Actually checked above
557 InstanceIdentifier<T> iidn = (InstanceIdentifier<T>)iid;
566 * Search the Operational Datastore for a specific OvsdbNode.
567 * @param dataBroker The dataBroker instance to create transactions
568 * @param uni The UNI's data
569 * @return The Optional OvsdbNode
571 public static Optional<Node> findOvsdbNode(DataBroker dataBroker,
572 UniAugmentation uni) {
573 List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
574 Optional<Node> optionalOvsdb;
575 if (!ovsdbNodes.isEmpty()) {
576 for (Node ovsdbNode : ovsdbNodes) {
577 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
578 .getAugmentation(OvsdbNodeAugmentation.class);
579 if (ovsdbNodeAugmentation.getConnectionInfo()
582 .equals(uni.getIpAddress().getIpv4Address())) {
583 LOG.info("Found ovsdb node");
584 optionalOvsdb = Optional.of(ovsdbNode);
585 return optionalOvsdb;
589 return Optional.absent();
593 * Search the Operation DataStore for a specific UNI
594 * @param dataBroker The dataBroker instance to create transactions
595 * @param ipAddress The IP address of the UNI
596 * @return An Optional UNI Node
598 public static Optional<Node> findUniNode(DataBroker dataBroker,
599 IpAddress ipAddress) {
600 List<Node> uniNodes = getUniNodes(dataBroker);
601 if (!uniNodes.isEmpty()) {
602 for (Node uniNode : uniNodes) {
603 UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
604 if (uniAugmentation.getIpAddress().equals(ipAddress)) {
605 LOG.info("Found Uni node");
606 return Optional.of(uniNode);
610 return Optional.absent();
614 * Retrieves the connection information from an Ovsdb Connection by
615 * using the Ovsdb Node Id
616 * @param dataBroker The dataBroker instance to create transactions
617 * @param ovsdbNodeId The NodeId of the OVSDB node
618 * @return The ConnectionInfo object
620 public static ConnectionInfo getConnectionInfo(DataBroker dataBroker,
621 NodeId ovsdbNodeId) {
622 InstanceIdentifier<Node> nodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
623 Optional<Node> node = readNode(dataBroker,
624 LogicalDatastoreType.OPERATIONAL,
626 if (node.isPresent()) {
627 Node ovsdbNode = node.get();
628 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
629 .getAugmentation(OvsdbNodeAugmentation.class);
630 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
631 return connectionInfo;
638 * Retrieve the list of links in the Operational DataStore
639 * @param dataBroker The dataBroker instance to create transactions
640 * @return A list of Links retrieved from the Operational DataStore
642 public static List<Link> getEvcLinks(DataBroker dataBroker) {
643 List<Link> evcLinks = new ArrayList<>();
644 InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
645 Topology topology = UnimgrUtils.read(dataBroker,
646 LogicalDatastoreType.OPERATIONAL,
648 if (topology != null && topology.getLink() != null) {
649 for (Link link : topology.getLink()) {
650 EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
651 if (evcAugmentation != null) {
660 * Retrieve the Local IP of the controller
661 * @return The LocalIp object of the Controller
663 public static IpAddress getLocalIp() {
666 ip = InetAddress.getLocalHost().getHostAddress();
667 Ipv4Address ipv4 = new Ipv4Address(ip);
668 IpAddress ipAddress = new IpAddress(ipv4);
670 } catch (UnknownHostException e) {
671 LOG.info("Unable to retrieve controller's ip address, using loopback.");
673 return new IpAddress(UnimgrConstants.LOCAL_IP);
677 * Retrieve a list of Ovsdb Nodes from the Operational DataStore
678 * @param dataBroker The dataBroker instance to create transactions
679 * @return The Ovsdb Node retrieved from the Operational DataStore
681 public static List<Node> getOvsdbNodes(DataBroker dataBroker) {
682 List<Node> ovsdbNodes = new ArrayList<>();
683 InstanceIdentifier<Topology> ovsdbTopoIdentifier = UnimgrMapper.getOvsdbTopologyIid();
684 Topology topology = UnimgrUtils.read(dataBroker,
685 LogicalDatastoreType.OPERATIONAL,
686 ovsdbTopoIdentifier);
687 if (topology != null && topology.getNode() != null) {
688 for (Node node : topology.getNode()) {
689 OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
690 if (ovsdbNodeAugmentation != null) {
691 ovsdbNodes.add(node);
699 * Retrieve a list of Uni Nodes from the Configuration DataStore
700 * @param dataBroker The dataBroker instance to create transactions
701 * @return A list of Uni Nodes from the Config dataStore
703 public static List<Node> getUniNodes(DataBroker dataBroker) {
704 List<Node> uniNodes = new ArrayList<>();
705 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
706 Topology topology = read(dataBroker,
707 LogicalDatastoreType.CONFIGURATION,
708 topologyInstanceIdentifier);
709 if (topology != null && topology.getNode() != null) {
710 for (Node node : topology.getNode()) {
711 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
712 if (uniAugmentation != null) {
721 * Retrieve a list of Uni Nodes on a specific DataStore
722 * @param dataBroker The dataBroker instance to create transactions
723 * @param store The store to which to send the read request
724 * @return A List of UNI Nodes.
726 public static List<Node> getUniNodes(DataBroker dataBroker,
727 LogicalDatastoreType store) {
728 List<Node> uniNodes = new ArrayList<>();
729 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
730 Topology topology = read(dataBroker,
732 topologyInstanceIdentifier);
733 if (topology != null && topology.getNode() != null) {
734 for (Node node : topology.getNode()) {
735 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
736 if (uniAugmentation != null) {
745 * Read a specific datastore type and return a DataObject as a casted
747 * @param dataBroker The dataBroker instance to create transactions
748 * @param store The store type to query
749 * @param path The generic path to query
750 * @return The DataObject as a casted Object
752 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
753 DataBroker dataBroker,
754 final LogicalDatastoreType store,
755 final InstanceIdentifier<D> path) {
757 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
758 Optional<D> optionalDataObject;
759 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
761 optionalDataObject = future.checkedGet();
762 if (optionalDataObject.isPresent()) {
763 result = optionalDataObject.get();
765 LOG.debug("{}: Failed to read {}",
766 Thread.currentThread().getStackTrace()[1], path);
768 } catch (ReadFailedException e) {
769 LOG.warn("Failed to read {} ", path, e);
776 * Read a specific node from the Operational Data store by default.
777 * @param dataBroker The dataBroker instance to create transactions
778 * @param genericNode The Instance Identifier of the Node
779 * @return The Optional Node instance
782 public static final Optional<Node> readNode(DataBroker dataBroker,
783 InstanceIdentifier<?> genericNode) {
784 ReadTransaction read = dataBroker.newReadOnlyTransaction();
785 InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
786 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
787 read.read(LogicalDatastoreType.OPERATIONAL,
790 return nodeFuture.checkedGet();
791 } catch (ReadFailedException e) {
792 LOG.info("Unable to read node with Iid {}", nodeIid);
794 return Optional.absent();
798 * Read a specific Link from a specific datastore
799 * @param dataBroker The dataBroker instance to create transactions
800 * @param store The datastore type.
801 * @param genericNode The Instance Identifier of the Link
802 * @return An Optional Link instance
804 public static final Optional<Link> readLink(DataBroker dataBroker,
805 LogicalDatastoreType store,
806 InstanceIdentifier<?> genericNode) {
807 ReadTransaction read = dataBroker.newReadOnlyTransaction();
808 InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
809 CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
811 return linkFuture.checkedGet();
812 } catch (ReadFailedException e) {
813 LOG.info("Unable to read node with Iid {}", linkIid);
815 return Optional.absent();
819 * Read a specific node from a specific data store type.
820 * @param dataBroker The dataBroker instance to create transactions
821 * @param store The data store type
822 * @param genericNode The Instance Identifier of a specific Node
823 * @return An Optional Node instance
825 public static final Optional<Node> readNode(DataBroker dataBroker,
826 LogicalDatastoreType store,
827 InstanceIdentifier<?> genericNode) {
828 ReadTransaction read = dataBroker.newReadOnlyTransaction();
829 InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
830 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
831 .read(store, nodeIid);
833 return nodeFuture.checkedGet();
834 } catch (ReadFailedException e) {
835 LOG.info("Unable to read node with Iid {}", nodeIid);
837 return Optional.absent();
841 * Updates a specific Uni Node on a specific DataStore type
842 * @param dataStore The datastore type
843 * @param uniKey The UNI key
844 * @param uni The Uni's data
845 * @param ovsdbNode The Ovsdb Node
846 * @param dataBroker The dataBroker instance to create transactions
848 public static void updateUniNode(LogicalDatastoreType dataStore,
849 InstanceIdentifier<?> uniKey,
852 DataBroker dataBroker) {
853 InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
854 OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
855 UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
856 if (ovsdbNodeRef != null) {
857 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
859 Optional<Node> optionalNode = readNode(dataBroker,
860 LogicalDatastoreType.CONFIGURATION,
862 if (optionalNode.isPresent()) {
863 Node node = optionalNode.get();
864 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
865 NodeBuilder nodeBuilder = new NodeBuilder();
866 nodeBuilder.setKey(node.getKey());
867 nodeBuilder.setNodeId(node.getNodeId());
868 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
869 transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
870 transaction.submit();
875 * Update a specific UNI node on a specific datastore type
876 * @param dataStore The datastore type
877 * @param uniKey The UNI key
878 * @param uni The Uni's data
879 * @param ovsdbNodeIid The Ovsdb Node Instance Identifier
880 * @param dataBroker The dataBroker instance to create transactions
882 public static void updateUniNode(LogicalDatastoreType dataStore,
883 InstanceIdentifier<?> uniKey,
885 InstanceIdentifier<?> ovsdbNodeIid,
886 DataBroker dataBroker) {
887 OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
888 UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
889 if (ovsdbNodeRef != null) {
890 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
892 Optional<Node> optionalNode = readNode(dataBroker,
893 LogicalDatastoreType.CONFIGURATION,
895 if (optionalNode.isPresent()) {
896 Node node = optionalNode.get();
897 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
898 NodeBuilder nodeBuilder = new NodeBuilder();
899 nodeBuilder.setKey(node.getKey());
900 nodeBuilder.setNodeId(node.getNodeId());
901 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
902 transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
903 transaction.submit();
908 * Updates a specific EVC into a specific DataStore type
909 * @param dataStore The datastore type
910 * @param evcKey The EVC key
911 * @param evcAugmentation The EVC's data
912 * @param sourceUniIid The Source Uni Instance Identifier
913 * @param destinationUniIid The destination Uni Instance Identifier
914 * @param dataBroker The dataBroker instance to create transactions
916 public static void updateEvcNode(LogicalDatastoreType dataStore,
917 InstanceIdentifier<?> evcKey,
918 EvcAugmentation evcAugmentation,
919 InstanceIdentifier<?> sourceUniIid,
920 InstanceIdentifier<?> destinationUniIid,
921 DataBroker dataBroker) {
922 EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
923 if (sourceUniIid != null && destinationUniIid != null) {
924 List<UniSource> sourceList = new ArrayList<UniSource>();
925 UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey();
926 short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder();
927 IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress();
928 UniSource uniSource = new UniSourceBuilder()
929 .setOrder(sourceOrder)
931 .setIpAddress(sourceIp)
932 .setUni(sourceUniIid)
934 sourceList.add(uniSource);
935 updatedEvcBuilder.setUniSource(sourceList);
937 List<UniDest> destinationList = new ArrayList<UniDest>();
938 UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey();
939 short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder();
940 IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress();
941 UniDest uniDest = new UniDestBuilder()
942 .setIpAddress(destIp)
945 .setUni(destinationUniIid)
947 destinationList.add(uniDest);
948 updatedEvcBuilder.setUniDest(destinationList);
949 Optional<Link> optionalEvcLink = readLink(dataBroker,
950 LogicalDatastoreType.CONFIGURATION,
952 if (optionalEvcLink.isPresent()) {
953 Link link = optionalEvcLink.get();
954 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
955 LinkBuilder linkBuilder = new LinkBuilder();
956 linkBuilder.setKey(link.getKey());
957 linkBuilder.setLinkId(link.getLinkId());
958 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
959 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
960 transaction.submit();
963 LOG.info("Invalid instance identifiers for sourceUni and destUni.");