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