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