Initial commit for OLM
[transportpce.git] / olm / src / main / java / org / opendaylight / transportpce / olm / OlmPowerSetupImpl.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;
9
10 import com.google.common.base.Optional;
11
12 import java.lang.reflect.InvocationTargetException;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.transportpce.olm.spanloss.RoadmLinks;
23 import org.opendaylight.transportpce.olm.spanloss.SpanLoss;
24 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.Measurements;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.currentpmlist.CurrentPm;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmNamesEnum;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.OlmService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.MeasurementsBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The Class OlmPowerSetupImpl.
45  */
46 public class OlmPowerSetupImpl implements OlmService {
47
48     /** The Constant LOG. */
49     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerSetupImpl.class);
50
51     /** The db. */
52     private final DataBroker db;
53
54     /** The mps. */
55     private final MountPointService mps;
56
57     /**
58      * Instantiates a new olm power setup impl.
59      *
60      * @param db
61      *            the db
62      * @param mps
63      *            the mps
64      */
65     public OlmPowerSetupImpl(DataBroker db, MountPointService mps) {
66         this.db = db;
67         this.mps = mps;
68     }
69
70     /**
71      * This method is the implementation of the 'get-pm' RESTCONF service, which
72      * is one of the external APIs into the olm application.
73      *
74      * <p>
75      * 1. get-pm This operation traverse through current PM list and gets PM for
76      * given NodeId and Resource name
77      *
78      * <p>
79      * The signature for this method was generated by yang tools from the
80      * renderer API model.
81      *
82      * @param input
83      *            Input parameter from the olm yang model
84      *
85      * @return Result of the request
86      */
87     @Override
88     public Future<RpcResult<GetPmOutput>> getPm(GetPmInput input) {
89         LOG.info("Getting PM Data for NodeId: {} ResouceType: {} ResourceName: {}",
90                 input.getNodeId(),input.getResourceType(),input.getResourceName());
91         new PortMapping(db, mps, input.getNodeId());
92         DataBroker deviceDb = PortMapping.getDeviceDataBroker(input.getNodeId(), mps);
93         InstanceIdentifier<CurrentPmlist> currentPmsIID = InstanceIdentifier.create(CurrentPmlist.class);
94         ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
95         Optional<CurrentPmlist> currentPmList;
96         String methodName = "";
97         List<CurrentPm> currentPms = new ArrayList<>();
98         GetPmOutputBuilder pmOutputBuilder = new GetPmOutputBuilder();
99         List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.Measurements>
100             measrements = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418
101                 .get.pm.output.Measurements>();
102         try {
103             currentPmList = rtx.read(LogicalDatastoreType.OPERATIONAL, currentPmsIID).get();
104             if (currentPmList.isPresent()) {
105                 currentPms.addAll(currentPmList.get().getCurrentPm());
106                 for (CurrentPm pm : currentPms) {
107                     MeasurementsBuilder measurement = new MeasurementsBuilder();
108                     if (pm.getResource().getResourceType().getType().toString().equals(input.getResourceType())) {
109
110                         switch (pm.getResource().getResourceType().getType()) {
111                             case CircuitPack:
112                                 methodName = "getCircuitPackName";
113                                 break;
114                             case Connection:
115                                 methodName = "getConnectionNumber";
116                                 break;
117                             case Degree:
118                                 methodName = "getDegreeNumber";
119                                 break;
120                             case Interface:
121                                 methodName = "getInterfaceName";
122                                 break;
123                             case InternalLink:
124                                 methodName = "getInternalLinkName";
125                                 break;
126                             case PhysicalLink:
127                                 methodName = "getPhysicalLinkName";
128                                 break;
129                             case Service:
130                                 methodName = "getServiceName";
131                                 break;
132                             case Shelf:
133                                 methodName = "getShelfName";
134                                 break;
135                             case SharedRiskGroup:
136                                 methodName = "getSrgNumber";
137                                 break;
138                             default:
139                                 methodName = "getPort";
140                                 break;
141                         }
142                         try {
143
144                             String pmResourceId = pm.getResource().getResource().getResource().getImplementedInterface()
145                                 .getMethod(methodName).invoke(pm.getResource().getResource().getResource()).toString();
146                             if (pmResourceId.equals(input.getResourceName()) && pm.getGranularity() != null) {
147                                 if (pm.getGranularity().getName().equals(input.getGranularity())) {
148                                     for (Measurements measure : pm.getMeasurements()) {
149                                         if (!measure.getMeasurement().getPmParameterName().getType().equals(
150                                             PmNamesEnum.VendorExtension)) {
151                                             measurement.setPmparameterName(measure.getMeasurement().getPmParameterName()
152                                                 .getType().toString());
153                                             measurement.setPmparameterValue(measure.getMeasurement()
154                                                 .getPmParameterValue().getDecimal64().toString());
155                                             measrements.add(measurement.build());
156                                         }
157                                     }
158                                 }
159                             }
160
161                         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
162                             | NoSuchMethodException | SecurityException ex) {
163                             LOG.warn("Unable to find PM for NodeID: {} ResourceName: {} ",input.getNodeId(),
164                                     input.getResourceName(),ex);
165                         }
166                     }
167                 }
168                 if (measrements.size() > 0) {
169                     pmOutputBuilder.setNodeId(input.getNodeId()).setResourceType(input.getResourceType()).setResourceId(
170                         input.getResourceName()).setGranularity(input.getGranularity()).setMeasurements(measrements);
171                 }
172
173             } else {
174                 LOG.info("Device PM Data is not available");
175             }
176
177         } catch (InterruptedException | ExecutionException ex) {
178             LOG.warn("Unable to get currentPmList for NodeID: {}",input.getNodeId(),ex);
179         }
180         LOG.info("PM Data found successfully for {}-{}",pmOutputBuilder.getNodeId(),pmOutputBuilder.getResourceId());
181         return RpcResultBuilder.success(pmOutputBuilder).buildFuture();
182     }
183
184
185     /**
186      * This method is the implementation of the 'service-power-setup' RESTCONF service, which
187      * is one of the external APIs into the olm application.
188      *
189      * <p>
190      * 1. service-power-setup: This operation performs following steps:
191      *    Step1: Calculate Spanloss on all links which are part of service.
192      *    TODO Step2: Calculate power levels for each Tp-Id
193      *    TODO Step3: Post power values on roadm connections
194      *
195      * <p>
196      * The signature for this method was generated by yang tools from the
197      * renderer API model.
198      *
199      * @param input
200      *            Input parameter from the olm yang model
201      *            Input will contain nodeId and termination point
202      *
203      * @return Result of the request
204      */
205     @Override
206     public Future<org.opendaylight.yangtools.yang.common.RpcResult<ServicePowerSetupOutput>> servicePowerSetup(
207         ServicePowerSetupInput input) {
208         List<RoadmLinks> roadmLinks = new ArrayList<RoadmLinks>();
209         ServicePowerSetupOutputBuilder output = new ServicePowerSetupOutputBuilder();
210         //Finds degree TpID from node and generates a list of links
211         for (int i = 0; i < input.getNodes().size(); i++) {
212             if (input.getNodes().get(i).getDestTp().toLowerCase().contains("deg")) {
213                 RoadmLinks rdmLink = new RoadmLinks();
214                 rdmLink.setSrcNodeId(input.getNodes().get(i).getNodeId());
215                 rdmLink.setSrcTpId(input.getNodes().get(i).getDestTp());
216                 rdmLink.setDestNodeId(input.getNodes().get(i + 1).getNodeId());
217                 rdmLink.setDestTpid(input.getNodes().get(i + 1).getSrcTp());
218                 roadmLinks.add(rdmLink);
219             }
220         }
221         boolean successVal = new SpanLoss(db, mps).getLinkSpanloss(roadmLinks);
222
223         if (successVal) {
224             output.setResult("Success");
225         } else {
226             output.setResult("Failed");
227         }
228
229         return RpcResultBuilder.success(output).buildFuture();
230
231     }
232
233 }