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 evc by using the Data contained in the EvcAugmentation
363 * @param dataBroker The instance of the DataBroker to create transactions
364 * @param evc The EVC's data
365 * @return true if evc created
367 public static boolean createEvc(DataBroker dataBroker, EvcAugmentation evc) {
372 * Creates and submit an UNI Node by using the Data contained in the UniAugmentation
373 * @param dataBroker The instance of the DataBroker to create transactions
374 * @param uni The UNI's data
375 * @return true if uni created
377 public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
378 NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
379 boolean result = false;
381 InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
382 NodeKey uniNodeKey = new NodeKey(uniNodeId);
383 Node nodeData = new NodeBuilder()
384 .setNodeId(uniNodeId)
386 .addAugmentation(UniAugmentation.class, uni)
388 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
389 transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
390 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
393 LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
394 } catch (Exception e) {
395 LOG.error("Exception while creating Uni Node" + "Uni Node Id: {}", uniNodeId);
401 * Creates an UNI node Id with an IP Address.
402 * @param ipAddress The IP address of the UNI
403 * @return A NodeId for a Specific UNI Node Id
405 public static NodeId createUniNodeId(IpAddress ipAddress) {
406 return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
410 * Creates and Submit a termination point Node to the configuration DateStore.
411 * @param dataBroker The instance of the data broker to create transactions
412 * @param uni The UNI's data
413 * @param bridgeNode The Bridge node
414 * @param bridgeName The Bridge name (example: br0)
415 * @param portName The Port name (example: eth0)
416 * @param type The type of termination (example: gre) Refer to OVSDB_INTERFACE_TYPE_MAP
417 * to review the list of available Interface Types.
419 public static void createTerminationPointNode(DataBroker dataBroker,
425 InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
426 .getTerminationPointIid(bridgeNode,
428 OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
429 new OvsdbTerminationPointAugmentationBuilder();
430 tpAugmentationBuilder.setName(portName);
432 tpAugmentationBuilder.setInterfaceType(SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type));
434 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
435 tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
436 tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
437 tpAugmentationBuilder.build());
438 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
439 transaction.put(LogicalDatastoreType.CONFIGURATION,
442 transaction.submit();
446 * Creates and Submit a termination point Node without specifying its interface type.
447 * @param dataBroker The instance of the data broker to create transactions
448 * @param uni The UNI's data
449 * @param bridgeNode The Bridge node
450 * @param bridgeName The Bridge name (example: br0)
451 * @param portName The Port name (example: eth0)
453 public static void createTerminationPointNode(DataBroker dataBroker,
458 InstanceIdentifier<TerminationPoint> tpIid = UnimgrMapper
459 .getTerminationPointIid(bridgeNode,
461 OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
462 new OvsdbTerminationPointAugmentationBuilder();
463 tpAugmentationBuilder.setName(portName);
464 tpAugmentationBuilder.setInterfaceType(null);
465 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
466 tpBuilder.setKey(InstanceIdentifier.keyOf(tpIid));
467 tpBuilder.addAugmentation(OvsdbTerminationPointAugmentation.class,
468 tpAugmentationBuilder.build());
469 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
470 transaction.put(LogicalDatastoreType.CONFIGURATION,
473 transaction.submit();
477 * Deletes a generic node
478 * @param dataBroker The instance of the data broker to create transactions
479 * @param store The DataStore where the delete
480 * @param path The path to delete
481 * @return An instance of a generic Data Object
483 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
484 DataBroker dataBroker,
485 final LogicalDatastoreType store,
486 final InstanceIdentifier<D> path) {
487 boolean result = false;
488 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
489 transaction.delete(store, path);
490 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
494 } catch (TransactionCommitFailedException e) {
495 LOG.warn("Failed to delete {} ", path, e);
501 * Deletes a termination Point from the configuration data store.
502 * @param dataBroker The instance of the data broker to create transactions
503 * @param terminationPoint The Termination Point of the OVSDB bridge
504 * @param ovsdbNode The ovsdb Node
505 * @return A checked Future
507 public static CheckedFuture<Void,
508 TransactionCommitFailedException>
509 deleteTerminationPoint(DataBroker dataBroker,
510 TerminationPoint terminationPoint,
512 InstanceIdentifier<TerminationPoint> terminationPointPath =
514 .create(NetworkTopology.class)
515 .child(Topology.class,
516 new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
519 .child(TerminationPoint.class,
520 terminationPoint.getKey());
521 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
522 transaction.delete(LogicalDatastoreType.CONFIGURATION, terminationPointPath);
523 transaction.delete(LogicalDatastoreType.OPERATIONAL, terminationPointPath);
524 transaction.submit();
525 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
530 * Generic function to delete a node on a specific dataStore
531 * @param dataBroker The instance of the data broker to create transactions.
532 * @param genericNode The instance identifier of a generic node
533 * @param store The dataStore where to send and submit the delete call.
535 public static boolean deleteNode(DataBroker dataBroker,
536 InstanceIdentifier<?> genericNode,
537 LogicalDatastoreType store) {
538 LOG.info("Received a request to delete node {}", genericNode);
539 boolean result = false;
540 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
541 transaction.delete(store, genericNode);
543 transaction.submit().checkedGet();
545 } catch (TransactionCommitFailedException e) {
546 LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
552 * Extract a data object by using its instance indentifier and it's class type.
553 * @param changes Data Change object
554 * @param klazz Class type
555 * @return The extracted DataObject as an Object casted as the class type
557 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extract(
558 Map<InstanceIdentifier<?>, DataObject> changes, Class<T> klazz) {
559 Map<InstanceIdentifier<T>,T> result = new HashMap<InstanceIdentifier<T>,T>();
560 if (changes != null && changes.entrySet() != null) {
561 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
562 if (klazz.isInstance(created.getValue())) {
563 @SuppressWarnings("unchecked")
564 T value = (T) created.getValue();
565 Class<?> type = created.getKey().getTargetType();
566 if (type.equals(klazz)) {
567 @SuppressWarnings("unchecked") // Actually checked above
568 InstanceIdentifier<T> iid = (InstanceIdentifier<T>) created.getKey();
569 result.put(iid, value);
578 * Extract original data from the data store.
579 * @param changes The dataChange object
580 * @param klazz The class type
581 * @return The DataObject casted as a Class type
583 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractOriginal(
584 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
585 return extract(changes.getOriginalData(),klazz);
589 * Extracts the removed nodes
590 * @param changes he dataChange object
591 * @param klazz The class type
592 * @return A set to removed nodes as DataObject casted as the class type
594 public static <T extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
595 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
596 Set<InstanceIdentifier<T>> result = new HashSet<InstanceIdentifier<T>>();
597 if (changes != null && changes.getRemovedPaths() != null) {
598 for (InstanceIdentifier<?> iid : changes.getRemovedPaths()) {
599 if (iid.getTargetType().equals(klazz)) {
600 @SuppressWarnings("unchecked") // Actually checked above
601 InstanceIdentifier<T> iidn = (InstanceIdentifier<T>)iid;
610 * Search the Operational Datastore for a specific OvsdbNode.
611 * @param dataBroker The dataBroker instance to create transactions
612 * @param uni The UNI's data
613 * @return The Optional OvsdbNode
615 public static Optional<Node> findOvsdbNode(DataBroker dataBroker,
616 UniAugmentation uni) {
617 List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
618 Optional<Node> optionalOvsdb;
619 if (!ovsdbNodes.isEmpty()) {
620 for (Node ovsdbNode : ovsdbNodes) {
621 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
622 .getAugmentation(OvsdbNodeAugmentation.class);
623 if (ovsdbNodeAugmentation.getConnectionInfo()
626 .equals(uni.getIpAddress().getIpv4Address())) {
627 LOG.info("Found ovsdb node");
628 optionalOvsdb = Optional.of(ovsdbNode);
629 return optionalOvsdb;
633 return Optional.absent();
637 * Search the Operation DataStore for a specific UNI
638 * @param dataBroker The dataBroker instance to create transactions
639 * @param ipAddress The IP address of the UNI
640 * @return An Optional UNI Node
642 public static Optional<Node> findUniNode(DataBroker dataBroker,
643 IpAddress ipAddress) {
644 List<Node> uniNodes = getUniNodes(dataBroker);
645 if (!uniNodes.isEmpty()) {
646 for (Node uniNode : uniNodes) {
647 UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
648 if (uniAugmentation.getIpAddress().equals(ipAddress)) {
649 LOG.info("Found Uni node");
650 return Optional.of(uniNode);
654 return Optional.absent();
658 * Retrieves the connection information from an Ovsdb Connection by
659 * using the Ovsdb Node Id
660 * @param dataBroker The dataBroker instance to create transactions
661 * @param ovsdbNodeId The NodeId of the OVSDB node
662 * @return The ConnectionInfo object
664 public static ConnectionInfo getConnectionInfo(DataBroker dataBroker,
665 NodeId ovsdbNodeId) {
666 InstanceIdentifier<Node> nodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNodeId);
667 Optional<Node> node = readNode(dataBroker,
668 LogicalDatastoreType.OPERATIONAL,
670 if (node.isPresent()) {
671 Node ovsdbNode = node.get();
672 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
673 .getAugmentation(OvsdbNodeAugmentation.class);
674 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
675 return connectionInfo;
682 * Retrieve the list of links in the Operational DataStore
683 * @param dataBroker The dataBroker instance to create transactions
684 * @return A list of Links retrieved from the Operational DataStore
686 public static List<Link> getEvcLinks(DataBroker dataBroker) {
687 List<Link> evcLinks = new ArrayList<>();
688 InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
689 Topology topology = UnimgrUtils.read(dataBroker,
690 LogicalDatastoreType.OPERATIONAL,
692 if (topology != null && topology.getLink() != null) {
693 for (Link link : topology.getLink()) {
694 EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
695 if (evcAugmentation != null) {
704 * Retrieve the Local IP of the controller
705 * @return The LocalIp object of the Controller
707 public static IpAddress getLocalIp() {
710 ip = InetAddress.getLocalHost().getHostAddress();
711 Ipv4Address ipv4 = new Ipv4Address(ip);
712 IpAddress ipAddress = new IpAddress(ipv4);
714 } catch (UnknownHostException e) {
715 LOG.info("Unable to retrieve controller's ip address, using loopback.");
717 return new IpAddress(UnimgrConstants.LOCAL_IP);
721 * Retrieve a list of Ovsdb Nodes from the Operational DataStore
722 * @param dataBroker The dataBroker instance to create transactions
723 * @return The Ovsdb Node retrieved from the Operational DataStore
725 public static List<Node> getOvsdbNodes(DataBroker dataBroker) {
726 List<Node> ovsdbNodes = new ArrayList<>();
727 InstanceIdentifier<Topology> ovsdbTopoIdentifier = UnimgrMapper.getOvsdbTopologyIid();
728 Topology topology = UnimgrUtils.read(dataBroker,
729 LogicalDatastoreType.OPERATIONAL,
730 ovsdbTopoIdentifier);
731 if (topology != null && topology.getNode() != null) {
732 for (Node node : topology.getNode()) {
733 OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
734 if (ovsdbNodeAugmentation != null) {
735 ovsdbNodes.add(node);
743 * Retrieve a list of Uni Nodes from the Configuration DataStore
744 * @param dataBroker The dataBroker instance to create transactions
745 * @return A list of Uni Nodes from the Config dataStore
747 public static List<Node> getUniNodes(DataBroker dataBroker) {
748 List<Node> uniNodes = new ArrayList<>();
749 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
750 Topology topology = read(dataBroker,
751 LogicalDatastoreType.CONFIGURATION,
752 topologyInstanceIdentifier);
753 if (topology != null && topology.getNode() != null) {
754 for (Node node : topology.getNode()) {
755 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
756 if (uniAugmentation != null) {
765 * Retrieve a list of Uni Nodes on a specific DataStore
766 * @param dataBroker The dataBroker instance to create transactions
767 * @param store The store to which to send the read request
768 * @return A List of UNI Nodes.
770 public static List<Node> getUniNodes(DataBroker dataBroker,
771 LogicalDatastoreType store) {
772 List<Node> uniNodes = new ArrayList<>();
773 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
774 Topology topology = read(dataBroker,
776 topologyInstanceIdentifier);
777 if (topology != null && topology.getNode() != null) {
778 for (Node node : topology.getNode()) {
779 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
780 if (uniAugmentation != null) {
789 * Retrieve a list of Unis on a specific DataStore
790 * @param dataBroker instance to create transactions
791 * @param store to which send the read request
792 * @return A List of Unis.
794 public static List<UniAugmentation> getUnis(DataBroker dataBroker,
795 LogicalDatastoreType store) {
796 List<UniAugmentation> unis = new ArrayList<>();
797 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
798 Topology topology = read(dataBroker,
800 topologyInstanceIdentifier);
801 if (topology != null && topology.getNode() != null) {
802 for (Node node : topology.getNode()) {
803 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
804 if (uniAugmentation != null) {
805 unis.add(uniAugmentation);
813 * Retrieve a list of Unis on a specific DataStore
814 * @param dataBroker instance to create transactions
815 * @param store to which send the read request
816 * @param ipAddress of the required Uni
819 public static UniAugmentation getUni(DataBroker dataBroker,
820 LogicalDatastoreType store, IpAddress ipAddress) {
821 InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
822 Topology topology = read(dataBroker,
824 topologyInstanceIdentifier);
825 if (topology != null && topology.getNode() != null) {
826 for (Node node : topology.getNode()) {
827 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
828 if (uniAugmentation != null && uniAugmentation.getIpAddress().getIpv4Address().getValue().equals(ipAddress.getIpv4Address().getValue())) {
829 return uniAugmentation;
837 * Read a specific datastore type and return a DataObject as a casted
839 * @param dataBroker The dataBroker instance to create transactions
840 * @param store The store type to query
841 * @param path The generic path to query
842 * @return The DataObject as a casted Object
844 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
845 DataBroker dataBroker,
846 final LogicalDatastoreType store,
847 final InstanceIdentifier<D> path) {
849 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
850 Optional<D> optionalDataObject;
851 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
853 optionalDataObject = future.checkedGet();
854 if (optionalDataObject.isPresent()) {
855 result = optionalDataObject.get();
857 LOG.debug("{}: Failed to read {}",
858 Thread.currentThread().getStackTrace()[1], path);
860 } catch (ReadFailedException e) {
861 LOG.warn("Failed to read {} ", path, e);
868 * Read a specific node from the Operational Data store by default.
869 * @param dataBroker The dataBroker instance to create transactions
870 * @param genericNode The Instance Identifier of the Node
871 * @return The Optional Node instance
874 public static final Optional<Node> readNode(DataBroker dataBroker,
875 InstanceIdentifier<?> genericNode) {
876 ReadTransaction read = dataBroker.newReadOnlyTransaction();
877 InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
878 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
879 read.read(LogicalDatastoreType.OPERATIONAL,
882 return nodeFuture.checkedGet();
883 } catch (ReadFailedException e) {
884 LOG.info("Unable to read node with Iid {}", nodeIid);
886 return Optional.absent();
890 * Read a specific Link from a specific datastore
891 * @param dataBroker The dataBroker instance to create transactions
892 * @param store The datastore type.
893 * @param genericNode The Instance Identifier of the Link
894 * @return An Optional Link instance
896 public static final Optional<Link> readLink(DataBroker dataBroker,
897 LogicalDatastoreType store,
898 InstanceIdentifier<?> genericNode) {
899 ReadTransaction read = dataBroker.newReadOnlyTransaction();
900 InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
901 CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
903 return linkFuture.checkedGet();
904 } catch (ReadFailedException e) {
905 LOG.info("Unable to read node with Iid {}", linkIid);
907 return Optional.absent();
911 * Read a specific node from a specific data store type.
912 * @param dataBroker The dataBroker instance to create transactions
913 * @param store The data store type
914 * @param genericNode The Instance Identifier of a specific Node
915 * @return An Optional Node instance
917 public static final Optional<Node> readNode(DataBroker dataBroker,
918 LogicalDatastoreType store,
919 InstanceIdentifier<?> genericNode) {
920 ReadTransaction read = dataBroker.newReadOnlyTransaction();
921 InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
922 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
923 .read(store, nodeIid);
925 return nodeFuture.checkedGet();
926 } catch (ReadFailedException e) {
927 LOG.info("Unable to read node with Iid {}", nodeIid);
929 return Optional.absent();
933 * Updates a specific Uni Node on a specific DataStore type
934 * @param dataStore The datastore type
935 * @param uniKey The UNI key
936 * @param uni The Uni's data
937 * @param ovsdbNode The Ovsdb Node
938 * @param dataBroker The dataBroker instance to create transactions
940 public static void updateUniNode(LogicalDatastoreType dataStore,
941 InstanceIdentifier<?> uniKey,
944 DataBroker dataBroker) {
945 InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
946 OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
947 UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
948 if (ovsdbNodeRef != null) {
949 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
951 Optional<Node> optionalNode = readNode(dataBroker,
952 LogicalDatastoreType.CONFIGURATION,
954 if (optionalNode.isPresent()) {
955 Node node = optionalNode.get();
956 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
957 NodeBuilder nodeBuilder = new NodeBuilder();
958 nodeBuilder.setKey(node.getKey());
959 nodeBuilder.setNodeId(node.getNodeId());
960 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
961 transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
962 transaction.submit();
967 * Update a specific UNI node on a specific datastore type
968 * @param dataStore The datastore type
969 * @param uniKey The UNI key
970 * @param uni The Uni's data
971 * @param ovsdbNodeIid The Ovsdb Node Instance Identifier
972 * @param dataBroker The dataBroker instance to create transactions
974 public static void updateUniNode(LogicalDatastoreType dataStore,
975 InstanceIdentifier<?> uniKey,
977 InstanceIdentifier<?> ovsdbNodeIid,
978 DataBroker dataBroker) {
979 OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
980 UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
981 if (ovsdbNodeRef != null) {
982 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
984 Optional<Node> optionalNode = readNode(dataBroker,
985 LogicalDatastoreType.CONFIGURATION,
987 if (optionalNode.isPresent()) {
988 Node node = optionalNode.get();
989 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
990 NodeBuilder nodeBuilder = new NodeBuilder();
991 nodeBuilder.setKey(node.getKey());
992 nodeBuilder.setNodeId(node.getNodeId());
993 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
994 transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
995 transaction.submit();
1000 * Updates a specific EVC into a specific DataStore type
1001 * @param dataStore The datastore type
1002 * @param evcKey The EVC key
1003 * @param evcAugmentation The EVC's data
1004 * @param sourceUniIid The Source Uni Instance Identifier
1005 * @param destinationUniIid The destination Uni Instance Identifier
1006 * @param dataBroker The dataBroker instance to create transactions
1008 public static void updateEvcNode(LogicalDatastoreType dataStore,
1009 InstanceIdentifier<?> evcKey,
1010 EvcAugmentation evcAugmentation,
1011 InstanceIdentifier<?> sourceUniIid,
1012 InstanceIdentifier<?> destinationUniIid,
1013 DataBroker dataBroker) {
1014 EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
1015 if (sourceUniIid != null && destinationUniIid != null) {
1016 List<UniSource> sourceList = new ArrayList<UniSource>();
1017 UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey();
1018 short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder();
1019 IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress();
1020 UniSource uniSource = new UniSourceBuilder()
1021 .setOrder(sourceOrder)
1023 .setIpAddress(sourceIp)
1024 .setUni(sourceUniIid)
1026 sourceList.add(uniSource);
1027 updatedEvcBuilder.setUniSource(sourceList);
1029 List<UniDest> destinationList = new ArrayList<UniDest>();
1030 UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey();
1031 short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder();
1032 IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress();
1033 UniDest uniDest = new UniDestBuilder()
1034 .setIpAddress(destIp)
1035 .setOrder(destOrder)
1037 .setUni(destinationUniIid)
1039 destinationList.add(uniDest);
1040 updatedEvcBuilder.setUniDest(destinationList);
1041 Optional<Link> optionalEvcLink = readLink(dataBroker,
1042 LogicalDatastoreType.CONFIGURATION,
1044 if (optionalEvcLink.isPresent()) {
1045 Link link = optionalEvcLink.get();
1046 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
1047 LinkBuilder linkBuilder = new LinkBuilder();
1048 linkBuilder.setKey(link.getKey());
1049 linkBuilder.setLinkId(link.getLinkId());
1050 linkBuilder.setDestination(link.getDestination());
1051 linkBuilder.setSource(link.getSource());
1052 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
1053 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
1054 transaction.submit();
1057 LOG.info("Invalid instance identifiers for sourceUni and destUni.");