Use version 13.1.0 of openroadm-service models
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PcePathDescription.java
1 /*
2  * Copyright © 2017 AT&T, 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 package org.opendaylight.transportpce.pce;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import org.opendaylight.transportpce.common.ResponseCodes;
15 import org.opendaylight.transportpce.common.StringConstants;
16 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
17 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
18 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
19 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev230526.FrequencyTHz;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev210924.OpucnTribSlotDef;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.AToZDirectionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ZToADirectionBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZ;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZKey;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ztoa.direction.ZToA;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ztoa.direction.ZToABuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ztoa.direction.ZToAKey;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.Resource;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.ResourceBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.LinkBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.NodeBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.TerminationPoint;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.TerminationPointBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
39 import org.opendaylight.yangtools.yang.common.Decimal64;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class PcePathDescription {
45     /* Logging. */
46     private static final Logger LOG = LoggerFactory.getLogger(PcePathDescription.class);
47
48     private List<PceLink> pathAtoZ = null;
49     private PceResult rc;
50     private Map<LinkId, PceLink> allPceLinks = null;
51
52     public PcePathDescription(List<PceLink> pathAtoZ, Map<LinkId, PceLink> allPceLinks, PceResult rc) {
53         super();
54         this.allPceLinks = allPceLinks;
55         this.pathAtoZ = pathAtoZ;
56         this.rc = rc;
57     }
58
59     public PceResult buildDescriptions() {
60         LOG.info("In buildDescriptions: AtoZ =  {}", pathAtoZ);
61         Map<AToZKey,AToZ> atozMap = new HashMap<>();
62         if (pathAtoZ == null) {
63             rc.setRC(ResponseCodes.RESPONSE_FAILED);
64             LOG.error("In buildDescriptions: there is empty AtoZ path");
65             return rc;
66         }
67
68         buildAtoZ(atozMap, pathAtoZ);
69         rc.setAtoZDirection(buildAtoZDirection(atozMap).build());
70         List<PceLink> pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
71         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
72
73         Map<ZToAKey,ZToA> ztoaMap = new HashMap<>();
74         if (pathZtoA == null) {
75             rc.setRC(ResponseCodes.RESPONSE_FAILED);
76             LOG.error("In buildDescriptions: there is empty ZtoA path");
77             return rc;
78         }
79         buildZtoA(ztoaMap, pathZtoA);
80         rc.setZtoADirection(buildZtoADirection(ztoaMap).build());
81
82         return rc;
83     }
84
85     /**
86      * Create a builder for AtoZDirection object.
87      * @param atozMap Map of AToZ object
88      * @return a builder for AtoZDirection object
89      */
90     private AToZDirectionBuilder buildAtoZDirection(Map<AToZKey, AToZ> atozMap) {
91         ModulationFormat modulationFormat = GridConstant.RATE_MODULATION_FORMAT_MAP
92                 .getOrDefault(Uint32.valueOf(rc.getRate()), ModulationFormat.DpQpsk);
93         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
94             .setRate(Uint32.valueOf(rc.getRate()))
95             .setModulationFormat(modulationFormat.getName())
96             .setAToZ(atozMap);
97         switch (rc.getServiceType()) {
98             case StringConstants.SERVICE_TYPE_400GE:
99             case StringConstants.SERVICE_TYPE_OTUC2:
100             case StringConstants.SERVICE_TYPE_OTUC3:
101             case StringConstants.SERVICE_TYPE_OTUC4:
102             case StringConstants.SERVICE_TYPE_100GE_T:
103             case StringConstants.SERVICE_TYPE_OTU4:
104                 atoZDirectionBldr
105                         .setAToZMaxFrequency(new FrequencyTHz(Decimal64.valueOf(rc.getMaxFreq())))
106                         .setAToZMinFrequency(new FrequencyTHz(Decimal64.valueOf(rc.getMinFreq())))
107                         .setAToZWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()))
108                         // Used precision 5 to get the exact decimal values of the frequency
109                         .setCentralFrequency(new FrequencyTHz(GridUtils.getCentralFrequencyWithPrecision(
110                                 rc.getMinFreq(), rc.getMaxFreq(), 5).getValue()))
111                         .setWidth(GridUtils.getWidthFromRateAndModulationFormatToModel131(
112                                 Uint32.valueOf(rc.getRate()), modulationFormat));
113                 break;
114             case StringConstants.SERVICE_TYPE_100GE_M:
115             case StringConstants.SERVICE_TYPE_100GE_S:
116             case StringConstants.SERVICE_TYPE_10GE:
117             case StringConstants.SERVICE_TYPE_1GE:
118             case StringConstants.SERVICE_TYPE_ODU4:
119             case StringConstants.SERVICE_TYPE_ODUC2:
120             case StringConstants.SERVICE_TYPE_ODUC3:
121             case StringConstants.SERVICE_TYPE_ODUC4:
122                 if (rc.getResultTribPortTribSlot() != null && rc.getResultTribPortTribSlot().get(0) != null
123                     && rc.getResultTribPortTribSlot().get(1) != null) {
124                     atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(0))
125                             .setMinTribSlot(rc.getResultTribPortTribSlot().get(0))
126                             .setMaxTribSlot(rc.getResultTribPortTribSlot().get(1));
127                 } else {
128                     LOG.warn("Trib port and trib slot number should be present");
129                     atoZDirectionBldr.setMinTribSlot(new OpucnTribSlotDef("0.0"))
130                         .setMaxTribSlot(new OpucnTribSlotDef("0.0"));
131                 }
132                 break;
133             default:
134                 LOG.warn("unknown service type : unable to set Min/Max frequencies or trib-port/trib-slot");
135                 break;
136         }
137         return atoZDirectionBldr;
138     }
139
140     /**
141      * Create a builder for ZtoADirection object.
142      * @param ztoaMap Map of ZToA object
143      * @return a builder for ZtoADirection object
144      */
145     private ZToADirectionBuilder buildZtoADirection(Map<ZToAKey, ZToA> ztoaMap) {
146         ModulationFormat modulationFormat = GridConstant.RATE_MODULATION_FORMAT_MAP
147                 .getOrDefault(Uint32.valueOf(rc.getRate()), ModulationFormat.DpQpsk);
148         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder().setRate(Uint32.valueOf(rc.getRate()))
149                 .setModulationFormat(modulationFormat.getName())
150                 .setZToA(ztoaMap);
151         switch (rc.getServiceType()) {
152             case StringConstants.SERVICE_TYPE_400GE:
153             case StringConstants.SERVICE_TYPE_OTUC2:
154             case StringConstants.SERVICE_TYPE_OTUC3:
155             case StringConstants.SERVICE_TYPE_OTUC4:
156             case StringConstants.SERVICE_TYPE_100GE_T:
157             case StringConstants.SERVICE_TYPE_OTU4:
158                 ztoADirectionBldr
159                         .setZToAMaxFrequency(new FrequencyTHz(Decimal64.valueOf(rc.getMaxFreq())))
160                         .setZToAMinFrequency(new FrequencyTHz(Decimal64.valueOf(rc.getMinFreq())))
161                         .setZToAWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()))
162                         .setCentralFrequency(new FrequencyTHz(GridUtils.getCentralFrequencyWithPrecision(
163                                 rc.getMinFreq(), rc.getMaxFreq(), 4).getValue()))
164                         .setWidth(GridUtils.getWidthFromRateAndModulationFormatToModel131(
165                                 Uint32.valueOf(rc.getRate()), modulationFormat));
166                 break;
167             case StringConstants.SERVICE_TYPE_100GE_M:
168             case StringConstants.SERVICE_TYPE_100GE_S:
169             case StringConstants.SERVICE_TYPE_10GE:
170             case StringConstants.SERVICE_TYPE_1GE:
171             case StringConstants.SERVICE_TYPE_ODU4:
172             case StringConstants.SERVICE_TYPE_ODUC2:
173             case StringConstants.SERVICE_TYPE_ODUC3:
174             case StringConstants.SERVICE_TYPE_ODUC4:
175                 if (rc.getResultTribPortTribSlot() != null && rc.getResultTribPortTribSlot().get(0) != null
176                     && rc.getResultTribPortTribSlot().get(1) != null) {
177                     ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(0))
178                             .setMinTribSlot(rc.getResultTribPortTribSlot().get(0))
179                             .setMaxTribSlot(rc.getResultTribPortTribSlot().get(1));
180                 } else {
181                     LOG.warn("Trib port and trib slot number should be present");
182                     ztoADirectionBldr.setMinTribSlot(new OpucnTribSlotDef("0.0"))
183                         .setMaxTribSlot(new OpucnTribSlotDef("0.0"));
184                 }
185                 break;
186             default:
187                 LOG.warn("unknown service type : unable to set Min/Max frequencies");
188                 break;
189         }
190         return ztoADirectionBldr;
191     }
192
193     @SuppressWarnings("java:S138")
194     //sonar issue This method has 77 lines, which is greater than the 75 lines authorized. Split it into smaller
195     //ignore as it's not relevant to split it from functional point
196     private void buildAtoZ(Map<AToZKey, AToZ> atozMap, List<PceLink> path) {
197         Integer index = 0;
198         PceLink lastLink = null;
199         AToZ lastResource = null;
200
201         // build A side Client TP
202         String tpName = path.get(0).getClient();
203         String xname = path.get(0).getSourceId().getValue();
204         TerminationPoint stp = new TerminationPointBuilder()
205                 .setTpId(tpName).setTpNodeId(xname)
206                 .build();
207
208         AToZKey clientKey = new AToZKey(index.toString());
209         Resource clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
210         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
211         atozMap.put(firstResource.key(),firstResource);
212         index += 1;
213         for (PceLink pcelink : path) {
214             String srcName = pcelink.getSourceId().getValue();
215             // Nodes
216             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501
217                     .pce.resource.resource.resource.Node sourceNode = new NodeBuilder()
218                     .setNodeId(srcName)
219                     .build();
220
221             // Source Resource
222             AToZKey sourceKey = new AToZKey(index.toString());
223             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).setState(State.InService).build();
224             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
225             index += 1;
226             atozMap.put(srcResource.key(),srcResource);
227
228             // source TP
229             tpName = pcelink.getSourceTP().getValue();
230             stp = new TerminationPointBuilder()
231                     .setTpNodeId(srcName).setTpId(tpName)
232                     .build();
233
234             // Resource
235             AToZKey srcTPKey = new AToZKey(index.toString());
236             Resource tpResource1 = new ResourceBuilder().setResource(stp).setState(State.InService).build();
237             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
238             index += 1;
239             atozMap.put(stpResource.key(),stpResource);
240
241             String linkName = pcelink.getLinkId().getValue();
242             // Link
243             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501
244                     .pce.resource.resource.resource.Link atozLink = new LinkBuilder()
245                     .setLinkId(linkName)
246                     .build();
247
248             // Link Resource
249             AToZKey linkKey = new AToZKey(index.toString());
250             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).setState(pcelink.getState()).build();
251             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
252             index += 1;
253             atozMap.put(linkResource.key(),linkResource);
254
255             String destName = pcelink.getDestId().getValue();
256             // target TP
257             tpName = pcelink.getDestTP().getValue();
258             TerminationPoint dtp = new TerminationPointBuilder()
259                 .setTpNodeId(destName).setTpId(tpName)
260                 .build();
261
262             // Resource
263             AToZKey destTPKey = new AToZKey(index.toString());
264             Resource tpResource2 = new ResourceBuilder().setResource(dtp).setState(State.InService).build();
265             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
266             index += 1;
267             atozMap.put(ttpResource.key(),ttpResource);
268
269             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce
270                 .resource.resource.resource.Node targetNode = new NodeBuilder()
271                 .setNodeId(destName)
272                 .build();
273
274             // Target Resource
275             AToZKey targetKey = new AToZKey(index.toString());
276             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).setState(State.InService).build();
277             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
278
279             lastLink = pcelink;
280         }
281
282         if (lastResource != null) {
283             atozMap.put(lastResource.key(),lastResource);
284         }
285
286         // build Z side Client TP
287         tpName = lastLink.getClient();
288         xname = lastLink.getDestId().getValue();
289         stp = new TerminationPointBuilder()
290                 .setTpNodeId(xname).setTpId(tpName)
291                 .build();
292
293         index += 1;
294         clientKey = new AToZKey(index.toString());
295         clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
296         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
297         atozMap.put(lastResource.key(),lastResource);
298
299     }
300
301     private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
302         Integer index = 0;
303         PceLink lastLink = null;
304         ZToA lastResource = null;
305
306         // build Z size Client TP
307         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
308         String tpName = pcelink.getClient();
309         String xname = pcelink.getSourceId().getValue();
310         TerminationPoint stp = new TerminationPointBuilder()
311                 .setTpNodeId(xname).setTpId(tpName)
312                 .build();
313
314         ZToAKey clientKey = new ZToAKey(index.toString());
315         Resource clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
316         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
317         ztoaList.put(firstResource.key(),firstResource);
318         index += 1;
319
320         for (PceLink pcelinkAtoZ : path) {
321
322             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
323             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
324
325             String srcName = pcelink.getSourceId().getValue();
326
327
328             // Nodes
329             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce
330                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
331                 .setNodeId(srcName).build();
332
333
334             // Source Resource
335             ZToAKey sourceKey = new ZToAKey(index.toString());
336             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).setState(State.InService).build();
337             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
338             index += 1;
339             ztoaList.put(srcResource.key(),srcResource);
340
341             // source TP
342             tpName = pcelink.getSourceTP().getValue();
343             stp = new TerminationPointBuilder()
344                     .setTpNodeId(srcName).setTpId(tpName)
345                     .build();
346
347             // Resource
348             ZToAKey srcTPKey = new ZToAKey(index.toString());
349             Resource tpResource1 = new ResourceBuilder().setResource(stp).setState(State.InService).build();
350             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
351             index += 1;
352             ztoaList.put(stpResource.key(),stpResource);
353
354             String linkName = pcelink.getLinkId().getValue();
355             // Link
356             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce
357                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
358                 .setLinkId(linkName).build();
359
360             // Link Resource
361             ZToAKey linkKey = new ZToAKey(index.toString());
362             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).setState(State.InService).build();
363             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
364             index += 1;
365             ztoaList.put(linkResource.key(),linkResource);
366
367             String destName = pcelink.getDestId().getValue();
368             // target TP
369             tpName = pcelink.getDestTP().getValue();
370             TerminationPoint ttp = new TerminationPointBuilder()
371                     .setTpNodeId(destName).setTpId(tpName).build();
372
373             // Resource
374             ZToAKey destTPKey = new ZToAKey(index.toString());
375             Resource tpResource2 = new ResourceBuilder().setResource(ttp).setState(State.InService).build();
376             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
377             index += 1;
378             ztoaList.put(ttpResource.key(),ttpResource);
379
380
381             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce
382                 .resource.resource.resource.Node targetNode = new NodeBuilder()
383                 .setNodeId(destName).build();
384             // Target Resource
385             ZToAKey targetKey = new ZToAKey(index.toString());
386             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).setState(State.InService).build();
387             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
388
389             lastLink = pcelink;
390         }
391         if (lastResource != null) {
392             ztoaList.put(lastResource.key(),lastResource);
393         }
394
395         // build Z side Client TP
396         tpName = lastLink.getClient();
397         xname = lastLink.getDestId().getValue();
398         stp = new TerminationPointBuilder()
399                 .setTpNodeId(xname).setTpId(tpName).build();
400
401         index += 1;
402         clientKey = new ZToAKey(index.toString());
403         clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
404         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
405         ztoaList.put(lastResource.key(),lastResource);
406     }
407
408     public PceResult getReturnStructure() {
409         return rc;
410     }
411 }