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