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