Initial commit for OLM
[transportpce.git] / olm / src / main / java / org / opendaylight / transportpce / olm / OlmPowerSetupImpl.java
diff --git a/olm/src/main/java/org/opendaylight/transportpce/olm/OlmPowerSetupImpl.java b/olm/src/main/java/org/opendaylight/transportpce/olm/OlmPowerSetupImpl.java
new file mode 100644 (file)
index 0000000..ba2d078
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+ * Copyright © 2017 AT&T and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.olm;
+
+import com.google.common.base.Optional;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.MountPointService;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.transportpce.olm.spanloss.RoadmLinks;
+import org.opendaylight.transportpce.olm.spanloss.SpanLoss;
+import org.opendaylight.transportpce.renderer.mapping.PortMapping;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.Measurements;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.currentpmlist.CurrentPm;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmNamesEnum;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.OlmService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.ServicePowerSetupOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.MeasurementsBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class OlmPowerSetupImpl.
+ */
+public class OlmPowerSetupImpl implements OlmService {
+
+    /** The Constant LOG. */
+    private static final Logger LOG = LoggerFactory.getLogger(OlmPowerSetupImpl.class);
+
+    /** The db. */
+    private final DataBroker db;
+
+    /** The mps. */
+    private final MountPointService mps;
+
+    /**
+     * Instantiates a new olm power setup impl.
+     *
+     * @param db
+     *            the db
+     * @param mps
+     *            the mps
+     */
+    public OlmPowerSetupImpl(DataBroker db, MountPointService mps) {
+        this.db = db;
+        this.mps = mps;
+    }
+
+    /**
+     * This method is the implementation of the 'get-pm' RESTCONF service, which
+     * is one of the external APIs into the olm application.
+     *
+     * <p>
+     * 1. get-pm This operation traverse through current PM list and gets PM for
+     * given NodeId and Resource name
+     *
+     * <p>
+     * The signature for this method was generated by yang tools from the
+     * renderer API model.
+     *
+     * @param input
+     *            Input parameter from the olm yang model
+     *
+     * @return Result of the request
+     */
+    @Override
+    public Future<RpcResult<GetPmOutput>> getPm(GetPmInput input) {
+        LOG.info("Getting PM Data for NodeId: {} ResouceType: {} ResourceName: {}",
+                input.getNodeId(),input.getResourceType(),input.getResourceName());
+        new PortMapping(db, mps, input.getNodeId());
+        DataBroker deviceDb = PortMapping.getDeviceDataBroker(input.getNodeId(), mps);
+        InstanceIdentifier<CurrentPmlist> currentPmsIID = InstanceIdentifier.create(CurrentPmlist.class);
+        ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
+        Optional<CurrentPmlist> currentPmList;
+        String methodName = "";
+        List<CurrentPm> currentPms = new ArrayList<>();
+        GetPmOutputBuilder pmOutputBuilder = new GetPmOutputBuilder();
+        List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.Measurements>
+            measrements = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418
+                .get.pm.output.Measurements>();
+        try {
+            currentPmList = rtx.read(LogicalDatastoreType.OPERATIONAL, currentPmsIID).get();
+            if (currentPmList.isPresent()) {
+                currentPms.addAll(currentPmList.get().getCurrentPm());
+                for (CurrentPm pm : currentPms) {
+                    MeasurementsBuilder measurement = new MeasurementsBuilder();
+                    if (pm.getResource().getResourceType().getType().toString().equals(input.getResourceType())) {
+
+                        switch (pm.getResource().getResourceType().getType()) {
+                            case CircuitPack:
+                                methodName = "getCircuitPackName";
+                                break;
+                            case Connection:
+                                methodName = "getConnectionNumber";
+                                break;
+                            case Degree:
+                                methodName = "getDegreeNumber";
+                                break;
+                            case Interface:
+                                methodName = "getInterfaceName";
+                                break;
+                            case InternalLink:
+                                methodName = "getInternalLinkName";
+                                break;
+                            case PhysicalLink:
+                                methodName = "getPhysicalLinkName";
+                                break;
+                            case Service:
+                                methodName = "getServiceName";
+                                break;
+                            case Shelf:
+                                methodName = "getShelfName";
+                                break;
+                            case SharedRiskGroup:
+                                methodName = "getSrgNumber";
+                                break;
+                            default:
+                                methodName = "getPort";
+                                break;
+                        }
+                        try {
+
+                            String pmResourceId = pm.getResource().getResource().getResource().getImplementedInterface()
+                                .getMethod(methodName).invoke(pm.getResource().getResource().getResource()).toString();
+                            if (pmResourceId.equals(input.getResourceName()) && pm.getGranularity() != null) {
+                                if (pm.getGranularity().getName().equals(input.getGranularity())) {
+                                    for (Measurements measure : pm.getMeasurements()) {
+                                        if (!measure.getMeasurement().getPmParameterName().getType().equals(
+                                            PmNamesEnum.VendorExtension)) {
+                                            measurement.setPmparameterName(measure.getMeasurement().getPmParameterName()
+                                                .getType().toString());
+                                            measurement.setPmparameterValue(measure.getMeasurement()
+                                                .getPmParameterValue().getDecimal64().toString());
+                                            measrements.add(measurement.build());
+                                        }
+                                    }
+                                }
+                            }
+
+                        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
+                            | NoSuchMethodException | SecurityException ex) {
+                            LOG.warn("Unable to find PM for NodeID: {} ResourceName: {} ",input.getNodeId(),
+                                    input.getResourceName(),ex);
+                        }
+                    }
+                }
+                if (measrements.size() > 0) {
+                    pmOutputBuilder.setNodeId(input.getNodeId()).setResourceType(input.getResourceType()).setResourceId(
+                        input.getResourceName()).setGranularity(input.getGranularity()).setMeasurements(measrements);
+                }
+
+            } else {
+                LOG.info("Device PM Data is not available");
+            }
+
+        } catch (InterruptedException | ExecutionException ex) {
+            LOG.warn("Unable to get currentPmList for NodeID: {}",input.getNodeId(),ex);
+        }
+        LOG.info("PM Data found successfully for {}-{}",pmOutputBuilder.getNodeId(),pmOutputBuilder.getResourceId());
+        return RpcResultBuilder.success(pmOutputBuilder).buildFuture();
+    }
+
+
+    /**
+     * This method is the implementation of the 'service-power-setup' RESTCONF service, which
+     * is one of the external APIs into the olm application.
+     *
+     * <p>
+     * 1. service-power-setup: This operation performs following steps:
+     *    Step1: Calculate Spanloss on all links which are part of service.
+     *    TODO Step2: Calculate power levels for each Tp-Id
+     *    TODO Step3: Post power values on roadm connections
+     *
+     * <p>
+     * The signature for this method was generated by yang tools from the
+     * renderer API model.
+     *
+     * @param input
+     *            Input parameter from the olm yang model
+     *            Input will contain nodeId and termination point
+     *
+     * @return Result of the request
+     */
+    @Override
+    public Future<org.opendaylight.yangtools.yang.common.RpcResult<ServicePowerSetupOutput>> servicePowerSetup(
+        ServicePowerSetupInput input) {
+        List<RoadmLinks> roadmLinks = new ArrayList<RoadmLinks>();
+        ServicePowerSetupOutputBuilder output = new ServicePowerSetupOutputBuilder();
+        //Finds degree TpID from node and generates a list of links
+        for (int i = 0; i < input.getNodes().size(); i++) {
+            if (input.getNodes().get(i).getDestTp().toLowerCase().contains("deg")) {
+                RoadmLinks rdmLink = new RoadmLinks();
+                rdmLink.setSrcNodeId(input.getNodes().get(i).getNodeId());
+                rdmLink.setSrcTpId(input.getNodes().get(i).getDestTp());
+                rdmLink.setDestNodeId(input.getNodes().get(i + 1).getNodeId());
+                rdmLink.setDestTpid(input.getNodes().get(i + 1).getSrcTp());
+                roadmLinks.add(rdmLink);
+            }
+        }
+        boolean successVal = new SpanLoss(db, mps).getLinkSpanloss(roadmLinks);
+
+        if (successVal) {
+            output.setResult("Success");
+        } else {
+            output.setResult("Failed");
+        }
+
+        return RpcResultBuilder.success(output).buildFuture();
+
+    }
+
+}
\ No newline at end of file