768760250671dd45dea91bf5a634ffb3238f60ba
[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 java.util.stream.Collectors;
16 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.Result;
17 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.explicit.route.hop.type.NumUnnumHop;
18 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathMetric;
19 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathRouteObjects;
20 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.Response;
21 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.NoPathCase;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.PathCase;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.General;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.GeneralBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.Include;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.IncludeBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.include_.OrderedHopsBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.HopType;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.HopTypeBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.hop.type.hop.type.NodeBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.HardConstraints;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.HardConstraintsBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint16;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Class for analyzing the result sent by GNPy.
40  *
41  * @author Ahmed Triki ( ahmed.triki@orange.com )
42  *
43  */
44
45 public class GnpyResult {
46
47     private static final Logger LOG = LoggerFactory.getLogger(GnpyResult.class);
48     private Response response = null;
49     private List<String> ordNodeList = new ArrayList<>();
50
51     public GnpyResult(Result result, GnpyTopoImpl gnpyTopo) throws GnpyException {
52         this.ordNodeList = gnpyTopo.getElementsList();
53         List<Response> responses = new ArrayList<>(result.nonnullResponse().values());
54         if (responses.isEmpty()) {
55             throw new GnpyException("In GnpyResult: the response from GNpy is null!");
56         }
57         LOG.info("The response id is {}; ", responses.get(0).getResponseId());
58         this.response = responses.get(0);
59         analyzeResult();
60     }
61
62     public boolean getPathFeasibility() {
63         boolean isFeasible = false;
64         if (response != null) {
65             if (response.getResponseType() instanceof NoPathCase) {
66                 LOG.info("In GnpyResult: The path is not feasible ");
67             } else if (response.getResponseType() instanceof PathCase) {
68                 isFeasible = true;
69                 LOG.info("In GnpyResult: The path is feasible ");
70             }
71         }
72         return isFeasible;
73     }
74
75     public List<PathRouteObjects> analyzeResult() {
76         List<PathRouteObjects> pathRouteObjectList = null;
77         if (response != null) {
78             if (response.getResponseType() instanceof NoPathCase) {
79                 NoPathCase noPathCase = (NoPathCase) response.getResponseType();
80                 String noPathType = noPathCase.getNoPath().getNoPath();
81                 LOG.info("GNPy: No path - {}", noPathType);
82                 if (((noPathType.equals("NO_FEASIBLE_BAUDRATE_WITH_SPACING"))
83                     && (noPathType.equals("NO_FEASIBLE_MODE"))) && ((noPathType.equals("MODE_NOT_FEASIBLE"))
84                         && (noPathType.equals("NO_SPECTRUM")))) {
85                     Collection<PathMetric> pathMetricList = noPathCase.getNoPath()
86                             .getPathProperties().nonnullPathMetric().values();
87                     LOG.info("GNPy : path is not feasible : {}", noPathType);
88                     for (PathMetric pathMetric : pathMetricList) {
89                         String metricType = pathMetric.getMetricType().getSimpleName();
90                         BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
91                         LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
92                     }
93                 }
94             } else if (response.getResponseType() instanceof PathCase) {
95                 LOG.info("GNPy : path is feasible");
96                 PathCase pathCase = (PathCase) response.getResponseType();
97                 Collection<PathMetric> pathMetricList = pathCase
98                         .getPathProperties().nonnullPathMetric().values();
99                 // Path metrics
100                 for (PathMetric pathMetric : pathMetricList) {
101                     String metricType = pathMetric.getMetricType().getSimpleName();
102                     BigDecimal accumulativeValue = pathMetric.getAccumulativeValue();
103                     LOG.info("Metric type {} // AccumulatriveValue {}", metricType, accumulativeValue);
104                 }
105                 // Path route objects
106                 pathRouteObjectList = pathCase.getPathProperties().getPathRouteObjects();
107                 LOG.info("in GnpyResult: finishing the computation of pathRouteObjectList");
108             }
109         }
110         return pathRouteObjectList;
111     }
112
113     public HardConstraints computeHardConstraintsFromGnpyPath(List<PathRouteObjects> pathRouteObjectList) {
114         HardConstraints hardConstraints = null;
115         // Includes the list of nodes in the GNPy computed path as constraints
116         // for the PCE
117         List<OrderedHops> orderedHopsList = new ArrayList<>();
118         int counter = 0;
119         for (PathRouteObjects pathRouteObjects : pathRouteObjectList) {
120             if (pathRouteObjects.getPathRouteObject().getType() instanceof NumUnnumHop) {
121                 NumUnnumHop numUnnumHop = (NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
122                 String nodeId = numUnnumHop.getNumUnnumHop().getNodeId();
123                 if (nodeId != null && this.ordNodeList.contains(nodeId)) {
124                     org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118
125                         .ordered.constraints.sp.hop.type.hop.type.Node node = new NodeBuilder().setNodeId(nodeId)
126                         .build();
127                     HopType hopType = new HopTypeBuilder().setHopType(node).build();
128                     OrderedHops orderedHops = new OrderedHopsBuilder()
129                             .setHopNumber(Uint16.valueOf(counter)).setHopType(hopType)
130                         .build();
131                     orderedHopsList.add(orderedHops);
132                     counter++;
133                 }
134             }
135         }
136         Include include = new IncludeBuilder()
137                 .setOrderedHops(orderedHopsList.stream()
138                         .collect(Collectors.toMap(OrderedHops::key, orderedHops -> orderedHops)))
139                 .build();
140         General general = new GeneralBuilder().setInclude(include).build();
141         hardConstraints = new HardConstraintsBuilder().setCoRoutingOrGeneral(general).build();
142         return hardConstraints;
143     }
144
145     public Response getResponse() {
146         return response;
147     }
148 }