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