Constants for service type
[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.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import org.opendaylight.transportpce.common.ResponseCodes;
15 import org.opendaylight.transportpce.common.StringConstants;
16 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
17 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
18 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.AToZDirectionBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ZToADirectionBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.atoz.direction.AToZ;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.atoz.direction.AToZBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.atoz.direction.AToZKey;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ztoa.direction.ZToA;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ztoa.direction.ZToABuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ztoa.direction.ZToAKey;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.Resource;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.ResourceBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.resource.resource.LinkBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.resource.resource.NodeBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.resource.resource.TerminationPoint;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.resource.resource.TerminationPointBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
33 import org.opendaylight.yangtools.yang.common.Uint16;
34 import org.opendaylight.yangtools.yang.common.Uint32;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class PcePathDescription {
39     /* Logging. */
40     private static final Logger LOG = LoggerFactory.getLogger(PcePathDescription.class);
41
42     private List<PceLink> pathAtoZ = 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         Map<AToZKey,AToZ> atozMap = new HashMap<>();
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(atozMap, pathAtoZ);
63         rc.setAtoZDirection(buildAtoZDirection(atozMap).build());
64         List<PceLink> pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
65         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
66
67         Map<ZToAKey,ZToA> ztoaMap = new HashMap<>();
68         if (pathZtoA == null) {
69             rc.setRC(ResponseCodes.RESPONSE_FAILED);
70             LOG.error("In buildDescriptions: there is empty ZtoA path");
71             return rc;
72         }
73         buildZtoA(ztoaMap, pathZtoA);
74         rc.setZtoADirection(buildZtoADirection(ztoaMap).build());
75
76         return rc;
77     }
78
79     /**
80      * Create a builder for AtoZDirection object.
81      * @param atozMap Map of AToZ object
82      * @return a builder for AtoZDirection object
83      */
84     private AToZDirectionBuilder buildAtoZDirection(Map<AToZKey, AToZ> atozMap) {
85         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
86             .setRate(Uint32.valueOf(rc.getRate()))
87             .setAToZ(atozMap);
88         if (StringConstants.SERVICE_TYPE_100GE.equals(rc.getServiceType())
89                 || StringConstants.SERVICE_TYPE_OTU4.equals(rc.getServiceType())) {
90             atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
91         } else if (StringConstants.SERVICE_TYPE_10GE.equals(rc.getServiceType())
92                 || StringConstants.SERVICE_TYPE_1GE.equals(rc.getServiceType())
93             || StringConstants.SERVICE_TYPE_ODU4.equals(rc.getServiceType())) {
94             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
95                 @SuppressWarnings("unchecked")
96                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
97                 atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(0))
98                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
99                     .setTribSlotNumber(tribSlotList.get(0));
100             } else {
101                 LOG.error("Trib port and trib slot number should be present");
102                 atoZDirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
103             }
104         }
105         return atoZDirectionBldr;
106     }
107
108     /**
109      * Create a builder for ZtoADirection object.
110      * @param ztoaMap Map of ZToA object
111      * @return a builder for ZtoADirection object
112      */
113     private ZToADirectionBuilder buildZtoADirection(Map<ZToAKey, ZToA> ztoaMap) {
114         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
115             .setRate(Uint32.valueOf(rc.getRate()))
116             .setZToA(ztoaMap);
117         if (StringConstants.SERVICE_TYPE_100GE.equals(rc.getServiceType())
118                 || StringConstants.SERVICE_TYPE_OTU4.equals(rc.getServiceType())) {
119             ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
120         } else if (StringConstants.SERVICE_TYPE_10GE.equals(rc.getServiceType())
121                 || StringConstants.SERVICE_TYPE_1GE.equals(rc.getServiceType())
122             || StringConstants.SERVICE_TYPE_ODU4.equals(rc.getServiceType())) {
123             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
124                 @SuppressWarnings("unchecked")
125                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
126                 ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(0))
127                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
128                     .setTribSlotNumber(tribSlotList.get(0));
129             } else {
130                 LOG.error("Trib port and trib slot number should be present");
131                 ztoADirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
132             }
133         }
134         return ztoADirectionBldr;
135     }
136
137     @SuppressWarnings("java:S138")
138     //sonar issue This method has 77 lines, which is greater than the 75 lines authorized. Split it into smaller
139     //ignore as it's not relevant to split it from functional point
140     private void buildAtoZ(Map<AToZKey, AToZ> atozMap, List<PceLink> path) {
141         Integer index = 0;
142         PceLink lastLink = null;
143         AToZ lastResource = null;
144
145         // build A side Client TP
146         String tpName = path.get(0).getClient();
147         String xname = path.get(0).getSourceId().getValue();
148         TerminationPoint stp = new TerminationPointBuilder()
149                 .setTpId(tpName).setTpNodeId(xname)
150                 .build();
151
152         AToZKey clientKey = new AToZKey(index.toString());
153         Resource clientResource = new ResourceBuilder().setResource(stp).build();
154         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
155         atozMap.put(firstResource.key(),firstResource);
156         index += 1;
157         for (PceLink pcelink : path) {
158             String srcName = pcelink.getSourceId().getValue();
159             // Nodes
160             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
161                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
162                 .setNodeId(srcName)
163                 .build();
164
165             // Source Resource
166             AToZKey sourceKey = new AToZKey(index.toString());
167             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
168             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
169             index += 1;
170             atozMap.put(srcResource.key(),srcResource);
171
172             // source TP
173             tpName = pcelink.getSourceTP().toString();
174             stp = new TerminationPointBuilder()
175                     .setTpNodeId(srcName).setTpId(tpName)
176                     .build();
177
178             // Resource
179             AToZKey srcTPKey = new AToZKey(index.toString());
180             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
181             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
182             index += 1;
183             atozMap.put(stpResource.key(),stpResource);
184
185             String linkName = pcelink.getLinkId().getValue();
186             // Link
187             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
188                 .resource.resource.resource.Link atozLink = new LinkBuilder()
189                 .setLinkId(linkName)
190                 .build();
191
192             // Link Resource
193             AToZKey linkKey = new AToZKey(index.toString());
194             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
195             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
196             index += 1;
197             atozMap.put(linkResource.key(),linkResource);
198
199             String destName = pcelink.getDestId().getValue();
200             // target TP
201             tpName = pcelink.getDestTP().toString();
202             TerminationPoint dtp = new TerminationPointBuilder()
203                 .setTpNodeId(destName).setTpId(tpName)
204                 .build();
205
206             // Resource
207             AToZKey destTPKey = new AToZKey(index.toString());
208             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
209             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
210             index += 1;
211             atozMap.put(ttpResource.key(),ttpResource);
212
213             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
214                 .resource.resource.resource.Node targetNode = new NodeBuilder()
215                 .setNodeId(destName)
216                 .build();
217
218             // Target Resource
219             AToZKey targetKey = new AToZKey(index.toString());
220             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
221             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
222
223             lastLink = pcelink;
224         }
225
226         if (lastResource != null) {
227             atozMap.put(lastResource.key(),lastResource);
228         }
229
230         // build Z side Client TP
231         tpName = lastLink.getClient();
232         xname = lastLink.getDestId().getValue();
233         stp = new TerminationPointBuilder()
234                 .setTpNodeId(xname).setTpId(tpName)
235                 .build();
236
237         index += 1;
238         clientKey = new AToZKey(index.toString());
239         clientResource = new ResourceBuilder().setResource(stp).build();
240         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
241         atozMap.put(lastResource.key(),lastResource);
242
243     }
244
245     private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
246         Integer index = 0;
247         PceLink lastLink = null;
248         ZToA lastResource = null;
249
250         // build Z size Client TP
251         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
252         String tpName = pcelink.getClient();
253         String xname = pcelink.getSourceId().getValue();
254         TerminationPoint stp = new TerminationPointBuilder()
255                 .setTpNodeId(xname).setTpId(tpName)
256                 .build();
257
258         ZToAKey clientKey = new ZToAKey(index.toString());
259         Resource clientResource = new ResourceBuilder().setResource(stp).build();
260         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
261         ztoaList.put(firstResource.key(),firstResource);
262         index += 1;
263
264         for (PceLink pcelinkAtoZ : path) {
265
266             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
267             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
268
269             String srcName = pcelink.getSourceId().getValue();
270
271
272             // Nodes
273             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
274                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
275                 .setNodeId(srcName).build();
276
277
278             // Source Resource
279             ZToAKey sourceKey = new ZToAKey(index.toString());
280             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
281             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
282             index += 1;
283             ztoaList.put(srcResource.key(),srcResource);
284
285             // source TP
286             tpName = pcelink.getSourceTP().toString();
287             stp = new TerminationPointBuilder()
288                     .setTpNodeId(srcName).setTpId(tpName)
289                     .build();
290
291             // Resource
292             ZToAKey srcTPKey = new ZToAKey(index.toString());
293             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
294             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
295             index += 1;
296             ztoaList.put(stpResource.key(),stpResource);
297
298             String linkName = pcelink.getLinkId().getValue();
299             // Link
300             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
301                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
302                 .setLinkId(linkName).build();
303
304             // Link Resource
305             ZToAKey linkKey = new ZToAKey(index.toString());
306             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).build();
307             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
308             index += 1;
309             ztoaList.put(linkResource.key(),linkResource);
310
311             String destName = pcelink.getDestId().getValue();
312             // target TP
313             tpName = pcelink.getDestTP().toString();
314             TerminationPoint ttp = new TerminationPointBuilder()
315                     .setTpNodeId(destName).setTpId(tpName).build();
316
317             // Resource
318             ZToAKey destTPKey = new ZToAKey(index.toString());
319             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
320             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
321             index += 1;
322             ztoaList.put(ttpResource.key(),ttpResource);
323
324
325             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
326                 .resource.resource.resource.Node targetNode = new NodeBuilder()
327                 .setNodeId(destName).build();
328             // Target Resource
329             ZToAKey targetKey = new ZToAKey(index.toString());
330             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
331             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
332
333             lastLink = pcelink;
334         }
335         if (lastResource != null) {
336             ztoaList.put(lastResource.key(),lastResource);
337         }
338
339         // build Z side Client TP
340         tpName = lastLink.getClient();
341         xname = lastLink.getDestId().getValue();
342         stp = new TerminationPointBuilder()
343                 .setTpNodeId(xname).setTpId(tpName).build();
344
345         index += 1;
346         clientKey = new ZToAKey(index.toString());
347         clientResource = new ResourceBuilder().setResource(stp).build();
348         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
349         ztoaList.put(lastResource.key(),lastResource);
350     }
351
352     public PceResult getReturnStructure() {
353         return rc;
354     }
355 }