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