Merge "remove dead code pointed out by sonar"
[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.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import org.opendaylight.transportpce.common.ResponseCodes;
15 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirectionBuilder;
16 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirectionBuilder;
17 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
18 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZKey;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToABuilder;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToAKey;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.Resource;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.ResourceBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.LinkBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.NodeBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.TerminationPoint;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.TerminationPointBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class PcePathDescription {
34     /* Logging. */
35     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
36
37     private List<PceLink> pathAtoZ = null;
38     private PceResult rc;
39
40     public PceResult getReturnStructure() {
41         return this.rc;
42     }
43
44     private Map<LinkId, PceLink> allPceLinks = null;
45
46     private List<PceLink> pathZtoA = null;
47
48     public PcePathDescription(List<PceLink> pathAtoZ, Map<LinkId, PceLink> allPceLinks, PceResult rc) {
49         super();
50         this.allPceLinks = allPceLinks;
51         this.pathAtoZ = pathAtoZ;
52         this.rc = rc;
53     }
54
55     public PceResult buildDescriptions() {
56         LOG.info("In buildDescriptions: AtoZ {}", this.pathAtoZ.toString());
57         List<AToZ> atozList = new ArrayList<AToZ>();
58         if (this.pathAtoZ == null) {
59             this.rc.setRC(ResponseCodes.RESPONSE_FAILED);
60             LOG.error("In buildDescriptions: there is empty AtoZ path");
61             return this.rc;
62         }
63
64         buildAtoZ(atozList, this.pathAtoZ);
65
66         this.rc.setAtoZDirection(new AToZDirectionBuilder().setRate(this.rc.getRate())
67                 .setAToZWavelengthNumber(this.rc.getResultWavelength()).setAToZ(atozList).build());
68
69         this.pathZtoA = ImmutableList.copyOf(this.pathAtoZ).reverse();
70         LOG.info("In buildDescriptions: ZtoA {}", this.pathZtoA.toString());
71
72         List<ZToA> ztoaList = new ArrayList<ZToA>();
73         if (this.pathZtoA == null) {
74             this.rc.setRC(ResponseCodes.RESPONSE_FAILED);
75             LOG.error("In buildDescriptions: there is empty ZtoA path");
76             return this.rc;
77         }
78         buildZtoA(ztoaList, this.pathZtoA);
79
80         this.rc.setZtoADirection(new ZToADirectionBuilder().setRate(this.rc.getRate())
81                 .setZToAWavelengthNumber(this.rc.getResultWavelength()).setZToA(ztoaList).build());
82
83         return this.rc;
84     }
85
86     private void buildAtoZ(List<AToZ> etoeList, List<PceLink> path) {
87
88         Integer index = 0;
89         PceLink lastLink = null;
90         AToZ lastResource = null;
91
92         // build A side Client TP
93         String tpName = path.get(0).getClient();
94         String xname = path.get(0).getSourceId().getValue();
95         TerminationPoint stp = new TerminationPointBuilder()
96                 .setTpId(tpName).setTpNodeId(xname)
97                 .build();
98
99         AToZKey clientKey = new AToZKey(index.toString());
100         Resource clientResource = new ResourceBuilder().setResource(stp).build();
101         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
102         etoeList.add(firstResource);
103
104         index++;
105
106         for (PceLink pcelink : path) {
107
108             String srcName = pcelink.getSourceId().getValue();
109
110             // Nodes
111             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
112                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
113                 .setNodeId(srcName)
114                 .build();
115
116             // Source Resource
117             AToZKey sourceKey = new AToZKey(index.toString());
118             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
119             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
120             index++;
121             etoeList.add(srcResource);
122
123             // source TP
124             tpName = pcelink.getSourceTP().toString();
125             stp = new TerminationPointBuilder()
126                     .setTpNodeId(srcName).setTpId(tpName)
127                     .build();
128
129             // Resource
130             AToZKey srcTPKey = new AToZKey(index.toString());
131             // tpName);
132             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
133             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
134             index++;
135             etoeList.add(stpResource);
136
137             String linkName = pcelink.getLinkId().getValue();
138             // Link
139             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
140                 .resource.resource.resource.Link atozLink = new LinkBuilder()
141                 .setLinkId(linkName)
142                 .build();
143
144             // Link Resource
145             AToZKey linkKey = new AToZKey(index.toString());
146             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
147             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
148             index++;
149             etoeList.add(linkResource);
150
151             String destName = pcelink.getDestId().getValue();
152             // target TP
153             tpName = pcelink.getDestTP().toString();
154             TerminationPoint dtp = new TerminationPointBuilder()
155                     .setTpNodeId(destName).setTpId(tpName)
156                     .build();
157
158             // Resource
159             AToZKey destTPKey = new AToZKey(index.toString());
160             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
161             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
162             index++;
163             etoeList.add(ttpResource);
164
165             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
166                 .resource.resource.resource.Node targetNode = new NodeBuilder()
167                 .setNodeId(destName)
168                 .build();
169
170             // Target Resource
171             AToZKey targetKey = new AToZKey(index.toString());
172             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
173             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
174
175             lastLink = pcelink;
176         }
177
178         etoeList.add(lastResource);
179
180         // build Z side Client TP
181         tpName = lastLink.getClient();
182         xname = lastLink.getDestId().getValue();
183         stp = new TerminationPointBuilder()
184                 .setTpNodeId(xname).setTpId(tpName)
185                 .build();
186
187
188         index++;
189         clientKey = new AToZKey(index.toString());
190         clientResource = new ResourceBuilder().setResource(stp).build();
191         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
192         etoeList.add(lastResource);
193
194     }
195
196     private void buildZtoA(List<ZToA> etoelist, List<PceLink> path) {
197         Integer index = 0;
198         PceLink lastLink = null;
199         ZToA lastResource = null;
200
201         // build Z size Client TP
202         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
203         String tpName = pcelink.getClient();
204         String xname = pcelink.getSourceId().getValue();
205         TerminationPoint stp = new TerminationPointBuilder()
206                 .setTpNodeId(xname).setTpId(tpName)
207                 .build();
208
209         ZToAKey clientKey = new ZToAKey(index.toString());
210         Resource clientResource = new ResourceBuilder().setResource(stp).build();
211         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
212         etoelist.add(firstResource);
213         index++;
214
215         for (PceLink pcelinkAtoZ : path) {
216
217             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
218             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ.toString(), pcelink.toString());
219
220             String srcName = pcelink.getSourceId().getValue();
221
222
223             // Nodes
224             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
225                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
226                 .setNodeId(srcName).build();
227
228
229             // Source Resource
230             ZToAKey sourceKey = new ZToAKey(index.toString());
231             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
232             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
233             index++;
234             etoelist.add(srcResource);
235
236             // source TP
237             tpName = pcelink.getSourceTP().toString();
238             stp = new TerminationPointBuilder()
239                     .setTpNodeId(srcName).setTpId(tpName)
240                     .build();
241
242             // Resource
243             ZToAKey srcTPKey = new ZToAKey(index.toString());
244             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
245             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
246             index++;
247             etoelist.add(stpResource);
248
249             String linkName = pcelink.getLinkId().getValue();
250             // Link
251             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
252                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
253                 .setLinkId(linkName).build();
254
255             // Link Resource
256             ZToAKey linkKey = new ZToAKey(index.toString());
257             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).build();
258             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
259             index++;
260             etoelist.add(linkResource);
261
262             String destName = pcelink.getDestId().getValue();
263             // target TP
264             tpName = pcelink.getDestTP().toString();
265             TerminationPoint ttp = new TerminationPointBuilder()
266                     .setTpNodeId(destName).setTpId(tpName).build();
267
268             // Resource
269             ZToAKey destTPKey = new ZToAKey(index.toString());
270             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
271             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
272             index++;
273             etoelist.add(ttpResource);
274
275
276             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
277                 .resource.resource.resource.Node targetNode = new NodeBuilder()
278                 .setNodeId(destName).build();
279             // Target Resource
280             ZToAKey targetKey = new ZToAKey(index.toString());
281             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
282             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
283
284             lastLink = pcelink;
285         }
286         etoelist.add(lastResource);
287
288         // build Z side Client TP
289         tpName = lastLink.getClient();
290         xname = lastLink.getDestId().getValue();
291         stp = new TerminationPointBuilder()
292                 .setTpNodeId(xname).setTpId(tpName).build();
293
294
295         index++;
296         clientKey = new ZToAKey(index.toString());
297         clientResource = new ResourceBuilder().setResource(stp).build();
298         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
299         etoelist.add(lastResource);
300
301     }
302
303 }