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