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