2 * Copyright (c) 2016 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
9 package org.opendaylight.unimgr.utils;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.unimgr.impl.UnimgrConstants;
19 import org.opendaylight.unimgr.impl.UnimgrMapper;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.Speed;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.base.Optional;
43 import com.google.common.util.concurrent.CheckedFuture;
45 public class UniUtils {
47 private static final Logger LOG = LoggerFactory.getLogger(UniUtils.class);
50 throw new AssertionError("Instantiating utility class.");
54 * Creates and submit an UNI Node by using the Data contained in the UniAugmentation
55 * @param dataBroker The instance of the DataBroker to create transactions
56 * @param uni The UNI's data
57 * @return true if uni created
59 public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
60 final NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
61 boolean result = false;
63 final InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
64 final NodeKey uniNodeKey = new NodeKey(uniNodeId);
65 final Node nodeData = new NodeBuilder()
68 .addAugmentation(UniAugmentation.class, uni)
70 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
71 transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
72 final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
75 LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
76 } catch (final Exception e) {
77 LOG.error("Exception while creating Uni Node, Uni Node Id: {}", uniNodeId, e);
83 * Creates an UNI node Id with an IP Address.
84 * @param ipAddress The IP address of the UNI
85 * @return A NodeId for a Specific UNI Node Id
87 public static NodeId createUniNodeId(IpAddress ipAddress) {
88 return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
92 * Search the Operation DataStore for a specific UNI
93 * @param dataBroker The dataBroker instance to create transactions
94 * @param ipAddress The IP address of the UNI
95 * @return An Optional UNI Node
97 public static Optional<Node> findUniNode(DataBroker dataBroker,
98 IpAddress ipAddress) {
99 final List<Node> uniNodes = getUniNodes(dataBroker);
100 if (!uniNodes.isEmpty()) {
101 for (final Node uniNode : uniNodes) {
102 final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
103 if (uniAugmentation.getIpAddress().equals(ipAddress)) {
104 LOG.info("Found Uni node");
105 return Optional.of(uniNode);
109 return Optional.absent();
113 * Retrieve a list of Uni Nodes from the Configuration DataStore
114 * @param dataBroker The dataBroker instance to create transactions
115 * @return A list of Uni Nodes from the Config dataStore
117 public static List<Node> getUniNodes(DataBroker dataBroker) {
118 final List<Node> uniNodes = new ArrayList<>();
119 final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
120 final Topology topology = MdsalUtils.read(dataBroker,
121 LogicalDatastoreType.CONFIGURATION,
122 topologyInstanceIdentifier);
123 if ((topology != null) && (topology.getNode() != null)) {
124 for (final Node node : topology.getNode()) {
125 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
126 if (uniAugmentation != null) {
135 * Retrieve a list of Uni Nodes on a specific DataStore
136 * @param dataBroker The dataBroker instance to create transactions
137 * @param store The store to which to send the read request
138 * @return A List of UNI Nodes.
140 public static List<Node> getUniNodes(DataBroker dataBroker,
141 LogicalDatastoreType store) {
142 final List<Node> uniNodes = new ArrayList<>();
143 final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
144 final Topology topology = MdsalUtils.read(dataBroker,
146 topologyInstanceIdentifier);
147 if ((topology != null) && (topology.getNode() != null)) {
148 for (final Node node : topology.getNode()) {
149 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
150 if (uniAugmentation != null) {
159 * Retrieve a list of Unis on a specific DataStore
160 * @param dataBroker instance to create transactions
161 * @param store to which send the read request
162 * @return A List of Unis.
164 public static List<UniAugmentation> getUnis(DataBroker dataBroker,
165 LogicalDatastoreType store) {
166 final List<UniAugmentation> unis = new ArrayList<>();
167 final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
168 final Topology topology = MdsalUtils.read(dataBroker,
170 topologyInstanceIdentifier);
171 if ((topology != null) && (topology.getNode() != null)) {
172 for (final Node node : topology.getNode()) {
173 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
174 if (uniAugmentation != null) {
175 unis.add(uniAugmentation);
183 * Retrieve a list of Unis on a specific DataStore
184 * @param dataBroker instance to create transactions
185 * @param store to which send the read request
186 * @param ipAddress of the required Uni
189 public static UniAugmentation getUni(DataBroker dataBroker,
190 LogicalDatastoreType store, IpAddress ipAddress) {
191 final InstanceIdentifier<Topology> topologyInstanceIdentifier = UnimgrMapper.getUniTopologyIid();
192 final Topology topology = MdsalUtils.read(dataBroker,
194 topologyInstanceIdentifier);
195 if ((topology != null) && (topology.getNode() != null)) {
196 for (final Node node : topology.getNode()) {
197 final UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
198 if ((uniAugmentation != null) && uniAugmentation.getIpAddress().getIpv4Address().getValue().equals(ipAddress.getIpv4Address().getValue())) {
199 return uniAugmentation;
207 * Updates a specific Uni Node on a specific DataStore type
208 * @param dataStore The datastore type
209 * @param uniIID The UNI InstanceIdentifier
210 * @param uni The Uni's data
211 * @param ovsdbNode The Ovsdb Node
212 * @param dataBroker The dataBroker instance to create transactions
213 * @return true if uni is updated
215 public static boolean updateUniNode(LogicalDatastoreType dataStore,
216 InstanceIdentifier<?> uniIID,
219 DataBroker dataBroker) {
220 final InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
221 final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
222 final UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
223 if (ovsdbNodeRef != null) {
224 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
226 final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
227 LogicalDatastoreType.CONFIGURATION,
229 if (optionalNode.isPresent()) {
230 final Node node = optionalNode.get();
231 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
232 final NodeBuilder nodeBuilder = new NodeBuilder();
233 nodeBuilder.setKey(node.getKey());
234 nodeBuilder.setNodeId(node.getNodeId());
235 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
236 transaction.put(dataStore, uniIID.firstIdentifierOf(Node.class), nodeBuilder.build());
237 transaction.submit();
244 * Update a specific UNI node on a specific datastore type
245 * @param dataStore The datastore type
246 * @param uniKey The UNI key
247 * @param uni The Uni's data
248 * @param ovsdbNodeIid The Ovsdb Node Instance Identifier
249 * @param dataBroker The dataBroker instance to create transactions
250 * @return true if uni is updated
252 public static boolean updateUniNode(LogicalDatastoreType dataStore,
253 InstanceIdentifier<?> uniKey,
255 InstanceIdentifier<?> ovsdbNodeIid,
256 DataBroker dataBroker) {
257 final OvsdbNodeRef ovsdbNodeRef = new OvsdbNodeRef(ovsdbNodeIid);
258 final UniAugmentationBuilder updatedUniBuilder = new UniAugmentationBuilder(uni);
259 if (ovsdbNodeRef != null) {
260 updatedUniBuilder.setOvsdbNodeRef(ovsdbNodeRef);
262 final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
263 LogicalDatastoreType.CONFIGURATION,
265 if (optionalNode.isPresent()) {
266 final Node node = optionalNode.get();
267 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
268 final NodeBuilder nodeBuilder = new NodeBuilder();
269 nodeBuilder.setKey(node.getKey());
270 nodeBuilder.setNodeId(node.getNodeId());
271 nodeBuilder.addAugmentation(UniAugmentation.class, updatedUniBuilder.build());
272 transaction.put(dataStore, uniKey.firstIdentifierOf(Node.class), nodeBuilder.build());
273 transaction.submit();
279 public static String getSpeed(Speed speedObject) {
281 if (speedObject instanceof Speed10M) {
285 else if (speedObject instanceof Speed100M) {
289 else if (speedObject instanceof Speed1G) {
293 else if (speedObject instanceof Speed10G) {
300 public static Speed getSpeed(final String speed) {
301 Speed speedObject = null;
302 if (speed.equals("10M")) {
303 speedObject = new Speed10MBuilder().setSpeed10M(true)
306 else if (speed.equals("100M")) {
307 speedObject = new Speed100MBuilder().setSpeed100M(true)
310 else if (speed.equals("1G")) {
311 speedObject = new Speed1GBuilder().setSpeed1G(true)
314 else if (speed.equals("10G")) {
315 speedObject = new Speed10GBuilder().setSpeed10G(true)