Fix few code issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / GnpyResult.java
1 /*
2  * Copyright © 2018 Orange, Inc. 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.transportpce.pce.gnpy;
10
11 import java.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.Result;
16 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.explicit.route.hop.type.NumUnnumHop;
17 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathMetric;
18 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathRouteObjects;
19 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.Response;
20 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.NoPathCase;
21 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.PathCase;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class for analyzing the result sent by GNPy.
32  *
33  * @author Ahmed Triki ( ahmed.triki@orange.com )
34  *
35  */
36
37 public class GnpyResult {
38
39     private static final Logger LOG = LoggerFactory.getLogger(GnpyResult.class);
40     private Response response = null;
41     private List<String> ordNodeList = new ArrayList<>();
42
43     public GnpyResult(Result result, GnpyTopoImpl gnpyTopo) throws GnpyException {
44         this.ordNodeList = gnpyTopo.getElementsList();
45         List<Response> responses = new ArrayList<>(result.nonnullResponse().values());
46         if (responses.isEmpty()) {
47             throw new GnpyException("In GnpyResult: the response from GNpy is null!");
48         }
49         LOG.info("The response id is {}; ", responses.get(0).getResponseId());
50         this.response = responses.get(0);
51         analyzeResult();
52     }
53
54     public boolean getPathFeasibility() {
55         boolean isFeasible = false;
56         if (response != null) {
57             if (response.getResponseType() instanceof NoPathCase) {
58                 LOG.info("In GnpyResult: The path is not feasible ");
59             } else if (response.getResponseType() instanceof PathCase) {
60                 isFeasible = true;
61                 LOG.info("In GnpyResult: The path is feasible ");
62             }
63         }
64         return isFeasible;
65     }
66
67     public List<PathRouteObjects> analyzeResult() {
68         List<PathRouteObjects> pathRouteObjectList = null;
69         if (response != null) {
70             if (response.getResponseType() instanceof NoPathCase) {
71                 NoPathCase noPathCase = (NoPathCase) response.getResponseType();
72                 String noPathType = noPathCase.getNoPath().getNoPath();
73                 LOG.info("GNPy: No path - {}", noPathType);
74                 if (((noPathType.equals("NO_FEASIBLE_BAUDRATE_WITH_SPACING"))
75                     && (noPathType.equals("NO_FEASIBLE_MODE"))) && ((noPathType.equals("MODE_NOT_FEASIBLE"))
76                         && (noPathType.equals("NO_SPECTRUM")))) {
77                     Collection<PathMetric> pathMetricList = noPathCase.getNoPath()
78                             .getPathProperties().nonnullPathMetric().values();
79                     LOG.info("GNPy : path is not feasible : {}", noPathType);
80                     for (PathMetric pathMetric : pathMetricList) {
81                         String metricType = pathMetric.getMetricType().getSimpleName();
82                         BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
83                         LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
84                     }
85                 }
86             } else if (response.getResponseType() instanceof PathCase) {
87                 LOG.info("GNPy : path is feasible");
88                 PathCase pathCase = (PathCase) response.getResponseType();
89                 Collection<PathMetric> pathMetricList = pathCase
90                         .getPathProperties().nonnullPathMetric().values();
91                 // Path metrics
92                 for (PathMetric pathMetric : pathMetricList) {
93                     String metricType = pathMetric.getMetricType().getSimpleName();
94                     BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
95                     LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
96                 }
97                 // Path route objects
98                 pathRouteObjectList = pathCase.getPathProperties().getPathRouteObjects();
99                 LOG.info("in GnpyResult: finishing the computation of pathRouteObjectList");
100             }
101         }
102         return pathRouteObjectList;
103     }
104
105     public HardConstraints computeHardConstraintsFromGnpyPath(List<PathRouteObjects> pathRouteObjectList) {
106         HardConstraints hardConstraints = null;
107         // Includes the list of nodes in the GNPy computed path as constraints
108         // for the PCE
109         List<NodeIdType> nodeIdList = new ArrayList<>();
110         for (PathRouteObjects pathRouteObjects : pathRouteObjectList) {
111             if (pathRouteObjects.getPathRouteObject().getType() instanceof NumUnnumHop) {
112                 NumUnnumHop numUnnumHop = (NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
113                 String nodeId = numUnnumHop.getNumUnnumHop().getNodeId();
114                 if (nodeId != null && this.ordNodeList.contains(nodeId)) {
115                     nodeIdList.add(new NodeIdType(nodeId));
116                 }
117             }
118         }
119
120         Include include = new IncludeBuilder().setNodeId(nodeIdList).build();
121         hardConstraints = new HardConstraintsBuilder().setInclude(include).build();
122         return hardConstraints;
123     }
124
125     public Response getResponse() {
126         return response;
127     }
128 }