Refactor SupportedIfCapability usage
[transportpce.git] / olm / src / main / java / org / opendaylight / transportpce / olm / util / OlmUtils.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.olm.util;
9
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.Network;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.OpenroadmNodeVersion;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.Nodes;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.NodesKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public final class OlmUtils {
29
30     private static final Logger LOG = LoggerFactory.getLogger(OlmUtils.class);
31     private static long DATABROKER_READ_TIMEOUT_SECONDS = 120;
32
33     /**
34      * This static method returns the port mapping {@link Nodes} for node.
35      *
36      * @param nodeId
37      *            Unique identifier for the mounted netconf node
38      * @param db
39      *            Databroker used to read data from data store.
40      * @return {@link Nodes } from portMapping for given nodeId
41      */
42     public static Optional<Nodes> getNode(String nodeId, DataBroker db) {
43         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.create(Network.class)
44             .child(Nodes.class, new NodesKey(nodeId));
45         try (ReadTransaction readTransaction = db.newReadOnlyTransaction()) {
46             return readTransaction.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
47                 .get(DATABROKER_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS);
48         } catch (InterruptedException | ExecutionException | TimeoutException ex) {
49             LOG.error("Unable to read Portmapping for nodeId {}", nodeId, ex);
50             return Optional.empty();
51         }
52     }
53
54     /**
55      * This method retrieves list of current PMs for given nodeId,
56      * resourceType, resourceName and Granularity.Currently vendorExtentions
57      * are excluded but can be added back based on requirement
58      *
59      * <p>
60      * 1. pmFetch This operation traverse through current PM list and gets PM for
61      * given NodeId and Resource name
62      *
63      * @param input
64      *            Input parameter from the olm yang model get-pm rpc
65      * @param deviceTransactionManager
66      *            Device tx manager
67      * @param openRoadmVersion
68      *            OpenRoadm version number
69      *
70      * @return Result of the request list of PM readings
71      */
72     public static GetPmOutputBuilder pmFetch(GetPmInput input, DeviceTransactionManager deviceTransactionManager,
73                                              OpenroadmNodeVersion openRoadmVersion) {
74         LOG.info("Getting PM Data for NodeId: {} ResourceType: {} ResourceName: {}", input.getNodeId(),
75             input.getResourceType(), input.getResourceIdentifier());
76         GetPmOutputBuilder pmOutputBuilder;
77         switch (openRoadmVersion.getIntValue()) {
78             case 1:
79                 pmOutputBuilder = OlmUtils121.pmFetch(input, deviceTransactionManager);
80                 break;
81             case 2:
82                 pmOutputBuilder = OlmUtils221.pmFetch(input, deviceTransactionManager);
83                 break;
84             case 3:
85                 pmOutputBuilder = OlmUtils710.pmFetch(input, deviceTransactionManager);
86                 break;
87             default:
88                 LOG.error("Unrecognized OpenRoadm version");
89                 pmOutputBuilder = new GetPmOutputBuilder();
90         }
91         return pmOutputBuilder;
92     }
93
94     private OlmUtils() {
95     }
96
97 }