Use version 13.1.0 of openroadm-service models
[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.List;
14 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result;
15 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.explicit.route.hop.type.NumUnnumHop;
16 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.generic.path.properties.path.properties.PathMetric;
17 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.generic.path.properties.path.properties.PathRouteObjects;
18 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.result.Response;
19 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.result.response.response.type.NoPathCase;
20 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.result.response.response.type.PathCase;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev221209.constraints.Include;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev221209.constraints.IncludeBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev221209.routing.constraints.HardConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev221209.routing.constraints.HardConstraintsBuilder;
26 import org.opendaylight.yangtools.yang.common.Decimal64;
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     }
52
53     public boolean getPathFeasibility() {
54         boolean isFeasible = false;
55         if (response != null) {
56             if (response.getResponseType() instanceof NoPathCase) {
57                 LOG.info("In GnpyResult: The path is not feasible ");
58             } else if (response.getResponseType() instanceof PathCase) {
59                 isFeasible = true;
60                 LOG.info("In GnpyResult: The path is feasible ");
61             }
62         }
63         return isFeasible;
64     }
65
66     public List<PathRouteObjects> analyzeResult() {
67         List<PathRouteObjects> pathRouteObjectList = null;
68         if (response != null) {
69             if (response.getResponseType() instanceof NoPathCase) {
70                 NoPathCase noPathCase = (NoPathCase) response.getResponseType();
71                 String noPathType = noPathCase.getNoPath().getNoPath();
72                 LOG.info("GNPy: No path - {}", noPathType);
73                 if (((noPathType.equals("NO_FEASIBLE_BAUDRATE_WITH_SPACING"))
74                     && (noPathType.equals("NO_FEASIBLE_MODE"))) && ((noPathType.equals("MODE_NOT_FEASIBLE"))
75                         && (noPathType.equals("NO_SPECTRUM")))) {
76                     Collection<PathMetric> pathMetricList = noPathCase.getNoPath()
77                             .getPathProperties().nonnullPathMetric().values();
78                     LOG.info("GNPy : path is not feasible : {}", noPathType);
79                     for (PathMetric pathMetric : pathMetricList) {
80                         String metricType = pathMetric.getMetricType().toString();
81                         Decimal64 accumulativeValue = pathMetric.getAccumulativeValue();
82                         LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
83                     }
84                 }
85             } else if (response.getResponseType() instanceof PathCase) {
86                 LOG.info("GNPy : path is feasible");
87                 PathCase pathCase = (PathCase) response.getResponseType();
88                 Collection<PathMetric> pathMetricList = pathCase
89                         .getPathProperties().nonnullPathMetric().values();
90                 // Path metrics
91                 for (PathMetric pathMetric : pathMetricList) {
92                     String metricType = pathMetric.getMetricType().toString();
93                     Decimal64 accumulativeValue = pathMetric.getAccumulativeValue();
94                     LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
95                 }
96                 // Path route objects
97                 pathRouteObjectList = pathCase.getPathProperties().getPathRouteObjects();
98                 LOG.info("in GnpyResult: finishing the computation of pathRouteObjectList");
99             }
100         }
101         return pathRouteObjectList;
102     }
103
104     public HardConstraints computeHardConstraintsFromGnpyPath(List<PathRouteObjects> pathRouteObjectList) {
105         HardConstraints hardConstraints = null;
106         // Includes the list of nodes in the GNPy computed path as constraints
107         // for the PCE
108         List<NodeIdType> nodeIdList = new ArrayList<>();
109         for (PathRouteObjects pathRouteObjects : pathRouteObjectList) {
110             if (pathRouteObjects.getPathRouteObject().getType() instanceof NumUnnumHop) {
111                 NumUnnumHop numUnnumHop = (NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
112                 String nodeId = numUnnumHop.getNumUnnumHop().getNodeId();
113                 if (nodeId != null && this.ordNodeList.contains(nodeId)) {
114                     nodeIdList.add(new NodeIdType(nodeId));
115                 }
116             }
117         }
118
119         Include include = new IncludeBuilder().setNodeId(nodeIdList).build();
120         hardConstraints = new HardConstraintsBuilder().setInclude(include).build();
121         return hardConstraints;
122     }
123
124     public Response getResponse() {
125         return response;
126     }
127 }