Bump to Magnesium dependencies
[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(),ztoa.getZToAWavelengthNumber());
174
175         // Create the path request
176         List<PathRequest> pathRequestList = new ArrayList<>();
177         PathRequest pathRequestEl = new PathRequestBuilder().setRequestId(requestId)
178             .setSource(this.mapNodeRefIp.get(sourceNode)).setDestination(this.mapNodeRefIp.get(destNode))
179             .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes())
180             .setBidirectional(false).setPathConstraints(pathConstraints)
181             .setExplicitRouteObjects(explicitRouteObjects).build();
182         pathRequestList.add(pathRequestEl);
183         LOG.debug("In GnpyServiceImpl: path request ZToA is extracted");
184         return pathRequestList;
185     }
186
187     //Extract RouteObjectIncludeExclude list in the case of pre-computed path A-to-Z
188     private void extractRouteObjectIcludeAtoZ(List<AToZ> listAtoZ) throws GnpyException {
189         for (int i = 0; i < listAtoZ.size(); i++) {
190             createResource(listAtoZ.get(i).getResource().getResource());
191         }
192     }
193
194     //Extract RouteObjectIncludeExclude list in the case of pre-computed path Z-to-A
195     private void extractRouteObjectIcludeZtoA(List<ZToA> listZtoA) throws GnpyException {
196         for (int i = 0; i < listZtoA.size(); i++) {
197             createResource(listZtoA.get(i).getResource().getResource());
198         }
199     }
200
201     //Create a new resource node or link
202     private void createResource(@Nullable Resource resource) throws GnpyException {
203         if (resource
204             instanceof
205                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
206                     .pce.resource.resource.resource.Node) {
207             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
208                 .pce.resource.resource.resource.Node node =
209                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
210                     .pce.resource.resource.resource.Node) resource;
211             if (node.getNodeId() == null) {
212                 throw new GnpyException("In gnpyServiceImpl: nodeId is null");
213             }
214             addNodeToRouteObject(this.mapDisgNodeRefNode.get(node.getNodeId()));
215             return;
216         }
217
218         if (resource
219             instanceof
220                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
221                     .pce.resource.resource.resource.Link) {
222             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
223                 .pce.resource.resource.resource.Link link =
224                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017
225                     .pce.resource.resource.resource.Link) resource;
226             addLinkToRouteObject(link.getLinkId());
227             return;
228         }
229     }
230
231     //Create RouteObjectIncludeExclude list in the case of hard constraint
232     private void extractHardConstraints(PceConstraints pceHardConstraints) throws GnpyException {
233         List<String> listNodeToInclude = getListToInclude(pceHardConstraints);
234         if (listNodeToInclude != null) {
235             for (int i = 0; i < listNodeToInclude.size(); i++) {
236                 String nodeId = listNodeToInclude.get(i);
237                 addNodeToRouteObject(nodeId);
238             }
239         }
240     }
241
242     // Create the list of nodes to include
243     private List<String> getListToInclude(PceConstraints pceHardConstraints) {
244         List<String> listNodeToInclude = new ArrayList<>();
245         if (pceHardConstraints != null) {
246             List<ResourcePair> listToInclude = pceHardConstraints.getListToInclude();
247             Iterator<ResourcePair> it = listToInclude.iterator();
248             while (it.hasNext()) {
249                 ResourcePair rs = it.next();
250                 if (rs.getType().name().equals("NODE")) {
251                     listNodeToInclude.add(rs.getName());
252                 }
253             }
254         }
255         return listNodeToInclude;
256     }
257
258     //Add a node to the route object
259     private void addNodeToRouteObject(String nodeRef) throws GnpyException {
260         boolean found = false;
261         IpAddress ipAddress = this.mapNodeRefIp.get(nodeRef);
262         if (ipAddress == null) {
263             throw new GnpyException(String.format("In gnpyServiceImpl : NodeRef %s does not exist", nodeRef));
264         }
265
266         for (Elements element : this.elements) {
267             if (element.getUid().contains(ipAddress.getIpv4Address().getValue())) {
268                 if ((this.currentNodeIpAddress == null) || (this.currentNodeIpAddress != ipAddress)) {
269                     this.currentNodeIpAddress = ipAddress;
270                     RouteObjectIncludeExclude routeObjectIncludeExclude =
271                         addRouteObjectIncludeExclude(ipAddress, Uint32.valueOf(1));
272                     routeObjectIncludeExcludes.add(routeObjectIncludeExclude);
273                     index++;
274                     found = true;
275                 }
276                 return;
277             }
278         }
279         if (!found) {
280             throw new GnpyException(String.format("In gnpyServiceImpl : NodeRef %s does not exist",nodeRef));
281         }
282     }
283
284     //Add a link to the route object
285     private void addLinkToRouteObject(String linkId) throws GnpyException {
286         if (linkId == null) {
287             throw new GnpyException(String.format("In GnpyServiceImpl: the linkId does not exist"));
288         }
289         //Only the ROADM-to-ROADM link are included in the route object
290         if (!mapLinkFiber.containsKey(linkId)) {
291             return;
292         }
293         List<String> listSubLink = this.mapLinkFiber.get(linkId);
294         if (listSubLink == null) {
295             throw new GnpyException(String.format("In gnpyServiceImpl addNodeRouteObject : no sublink in %s",linkId));
296         }
297         for (String subLink : listSubLink) {
298             IpAddress fiberIp = this.mapFiberIp.get(subLink);
299             if (fiberIp == null) {
300                 throw new GnpyException(String.format("In gnpyServiceImpl addNodeRouteObject : fiberIp of %s is null",
301                     subLink));
302             }
303             RouteObjectIncludeExclude routeObjectIncludeExclude =
304                 addRouteObjectIncludeExclude(fiberIp, Uint32.valueOf(1));
305             routeObjectIncludeExcludes.add(routeObjectIncludeExclude);
306             index++;
307         }
308     }
309
310     // Add routeObjectIncludeExclude
311     private RouteObjectIncludeExclude addRouteObjectIncludeExclude(IpAddress ipAddress, Uint32 teTpValue) {
312         TeNodeId teNodeId = new TeNodeId(ipAddress);
313         TeTpId teTpId = new TeTpId(teTpValue);
314         NumUnnumHop numUnnumHop = new org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type.num
315             .unnum.hop.NumUnnumHopBuilder()
316                 .setNodeId(teNodeId.getIpv4Address().getValue())
317                 .setLinkTpId(teTpId.getUint32().toString())
318                 .setHopType(TeHopType.STRICT).build();
319         Type type1 = new NumUnnumHopBuilder().setNumUnnumHop(numUnnumHop).build();
320         // Create routeObjectIncludeExclude element
321         RouteObjectIncludeExclude routeObjectIncludeExclude = new RouteObjectIncludeExcludeBuilder()
322             .setIndex(this.index).setExplicitRouteUsage(RouteIncludeEro.class).setType(type1).build();
323         return routeObjectIncludeExclude;
324     }
325
326     //Create the path constraints
327     private PathConstraints createPathConstraints(Long rate, Long wavelengthNumber) {
328         // Create EffectiveFreqSlot
329         int freqNdex = 0;
330         if (wavelengthNumber != null) {
331             double freq = (MAX_CENTRAL_FREQ - FIX_CH * (wavelengthNumber - 1));
332             freqNdex = (int) Math.round((freq - FLEX_CENTRAL_FREQ) / SLOT_BW);
333         }
334         List<EffectiveFreqSlot> effectiveFreqSlot = new ArrayList<>();
335         EffectiveFreqSlot effectiveFreqSlot1 = new EffectiveFreqSlotBuilder().setM(NB_SLOT_BW).setN(freqNdex).build();
336         effectiveFreqSlot.add(effectiveFreqSlot1);
337         // Create Te-Bandwidth
338         TeBandwidth teBandwidth = new TeBandwidthBuilder().setPathBandwidth(new BigDecimal(rate))
339             .setTechnology("flexi-grid").setTrxType("openroadm-beta1")
340             .setTrxMode("W100G").setEffectiveFreqSlot(effectiveFreqSlot)
341             .setSpacing(new BigDecimal(FIX_CH * CONVERT_TH_HZ)).build();
342         PathConstraints pathConstraints = new PathConstraintsBuilder().setTeBandwidth(teBandwidth).build();
343         return pathConstraints;
344     }
345
346     //Create the synchronization
347     private List<Synchronization> extractSynchronization(Uint32 requestId) {
348         // Create RequestIdNumber
349         List<Uint32> requestIdNumber = new ArrayList<>();
350         requestIdNumber.add(requestId);
351         // Create a synchronization
352         Svec svec = new SvecBuilder().setRelaxable(true)
353             .setDisjointness(new TePathDisjointness(true, true, false))
354             .setRequestIdNumber(requestIdNumber).build();
355         List<Synchronization> synchro = new ArrayList<>();
356         Synchronization synchronization1 = new SynchronizationBuilder().setSynchronizationId(Long.valueOf(0))
357                 .setSvec(svec).build();
358         synchro.add(synchronization1);
359         return (synchro);
360     }
361
362     public List<PathRequest> getPathRequest() {
363         return pathRequest;
364     }
365
366     public void setPathRequest(List<PathRequest> pathRequest) {
367         this.pathRequest = pathRequest;
368     }
369
370     public List<Synchronization> getSynchronization() {
371         return synchronization;
372     }
373
374     public void setSynchronization(List<Synchronization> synchronization) {
375         this.synchronization = synchronization;
376     }
377
378 }