Remove transportpce-routing-constraint model
[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.rev181130.NodeIdType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.General;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.GeneralBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Include;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.IncludeBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraints;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraintsBuilder;
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         analyzeResult();
54     }
55
56     public boolean getPathFeasibility() {
57         boolean isFeasible = false;
58         if (response != null) {
59             if (response.getResponseType() instanceof NoPathCase) {
60                 LOG.info("In GnpyResult: The path is not feasible ");
61             } else if (response.getResponseType() instanceof PathCase) {
62                 isFeasible = true;
63                 LOG.info("In GnpyResult: The path is feasible ");
64             }
65         }
66         return isFeasible;
67     }
68
69     public List<PathRouteObjects> analyzeResult() {
70         List<PathRouteObjects> pathRouteObjectList = null;
71         if (response != null) {
72             if (response.getResponseType() instanceof NoPathCase) {
73                 NoPathCase noPathCase = (NoPathCase) response.getResponseType();
74                 String noPathType = noPathCase.getNoPath().getNoPath();
75                 LOG.info("GNPy: No path - {}", noPathType);
76                 if (((noPathType.equals("NO_FEASIBLE_BAUDRATE_WITH_SPACING"))
77                     && (noPathType.equals("NO_FEASIBLE_MODE"))) && ((noPathType.equals("MODE_NOT_FEASIBLE"))
78                         && (noPathType.equals("NO_SPECTRUM")))) {
79                     Collection<PathMetric> pathMetricList = noPathCase.getNoPath()
80                             .getPathProperties().nonnullPathMetric().values();
81                     LOG.info("GNPy : path is not feasible : {}", noPathType);
82                     for (PathMetric pathMetric : pathMetricList) {
83                         String metricType = pathMetric.getMetricType().getSimpleName();
84                         BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
85                         LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
86                     }
87                 }
88             } else if (response.getResponseType() instanceof PathCase) {
89                 LOG.info("GNPy : path is feasible");
90                 PathCase pathCase = (PathCase) response.getResponseType();
91                 Collection<PathMetric> pathMetricList = pathCase
92                         .getPathProperties().nonnullPathMetric().values();
93                 // Path metrics
94                 for (PathMetric pathMetric : pathMetricList) {
95                     String metricType = pathMetric.getMetricType().getSimpleName();
96                     BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
97                     LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
98                 }
99                 // Path route objects
100                 pathRouteObjectList = pathCase.getPathProperties().getPathRouteObjects();
101                 LOG.info("in GnpyResult: finishing the computation of pathRouteObjectList");
102             }
103         }
104         return pathRouteObjectList;
105     }
106
107     public HardConstraints computeHardConstraintsFromGnpyPath(List<PathRouteObjects> pathRouteObjectList) {
108         HardConstraints hardConstraints = null;
109         // Includes the list of nodes in the GNPy computed path as constraints
110         // for the PCE
111         List<NodeIdType> nodeIdList = new ArrayList<>();
112         for (PathRouteObjects pathRouteObjects : pathRouteObjectList) {
113             if (pathRouteObjects.getPathRouteObject().getType() instanceof NumUnnumHop) {
114                 NumUnnumHop numUnnumHop = (NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
115                 String nodeId = numUnnumHop.getNumUnnumHop().getNodeId();
116                 if (nodeId != null && this.ordNodeList.contains(nodeId)) {
117                     nodeIdList.add(new NodeIdType(nodeId));
118                 }
119             }
120         }
121
122         Include include = new IncludeBuilder().setNodeId(nodeIdList).build();
123         General general = new GeneralBuilder().setInclude(include).build();
124         hardConstraints = new HardConstraintsBuilder().setCoRoutingOrGeneral(general).build();
125         return hardConstraints;
126     }
127
128     public Response getResponse() {
129         return response;
130     }
131 }