clean api pom
[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.rev150608.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());// tpName);
131             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
132             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
133             index++;
134             etoeList.add(stpResource);
135
136             String linkName = pcelink.getLinkId().getValue();
137             // Link
138             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
139                 .resource.resource.resource.Link atozLink = new LinkBuilder()
140                 .setLinkId(linkName)
141                 .build();
142
143             // Link Resource
144             AToZKey linkKey = new AToZKey(index.toString());
145             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
146             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
147             index++;
148             etoeList.add(linkResource);
149
150             String destName = pcelink.getDestId().getValue();
151             // target TP
152             tpName = pcelink.getDestTP().toString();
153             TerminationPoint dtp = new TerminationPointBuilder()
154                     .setTpNodeId(destName).setTpId(tpName)
155                     .build();
156
157             // Resource
158             AToZKey destTPKey = new AToZKey(index.toString());
159             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
160             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
161             index++;
162             etoeList.add(ttpResource);
163
164             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
165                 .resource.resource.resource.Node targetNode = new NodeBuilder()
166                 .setNodeId(destName)
167                 .build();
168
169             // Target Resource
170             AToZKey targetKey = new AToZKey(index.toString());
171             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
172             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
173
174             lastLink = pcelink;
175         }
176
177         etoeList.add(lastResource);
178
179         // build Z side Client TP
180         tpName = lastLink.getClient();
181         xname = lastLink.getDestId().getValue();
182         stp = new TerminationPointBuilder()
183                 .setTpNodeId(xname).setTpId(tpName)
184                 .build();
185
186
187         index++;
188         clientKey = new AToZKey(index.toString());
189         clientResource = new ResourceBuilder().setResource(stp).build();
190         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
191         etoeList.add(lastResource);
192
193     }
194
195     private void buildZtoA(List<ZToA> etoelist, List<PceLink> path) {
196         Integer index = 0;
197         PceLink lastLink = null;
198         ZToA lastResource = null;
199
200         // build Z size Client TP
201         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
202         String tpName = pcelink.getClient();
203         String xname = pcelink.getSourceId().getValue();
204         TerminationPoint stp = new TerminationPointBuilder()
205                 .setTpNodeId(xname).setTpId(tpName)
206                 .build();
207
208         ZToAKey clientKey = new ZToAKey(index.toString());
209         Resource clientResource = new ResourceBuilder().setResource(stp).build();
210         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
211         etoelist.add(firstResource);
212         index++;
213
214         for (PceLink pcelinkAtoZ : path) {
215
216             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
217             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ.toString(), pcelink.toString());
218
219             String srcName = pcelink.getSourceId().getValue();
220
221
222             // Nodes
223             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
224                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
225                 .setNodeId(srcName).build();
226
227
228             // Source Resource
229             ZToAKey sourceKey = new ZToAKey(index.toString());
230             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
231             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
232             index++;
233             etoelist.add(srcResource);
234
235             // source TP
236             tpName = pcelink.getSourceTP().toString();
237             stp = new TerminationPointBuilder()
238                     .setTpNodeId(srcName).setTpId(tpName)
239                     .build();
240
241             // Resource
242             ZToAKey srcTPKey = new ZToAKey(index.toString());
243             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
244             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
245             index++;
246             etoelist.add(stpResource);
247
248             String linkName = pcelink.getLinkId().getValue();
249             // Link
250             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
251                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
252                 .setLinkId(linkName).build();
253
254             // Link Resource
255             ZToAKey linkKey = new ZToAKey(index.toString());
256             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).build();
257             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
258             index++;
259             etoelist.add(linkResource);
260
261             String destName = pcelink.getDestId().getValue();
262             // target TP
263             tpName = pcelink.getDestTP().toString();
264             TerminationPoint ttp = new TerminationPointBuilder()
265                     .setTpNodeId(destName).setTpId(tpName).build();
266
267             // Resource
268             ZToAKey destTPKey = new ZToAKey(index.toString());
269             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
270             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
271             index++;
272             etoelist.add(ttpResource);
273
274
275             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
276                 .resource.resource.resource.Node targetNode = new NodeBuilder()
277                 .setNodeId(destName).build();
278             // Target Resource
279             ZToAKey targetKey = new ZToAKey(index.toString());
280             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
281             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
282
283             lastLink = pcelink;
284         }
285         etoelist.add(lastResource);
286
287         // build Z side Client TP
288         tpName = lastLink.getClient();
289         xname = lastLink.getDestId().getValue();
290         stp = new TerminationPointBuilder()
291                 .setTpNodeId(xname).setTpId(tpName).build();
292
293
294         index++;
295         clientKey = new ZToAKey(index.toString());
296         clientResource = new ResourceBuilder().setResource(stp).build();
297         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
298         etoelist.add(lastResource);
299
300     }
301
302 }