fix some sonar issues
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / utils / UniUtils.java
1 /*
2  * Copyright (c) 2016 CableLabs and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.unimgr.utils;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
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;
41
42 import com.google.common.base.Optional;
43 import com.google.common.util.concurrent.CheckedFuture;
44
45 public class UniUtils {
46
47     private static final Logger LOG = LoggerFactory.getLogger(UniUtils.class);
48
49     private UniUtils() {
50         throw new AssertionError("Instantiating utility class.");
51     }
52
53     /**
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
58      */
59     public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
60         final NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
61         boolean result = false;
62         try {
63             final InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
64             final NodeKey uniNodeKey = new NodeKey(uniNodeId);
65             final Node nodeData = new NodeBuilder()
66                                     .setNodeId(uniNodeId)
67                                     .setKey(uniNodeKey)
68                                     .addAugmentation(UniAugmentation.class, uni)
69                                     .build();
70             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
71             transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
72             final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
73             future.checkedGet();
74             result = true;
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);
78         }
79         return result;
80     }
81
82     /**
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
86      */
87     public static NodeId createUniNodeId(IpAddress ipAddress) {
88         return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
89     }
90
91     /**
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
96      */
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);
106                 }
107             }
108         }
109         return Optional.absent();
110     }
111
112     /**
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
116      */
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) {
127                     uniNodes.add(node);
128                 }
129             }
130         }
131         return uniNodes;
132     }
133
134     /**
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.
139      */
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,
145                                  store,
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) {
151                     uniNodes.add(node);
152                 }
153             }
154         }
155         return uniNodes;
156     }
157
158     /**
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.
163      */
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,
169                                  store,
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);
176                 }
177             }
178         }
179         return unis;
180     }
181
182     /**
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
187      * @return uni.
188      */
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,
193                                  store,
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;
200                 }
201             }
202         }
203         return null;
204     }
205
206     /**
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
214      */
215     public static boolean updateUniNode(LogicalDatastoreType dataStore,
216                                      InstanceIdentifier<?> uniIID,
217                                      UniAugmentation uni,
218                                      Node ovsdbNode,
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);
225         }
226         final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
227                                                LogicalDatastoreType.CONFIGURATION,
228                                                uniIID);
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();
238             return true;
239         }
240         return false;
241     }
242
243     /**
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
251      */
252     public static boolean updateUniNode(LogicalDatastoreType dataStore,
253                                      InstanceIdentifier<?> uniKey,
254                                      UniAugmentation uni,
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);
261         }
262         final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
263                                                LogicalDatastoreType.CONFIGURATION,
264                                                uniKey);
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();
274             return true;
275         }
276         return false;
277     }
278
279     public static String getSpeed(Speed speedObject) {
280         String speed = null;
281         if (speedObject instanceof Speed10M) {
282             // map to 10MB
283             speed = "10000000";
284         }
285         else if (speedObject instanceof Speed100M) {
286             // map to 20MB
287             speed = "20000000";
288         }
289         else if (speedObject instanceof Speed1G) {
290             // map to 30MB
291             speed = "30000000";
292         }
293         else if (speedObject instanceof Speed10G) {
294             // map to 40MB
295             speed = "40000000";
296         }
297         return speed;
298     }
299
300     public static Speed getSpeed(final String speed) {
301         Speed speedObject = null;
302         if (speed.equals("10M")) {
303             speedObject = new Speed10MBuilder().setSpeed10M(true)
304                                                .build();
305         }
306         else if (speed.equals("100M")) {
307             speedObject = new Speed100MBuilder().setSpeed100M(true)
308                                                 .build();
309         }
310         else if (speed.equals("1G")) {
311             speedObject = new Speed1GBuilder().setSpeed1G(true)
312                                               .build();
313         }
314         else if (speed.equals("10G")) {
315             speedObject = new Speed10GBuilder().setSpeed10G(true)
316                                                .build();
317         }
318         return speedObject;
319     }
320 }