Fix issues related to Magnesium bump
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / GnpyServiceImpl.java
1 /*
2  * Copyright © 2019 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.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
20 import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
21 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Elements;
22 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.RouteIncludeEro;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.TeHopType;
24 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.TeNodeId;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.TePathDisjointness;
26 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.TeTpId;
27 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.common.constraints_config.TeBandwidth;
28 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.common.constraints_config.TeBandwidthBuilder;
29 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.Type;
30 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type.NumUnnumHopBuilder;
31 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type.num.unnum.hop.NumUnnumHop;
32 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.constraints.PathConstraints;
33 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.constraints.PathConstraintsBuilder;
34 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.gnpy.specific.parameters.EffectiveFreqSlot;
35 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.gnpy.specific.parameters.EffectiveFreqSlotBuilder;
36 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.path.route.objects.ExplicitRouteObjects;
37 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.path.route.objects.ExplicitRouteObjectsBuilder;
38 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.path.route.objects.explicit.route.objects.RouteObjectIncludeExclude;
39 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.path.route.objects.explicit.route.objects.RouteObjectIncludeExcludeBuilder;
40 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.service.PathRequest;
41 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.service.PathRequestBuilder;
42 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.Synchronization;
43 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.SynchronizationBuilder;
44 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.synchronization.Svec;
45 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.synchronization.info.synchronization.SvecBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
50 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
51 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.Resource;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Class to create the service corresponding to GNPy requirements.
59  *
60  * @author Ahmed Triki ( ahmed.triki@orange.com )
61  *
62  */
63
64 public class GnpyServiceImpl {
65     private static final Logger LOG = LoggerFactory.getLogger(GnpyServiceImpl.class);
66     private static final double FIX_CH = 0.05; //Fix-grid channel width (THz)
67     private static final int NB_SLOT_BW = 4; //Number of slot in 50GHz channel (THz)
68     private static final double SLOT_BW = 0.00625; //Nominal central frequency granularity (THz)
69     private static final double MAX_CENTRAL_FREQ = 196.1; //Minimum channel center frequency (openRoadm spec) (THz)
70     private static final double FLEX_CENTRAL_FREQ = 193.1; //Flex-grid reference channel frequency (THz)
71     private static final double CONVERT_TH_HZ = 1e12; //Convert THz to Hz
72
73     private List<PathRequest> pathRequest = new ArrayList<>();
74     private List<Synchronization> synchronization = new ArrayList<>();
75     private Map<String, String> mapDisgNodeRefNode = new HashMap<String, String>();
76     private Map<String, IpAddress> mapNodeRefIp = new HashMap<String, IpAddress>();
77     private Map<String, List<String>> mapLinkFiber = new HashMap<String, List<String>>();
78     private Map<String, IpAddress> mapFiberIp = new HashMap<String, IpAddress>();
79     private List<String> trxList = new ArrayList<>();
80     private List<Elements> elements = new ArrayList<>();
81     private List<RouteObjectIncludeExclude> routeObjectIncludeExcludes = new ArrayList<>();
82     private Long index = (long) 0; //index of the element in the path
83     private IpAddress currentNodeIpAddress = null;
84
85     /*
86      * Construct the GnpyServiceImpl
87      */
88     public GnpyServiceImpl(PathComputationRequestInput input, AToZDirection atoz, Uint32 requestId,
89                 GnpyTopoImpl gnpyTopo, PceConstraints pceHardConstraints) throws GnpyException {
90         this.elements = gnpyTopo.getElements();
91         this.mapDisgNodeRefNode = gnpyTopo.getMapDisgNodeRefNode();
92         this.mapNodeRefIp = gnpyTopo.getMapNodeRefIp();
93         this.mapLinkFiber = gnpyTopo.getMapLinkFiber();
94         this.mapFiberIp = gnpyTopo.getMapFiberIp();
95         this.trxList = gnpyTopo.getTrxList();
96         try {
97             this.pathRequest = extractPathRequest(input, atoz, requestId.toJava(), pceHardConstraints);
98             this.synchronization = extractSynchronization(requestId);
99         } catch (NullPointerException e) {
100             throw new GnpyException("In GnpyServiceImpl: one of the elements is null",e);
101         }
102     }
103
104     public GnpyServiceImpl(PathComputationRequestInput input, ZToADirection ztoa, Uint32 requestId,
105                 GnpyTopoImpl gnpyTopo, PceConstraints pceHardConstraints) throws GnpyException {
106         this.elements = gnpyTopo.getElements();
107         this.mapDisgNodeRefNode = gnpyTopo.getMapDisgNodeRefNode();
108         this.mapNodeRefIp = gnpyTopo.getMapNodeRefIp();
109         this.mapLinkFiber = gnpyTopo.getMapLinkFiber();
110         this.mapFiberIp = gnpyTopo.getMapFiberIp();
111         this.trxList = gnpyTopo.getTrxList();
112         try {
113             pathRequest = extractPathRequest(input, ztoa, requestId.toJava(), pceHardConstraints);
114             synchronization = extractSynchronization(requestId);
115         } catch (NullPointerException e) {
116             throw new GnpyException("In GnpyServiceImpl: one of the elements of service is null",e);
117         }
118     }
119
120     private List<PathRequest> extractPathRequest(PathComputationRequestInput input, AToZDirection atoz, Long requestId,
121         PceConstraints pceHardConstraints) throws GnpyException {
122
123         // Create the source and destination nodes
124         String sourceNode = input.getServiceAEnd().getNodeId();
125         String destNode = input.getServiceZEnd().getNodeId();
126         if (!trxList.contains(sourceNode) || !trxList.contains(destNode)) {
127             throw new GnpyException("In GnpyServiceImpl: source and destination should be transmitter nodes");
128         }
129
130         // Create explicitRouteObjects
131         List<AToZ> listAtoZ = atoz.getAToZ();
132         if (listAtoZ != null) {
133             extractRouteObjectIcludeAtoZ(listAtoZ);
134         } else {
135             extractHardConstraints(pceHardConstraints);
136         }
137         ExplicitRouteObjects explicitRouteObjects = new ExplicitRouteObjectsBuilder()
138             .setRouteObjectIncludeExclude(routeObjectIncludeExcludes).build();
139         //Create Path Constraint
140         PathConstraints pathConstraints =
141             createPathConstraints(atoz.getRate().toJava(),atoz.getAToZWavelengthNumber().toJava());
142
143         // Create the path request
144         List<PathRequest> pathRequestList = new ArrayList<>();
145         PathRequest pathRequestEl = new PathRequestBuilder().setRequestId(requestId)
146             .setSource(this.mapNodeRefIp.get(sourceNode)).setDestination(this.mapNodeRefIp.get(destNode))
147             .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes())
148             .setBidirectional(false).setPathConstraints(pathConstraints).setPathConstraints(pathConstraints)
149             .setExplicitRouteObjects(explicitRouteObjects).build();
150         pathRequestList.add(pathRequestEl);
151         LOG.debug("In GnpyServiceImpl: path request AToZ is extracted");
152         return pathRequestList;
153     }
154
155     private List<PathRequest> extractPathRequest(PathComputationRequestInput input, ZToADirection ztoa, Long requestId,
156         PceConstraints pceHardConstraints) throws GnpyException {
157         // Create the source and destination nodes
158         String sourceNode = input.getServiceZEnd().getNodeId();
159         String destNode = input.getServiceAEnd().getNodeId();
160         if (!trxList.contains(sourceNode) || !trxList.contains(destNode)) {
161             throw new GnpyException("In GnpyServiceImpl: source and destination should be transmitter nodes");
162         }
163         // Create explicitRouteObjects
164         List<ZToA> listZtoA = ztoa.getZToA();
165         if (listZtoA != null) {
166             extractRouteObjectIcludeZtoA(listZtoA);
167         } else {
168             extractHardConstraints(pceHardConstraints);
169         }
170         ExplicitRouteObjects explicitRouteObjects = new ExplicitRouteObjectsBuilder()
171             .setRouteObjectIncludeExclude(routeObjectIncludeExcludes).build();
172         //Create Path Constraint
173         PathConstraints pathConstraints = createPathConstraints(ztoa.getRate().toJava(),
174             ztoa.getZToAWavelengthNumber().toJava());
175
176         // Create the path request
177         List<PathRequest> pathRequestList = new ArrayList<>();
178         PathRequest pathRequestEl = new PathRequestBuilder().setRequestId(requestId)
179             .setSource(this.mapNodeRefIp.get(sourceNode)).setDestination(this.mapNodeRefIp.get(destNode))
180             .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes())
181             .setBidirectional(false).setPathConstraints(pathConstraints)
182             .setExplicitRouteObjects(explicitRouteObjects).build();
183         pathRequestList.add(pathRequestEl);
184         LOG.debug("In GnpyServiceImpl: path request ZToA is extracted");
185         return pathRequestList;
186     }
187
188     //Extract RouteObjectIncludeExclude list in the case of pre-computed path A-to-Z
189     private void extractRouteObjectIcludeAtoZ(List<AToZ> listAtoZ) throws GnpyException {
190         for (int i = 0; i < listAtoZ.size(); i++) {
191             createResource(listAtoZ.get(i).getResource().getResource());
192         }
193     }
194
195     //Extract RouteObjectIncludeExclude list in the case of pre-computed path Z-to-A
196     private void extractRouteObjectIcludeZtoA(List<ZToA> listZtoA) throws GnpyException {
197         for (int i = 0; i < listZtoA.size(); i++) {
198             createResource(listZtoA.get(i).getResource().getResource());
199         }
200     }
201
202     //Create a new resource node or link
203     private void createResource(@Nullable Resource resource) throws GnpyException {
204         if (resource
205             instanceof
206                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
207                     .pce.resource.resource.resource.Node) {
208             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
209                 .pce.resource.resource.resource.Node node =
210                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
211                     .pce.resource.resource.resource.Node) resource;
212             if (node.getNodeId() == null) {
213                 throw new GnpyException("In gnpyServiceImpl: nodeId is null");
214             }
215             addNodeToRouteObject(this.mapDisgNodeRefNode.get(node.getNodeId()));
216             return;
217         }
218
219         if (resource
220             instanceof
221                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
222                     .pce.resource.resource.resource.Link) {
223             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
224                 .pce.resource.resource.resource.Link link =
225                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
226                     .pce.resource.resource.resource.Link) resource;
227             addLinkToRouteObject(link.getLinkId());
228             return;
229         }
230     }
231
232     //Create RouteObjectIncludeExclude list in the case of hard constraint
233     private void extractHardConstraints(PceConstraints pceHardConstraints) throws GnpyException {
234         List<String> listNodeToInclude = getListToInclude(pceHardConstraints);
235         if (listNodeToInclude != null) {
236             for (int i = 0; i < listNodeToInclude.size(); i++) {
237                 String nodeId = listNodeToInclude.get(i);
238                 addNodeToRouteObject(nodeId);
239             }
240         }
241     }
242
243     // Create the list of nodes to include
244     private List<String> getListToInclude(PceConstraints pceHardConstraints) {
245         List<String> listNodeToInclude = new ArrayList<>();
246         if (pceHardConstraints != null) {
247             List<ResourcePair> listToInclude = pceHardConstraints.getListToInclude();
248             Iterator<ResourcePair> it = listToInclude.iterator();
249             while (it.hasNext()) {
250                 ResourcePair rs = it.next();
251                 if (rs.getType().name().equals("NODE")) {
252                     listNodeToInclude.add(rs.getName());
253                 }
254             }
255         }
256         return listNodeToInclude;
257     }
258
259     //Add a node to the route object
260     private void addNodeToRouteObject(String nodeRef) throws GnpyException {
261         boolean found = false;
262         IpAddress ipAddress = this.mapNodeRefIp.get(nodeRef);
263         if (ipAddress == null) {
264             throw new GnpyException(String.format("In gnpyServiceImpl : NodeRef %s does not exist", nodeRef));
265         }
266
267         for (Elements element : this.elements) {
268             if (element.getUid().contains(ipAddress.getIpv4Address().getValue())) {
269                 if ((this.currentNodeIpAddress == null) || (this.currentNodeIpAddress != ipAddress)) {
270                     this.currentNodeIpAddress = ipAddress;
271                     RouteObjectIncludeExclude routeObjectIncludeExclude =
272                         addRouteObjectIncludeExclude(ipAddress, Uint32.valueOf(1));
273                     routeObjectIncludeExcludes.add(routeObjectIncludeExclude);
274                     index++;
275                     found = true;
276                 }
277                 return;
278             }
279         }
280         if (!found) {
281             throw new GnpyException(String.format("In gnpyServiceImpl : NodeRef %s does not exist",nodeRef));
282         }
283     }
284
285     //Add a link to the route object
286     private void addLinkToRouteObject(String linkId) throws GnpyException {
287         if (linkId == null) {
288             throw new GnpyException(String.format("In GnpyServiceImpl: the linkId does not exist"));
289         }
290         //Only the ROADM-to-ROADM link are included in the route object
291         if (!mapLinkFiber.containsKey(linkId)) {
292             return;
293         }
294         List<String> listSubLink = this.mapLinkFiber.get(linkId);
295         if (listSubLink == null) {
296             throw new GnpyException(String.format("In gnpyServiceImpl addNodeRouteObject : no sublink in %s",linkId));
297         }
298         for (String subLink : listSubLink) {
299             IpAddress fiberIp = this.mapFiberIp.get(subLink);
300             if (fiberIp == null) {
301                 throw new GnpyException(String.format("In gnpyServiceImpl addNodeRouteObject : fiberIp of %s is null",
302                     subLink));
303             }
304             RouteObjectIncludeExclude routeObjectIncludeExclude =
305                 addRouteObjectIncludeExclude(fiberIp, Uint32.valueOf(1));
306             routeObjectIncludeExcludes.add(routeObjectIncludeExclude);
307             index++;
308         }
309     }
310
311     // Add routeObjectIncludeExclude
312     private RouteObjectIncludeExclude addRouteObjectIncludeExclude(IpAddress ipAddress, Uint32 teTpValue) {
313         TeNodeId teNodeId = new TeNodeId(ipAddress);
314         TeTpId teTpId = new TeTpId(teTpValue);
315         NumUnnumHop numUnnumHop = new org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type.num
316             .unnum.hop.NumUnnumHopBuilder()
317                 .setNodeId(teNodeId.getIpv4Address().getValue())
318                 .setLinkTpId(teTpId.getUint32().toString())
319                 .setHopType(TeHopType.STRICT).build();
320         Type type1 = new NumUnnumHopBuilder().setNumUnnumHop(numUnnumHop).build();
321         // Create routeObjectIncludeExclude element
322         RouteObjectIncludeExclude routeObjectIncludeExclude = new RouteObjectIncludeExcludeBuilder()
323             .setIndex(this.index).setExplicitRouteUsage(RouteIncludeEro.class).setType(type1).build();
324         return routeObjectIncludeExclude;
325     }
326
327     //Create the path constraints
328     private PathConstraints createPathConstraints(Long rate, Long wavelengthNumber) {
329         // Create EffectiveFreqSlot
330         int freqNdex = 0;
331         if (wavelengthNumber != null) {
332             double freq = (MAX_CENTRAL_FREQ - FIX_CH * (wavelengthNumber - 1));
333             freqNdex = (int) Math.round((freq - FLEX_CENTRAL_FREQ) / SLOT_BW);
334         }
335         List<EffectiveFreqSlot> effectiveFreqSlot = new ArrayList<>();
336         EffectiveFreqSlot effectiveFreqSlot1 = new EffectiveFreqSlotBuilder().setM(NB_SLOT_BW).setN(freqNdex).build();
337         effectiveFreqSlot.add(effectiveFreqSlot1);
338         // Create Te-Bandwidth
339         TeBandwidth teBandwidth = new TeBandwidthBuilder().setPathBandwidth(new BigDecimal(rate))
340             .setTechnology("flexi-grid").setTrxType("openroadm-beta1")
341             .setTrxMode("W100G").setEffectiveFreqSlot(effectiveFreqSlot)
342             .setSpacing(new BigDecimal(FIX_CH * CONVERT_TH_HZ)).build();
343         PathConstraints pathConstraints = new PathConstraintsBuilder().setTeBandwidth(teBandwidth).build();
344         return pathConstraints;
345     }
346
347     //Create the synchronization
348     private List<Synchronization> extractSynchronization(Uint32 requestId) {
349         // Create RequestIdNumber
350         List<Uint32> requestIdNumber = new ArrayList<>();
351         requestIdNumber.add(requestId);
352         // Create a synchronization
353         Svec svec = new SvecBuilder().setRelaxable(true)
354             .setDisjointness(new TePathDisjointness(true, true, false))
355             .setRequestIdNumber(requestIdNumber).build();
356         List<Synchronization> synchro = new ArrayList<>();
357         Synchronization synchronization1 = new SynchronizationBuilder().setSynchronizationId(Long.valueOf(0))
358                 .setSvec(svec).build();
359         synchro.add(synchronization1);
360         return (synchro);
361     }
362
363     public List<PathRequest> getPathRequest() {
364         return pathRequest;
365     }
366
367     public void setPathRequest(List<PathRequest> pathRequest) {
368         this.pathRequest = pathRequest;
369     }
370
371     public List<Synchronization> getSynchronization() {
372         return synchronization;
373     }
374
375     public void setSynchronization(List<Synchronization> synchronization) {
376         this.synchronization = synchronization;
377     }
378
379 }