Adapt PCE code for OTN services
[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         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
68             .setRate(rc.getRate())
69             .setAToZ(atozList);
70         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
71             atoZDirectionBldr.setAToZWavelengthNumber(rc.getResultWavelength());
72         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
73             || "ODU4".equals(rc.getServiceType())) {
74             atoZDirectionBldr.setAToZWavelengthNumber(Long.valueOf(0));
75         }
76         rc.setAtoZDirection(atoZDirectionBldr.build());
77         pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
78         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA.toString());
79
80         List<ZToA> ztoaList = new ArrayList<ZToA>();
81         if (pathZtoA == null) {
82             rc.setRC(ResponseCodes.RESPONSE_FAILED);
83             LOG.error("In buildDescriptions: there is empty ZtoA path");
84             return rc;
85         }
86         buildZtoA(ztoaList, pathZtoA);
87         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
88             .setRate(rc.getRate())
89             .setZToA(ztoaList);
90         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
91             ztoADirectionBldr.setZToAWavelengthNumber(rc.getResultWavelength());
92         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
93             || "ODU4".equals(rc.getServiceType())) {
94             ztoADirectionBldr.setZToAWavelengthNumber(Long.valueOf(0));
95         }
96         rc.setZtoADirection(ztoADirectionBldr.build());
97
98         return rc;
99     }
100
101     private void buildAtoZ(List<AToZ> etoeList, List<PceLink> path) {
102         Integer index = 0;
103         PceLink lastLink = null;
104         AToZ lastResource = null;
105
106         // build A side Client TP
107         String tpName = path.get(0).getClient();
108         String xname = path.get(0).getSourceId().getValue();
109         TerminationPoint stp = new TerminationPointBuilder()
110                 .setTpId(tpName).setTpNodeId(xname)
111                 .build();
112
113         AToZKey clientKey = new AToZKey(index.toString());
114         Resource clientResource = new ResourceBuilder().setResource(stp).build();
115         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
116         etoeList.add(firstResource);
117         index++;
118         for (PceLink pcelink : path) {
119             String srcName = pcelink.getSourceId().getValue();
120             // Nodes
121             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
122                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
123                 .setNodeId(srcName)
124                 .build();
125
126             // Source Resource
127             AToZKey sourceKey = new AToZKey(index.toString());
128             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
129             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
130             index++;
131             etoeList.add(srcResource);
132
133             // source TP
134             tpName = pcelink.getSourceTP().toString();
135             stp = new TerminationPointBuilder()
136                     .setTpNodeId(srcName).setTpId(tpName)
137                     .build();
138
139             // Resource
140             AToZKey srcTPKey = new AToZKey(index.toString());
141             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
142             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
143             index++;
144             etoeList.add(stpResource);
145
146             String linkName = pcelink.getLinkId().getValue();
147             // Link
148             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
149                 .resource.resource.resource.Link atozLink = new LinkBuilder()
150                 .setLinkId(linkName)
151                 .build();
152
153             // Link Resource
154             AToZKey linkKey = new AToZKey(index.toString());
155             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
156             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
157             index++;
158             etoeList.add(linkResource);
159
160             String destName = pcelink.getDestId().getValue();
161             // target TP
162             tpName = pcelink.getDestTP().toString();
163             TerminationPoint dtp = new TerminationPointBuilder()
164                 .setTpNodeId(destName).setTpId(tpName)
165                 .build();
166
167             // Resource
168             AToZKey destTPKey = new AToZKey(index.toString());
169             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
170             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
171             index++;
172             etoeList.add(ttpResource);
173
174             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
175                 .resource.resource.resource.Node targetNode = new NodeBuilder()
176                 .setNodeId(destName)
177                 .build();
178
179             // Target Resource
180             AToZKey targetKey = new AToZKey(index.toString());
181             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
182             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
183
184             lastLink = pcelink;
185         }
186
187         etoeList.add(lastResource);
188
189         // build Z side Client TP
190         tpName = lastLink.getClient();
191         xname = lastLink.getDestId().getValue();
192         stp = new TerminationPointBuilder()
193                 .setTpNodeId(xname).setTpId(tpName)
194                 .build();
195
196
197         index++;
198         clientKey = new AToZKey(index.toString());
199         clientResource = new ResourceBuilder().setResource(stp).build();
200         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
201         etoeList.add(lastResource);
202
203     }
204
205     private void buildZtoA(List<ZToA> etoelist, List<PceLink> path) {
206         Integer index = 0;
207         PceLink lastLink = null;
208         ZToA lastResource = null;
209
210         // build Z size Client TP
211         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
212         String tpName = pcelink.getClient();
213         String xname = pcelink.getSourceId().getValue();
214         TerminationPoint stp = new TerminationPointBuilder()
215                 .setTpNodeId(xname).setTpId(tpName)
216                 .build();
217
218         ZToAKey clientKey = new ZToAKey(index.toString());
219         Resource clientResource = new ResourceBuilder().setResource(stp).build();
220         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
221         etoelist.add(firstResource);
222         index++;
223
224         for (PceLink pcelinkAtoZ : path) {
225
226             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
227             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ.toString(), pcelink.toString());
228
229             String srcName = pcelink.getSourceId().getValue();
230
231
232             // Nodes
233             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
234                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
235                 .setNodeId(srcName).build();
236
237
238             // Source Resource
239             ZToAKey sourceKey = new ZToAKey(index.toString());
240             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
241             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
242             index++;
243             etoelist.add(srcResource);
244
245             // source TP
246             tpName = pcelink.getSourceTP().toString();
247             stp = new TerminationPointBuilder()
248                     .setTpNodeId(srcName).setTpId(tpName)
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.rev171017.pce
261                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
262                 .setLinkId(linkName).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                     .setTpNodeId(destName).setTpId(tpName).build();
276
277             // Resource
278             ZToAKey destTPKey = new ZToAKey(index.toString());
279             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
280             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
281             index++;
282             etoelist.add(ttpResource);
283
284
285             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
286                 .resource.resource.resource.Node targetNode = new NodeBuilder()
287                 .setNodeId(destName).build();
288             // Target Resource
289             ZToAKey targetKey = new ZToAKey(index.toString());
290             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
291             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
292
293             lastLink = pcelink;
294         }
295         etoelist.add(lastResource);
296
297         // build Z side Client TP
298         tpName = lastLink.getClient();
299         xname = lastLink.getDestId().getValue();
300         stp = new TerminationPointBuilder()
301                 .setTpNodeId(xname).setTpId(tpName).build();
302
303
304         index++;
305         clientKey = new ZToAKey(index.toString());
306         clientResource = new ResourceBuilder().setResource(stp).build();
307         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
308         etoelist.add(lastResource);
309
310     }
311
312 }