ff46c71f21cce425a6d36d69c46d6f526715e51d
[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.rev200629.path.description.AToZDirectionBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ZToADirectionBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.atoz.direction.AToZ;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.atoz.direction.AToZBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.atoz.direction.AToZKey;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ztoa.direction.ZToA;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ztoa.direction.ZToABuilder;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ztoa.direction.ZToAKey;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.Resource;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.ResourceBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.LinkBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.NodeBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.TerminationPoint;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.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 List<PceLink> pathZtoA = null;
41     private PceResult rc;
42     private Map<LinkId, PceLink> allPceLinks = null;
43
44     public PcePathDescription(List<PceLink> pathAtoZ, Map<LinkId, PceLink> allPceLinks, PceResult rc) {
45         super();
46         this.allPceLinks = allPceLinks;
47         this.pathAtoZ = pathAtoZ;
48         this.rc = rc;
49     }
50
51     public PceResult buildDescriptions() {
52         LOG.info("In buildDescriptions: AtoZ =  {}", pathAtoZ);
53         List<AToZ> atozList = new ArrayList<>();
54         if (pathAtoZ == null) {
55             rc.setRC(ResponseCodes.RESPONSE_FAILED);
56             LOG.error("In buildDescriptions: there is empty AtoZ path");
57             return rc;
58         }
59
60         buildAtoZ(atozList, pathAtoZ);
61         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
62             .setRate(rc.getRate())
63             .setAToZ(atozList);
64         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
65             atoZDirectionBldr.setAToZWavelengthNumber(rc.getResultWavelength());
66         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
67             || "ODU4".equals(rc.getServiceType())) {
68             atoZDirectionBldr.setAToZWavelengthNumber(Long.valueOf(0));
69         }
70         rc.setAtoZDirection(atoZDirectionBldr.build());
71         pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
72         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
73
74         List<ZToA> ztoaList = new ArrayList<>();
75         if (pathZtoA == null) {
76             rc.setRC(ResponseCodes.RESPONSE_FAILED);
77             LOG.error("In buildDescriptions: there is empty ZtoA path");
78             return rc;
79         }
80         buildZtoA(ztoaList, pathZtoA);
81         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
82             .setRate(rc.getRate())
83             .setZToA(ztoaList);
84         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
85             ztoADirectionBldr.setZToAWavelengthNumber(rc.getResultWavelength());
86         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
87             || "ODU4".equals(rc.getServiceType())) {
88             ztoADirectionBldr.setZToAWavelengthNumber(Long.valueOf(0));
89         }
90         rc.setZtoADirection(ztoADirectionBldr.build());
91
92         return rc;
93     }
94
95     private void buildAtoZ(List<AToZ> etoeList, List<PceLink> path) {
96         Integer index = 0;
97         PceLink lastLink = null;
98         AToZ lastResource = null;
99
100         // build A side Client TP
101         String tpName = path.get(0).getClient();
102         String xname = path.get(0).getSourceId().getValue();
103         TerminationPoint stp = new TerminationPointBuilder()
104                 .setTpId(tpName).setTpNodeId(xname)
105                 .build();
106
107         AToZKey clientKey = new AToZKey(index.toString());
108         Resource clientResource = new ResourceBuilder().setResource(stp).build();
109         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
110         etoeList.add(firstResource);
111         index += 1;
112         for (PceLink pcelink : path) {
113             String srcName = pcelink.getSourceId().getValue();
114             // Nodes
115             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
116                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
117                 .setNodeId(srcName)
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 += 1;
125             etoeList.add(srcResource);
126
127             // source TP
128             tpName = pcelink.getSourceTP().toString();
129             stp = new TerminationPointBuilder()
130                     .setTpNodeId(srcName).setTpId(tpName)
131                     .build();
132
133             // Resource
134             AToZKey srcTPKey = new AToZKey(index.toString());
135             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
136             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
137             index += 1;
138             etoeList.add(stpResource);
139
140             String linkName = pcelink.getLinkId().getValue();
141             // Link
142             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
143                 .resource.resource.resource.Link atozLink = new LinkBuilder()
144                 .setLinkId(linkName)
145                 .build();
146
147             // Link Resource
148             AToZKey linkKey = new AToZKey(index.toString());
149             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
150             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
151             index += 1;
152             etoeList.add(linkResource);
153
154             String destName = pcelink.getDestId().getValue();
155             // target TP
156             tpName = pcelink.getDestTP().toString();
157             TerminationPoint dtp = new TerminationPointBuilder()
158                 .setTpNodeId(destName).setTpId(tpName)
159                 .build();
160
161             // Resource
162             AToZKey destTPKey = new AToZKey(index.toString());
163             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
164             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
165             index += 1;
166             etoeList.add(ttpResource);
167
168             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
169                 .resource.resource.resource.Node targetNode = new NodeBuilder()
170                 .setNodeId(destName)
171                 .build();
172
173             // Target Resource
174             AToZKey targetKey = new AToZKey(index.toString());
175             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
176             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
177
178             lastLink = pcelink;
179         }
180
181         etoeList.add(lastResource);
182
183         // build Z side Client TP
184         tpName = lastLink.getClient();
185         xname = lastLink.getDestId().getValue();
186         stp = new TerminationPointBuilder()
187                 .setTpNodeId(xname).setTpId(tpName)
188                 .build();
189
190         index += 1;
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 += 1;
216
217         for (PceLink pcelinkAtoZ : path) {
218
219             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
220             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
221
222             String srcName = pcelink.getSourceId().getValue();
223
224
225             // Nodes
226             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.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 += 1;
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 += 1;
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.rev200629.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 += 1;
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 += 1;
275             etoelist.add(ttpResource);
276
277
278             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.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         index += 1;
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     public PceResult getReturnStructure() {
304         return rc;
305     }
306 }