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