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