Introduce the 100GE_S service type in PCE
[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_100GE_S:
105             case StringConstants.SERVICE_TYPE_10GE:
106             case StringConstants.SERVICE_TYPE_1GE:
107             case StringConstants.SERVICE_TYPE_ODU4:
108             case StringConstants.SERVICE_TYPE_ODUC4:
109                 if (rc.getResultTribPortTribSlot().get(0) != null && rc.getResultTribPortTribSlot().get(1) != null) {
110                     atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(0))
111                             .setMinTribSlot(rc.getResultTribPortTribSlot().get(0))
112                             .setMaxTribSlot(rc.getResultTribPortTribSlot().get(1));
113                 } else {
114                     LOG.error("Trib port and trib slot number should be present");
115                     atoZDirectionBldr.setMinTribSlot(new OpucnTribSlotDef("0.0"))
116                         .setMaxTribSlot(new OpucnTribSlotDef("0.0"));
117                 }
118                 break;
119             default:
120                 LOG.warn("unknown service type : unable to set Min/Max frequencies");
121                 break;
122         }
123         return atoZDirectionBldr;
124     }
125
126     /**
127      * Create a builder for ZtoADirection object.
128      * @param ztoaMap Map of ZToA object
129      * @return a builder for ZtoADirection object
130      */
131     private ZToADirectionBuilder buildZtoADirection(Map<ZToAKey, ZToA> ztoaMap) {
132         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder().setRate(Uint32.valueOf(rc.getRate()))
133                 .setModulationFormat(GridConstant.RATE_MODULATION_FORMAT_MAP
134                         .getOrDefault(Uint32.valueOf(rc.getRate()), ModulationFormat.DpQpsk).getName())
135                 .setZToA(ztoaMap);
136         switch (rc.getServiceType()) {
137             case StringConstants.SERVICE_TYPE_400GE:
138             case StringConstants.SERVICE_TYPE_OTUC4:
139             case StringConstants.SERVICE_TYPE_100GE_T:
140             case StringConstants.SERVICE_TYPE_OTU4:
141                 ztoADirectionBldr.setZToAMaxFrequency(new FrequencyTHz(rc.getMaxFreq()));
142                 ztoADirectionBldr.setZToAMinFrequency(new FrequencyTHz(rc.getMinFreq()));
143                 ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
144                 break;
145             case StringConstants.SERVICE_TYPE_100GE_M:
146             case StringConstants.SERVICE_TYPE_100GE_S:
147             case StringConstants.SERVICE_TYPE_10GE:
148             case StringConstants.SERVICE_TYPE_1GE:
149             case StringConstants.SERVICE_TYPE_ODU4:
150                 if (rc.getResultTribPortTribSlot().get(0) != null && rc.getResultTribPortTribSlot().get(1) != null) {
151                     ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(0))
152                             .setMinTribSlot(rc.getResultTribPortTribSlot().get(0))
153                             .setMaxTribSlot(rc.getResultTribPortTribSlot().get(1));
154                 } else {
155                     LOG.error("Trib port and trib slot number should be present");
156                     ztoADirectionBldr.setMinTribSlot(new OpucnTribSlotDef("0.0"))
157                         .setMaxTribSlot(new OpucnTribSlotDef("0.0"));
158                 }
159                 break;
160             default:
161                 LOG.warn("unknown service type : unable to set Min/Max frequencies");
162                 break;
163         }
164         return ztoADirectionBldr;
165     }
166
167     @SuppressWarnings("java:S138")
168     //sonar issue This method has 77 lines, which is greater than the 75 lines authorized. Split it into smaller
169     //ignore as it's not relevant to split it from functional point
170     private void buildAtoZ(Map<AToZKey, AToZ> atozMap, List<PceLink> path) {
171         Integer index = 0;
172         PceLink lastLink = null;
173         AToZ lastResource = null;
174
175         // build A side Client TP
176         String tpName = path.get(0).getClient();
177         String xname = path.get(0).getSourceId().getValue();
178         TerminationPoint stp = new TerminationPointBuilder()
179                 .setTpId(tpName).setTpNodeId(xname)
180                 .build();
181
182         AToZKey clientKey = new AToZKey(index.toString());
183         Resource clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
184         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
185         atozMap.put(firstResource.key(),firstResource);
186         index += 1;
187         for (PceLink pcelink : path) {
188             String srcName = pcelink.getSourceId().getValue();
189             // Nodes
190             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
191                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
192                 .setNodeId(srcName)
193                 .build();
194
195             // Source Resource
196             AToZKey sourceKey = new AToZKey(index.toString());
197             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).setState(State.InService).build();
198             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
199             index += 1;
200             atozMap.put(srcResource.key(),srcResource);
201
202             // source TP
203             tpName = pcelink.getSourceTP().getValue();
204             stp = new TerminationPointBuilder()
205                     .setTpNodeId(srcName).setTpId(tpName)
206                     .build();
207
208             // Resource
209             AToZKey srcTPKey = new AToZKey(index.toString());
210             Resource tpResource1 = new ResourceBuilder().setResource(stp).setState(State.InService).build();
211             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
212             index += 1;
213             atozMap.put(stpResource.key(),stpResource);
214
215             String linkName = pcelink.getLinkId().getValue();
216             // Link
217             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
218                 .resource.resource.resource.Link atozLink = new LinkBuilder()
219                 .setLinkId(linkName)
220                 .build();
221
222             // Link Resource
223             AToZKey linkKey = new AToZKey(index.toString());
224             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).setState(pcelink.getState()).build();
225             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
226             index += 1;
227             atozMap.put(linkResource.key(),linkResource);
228
229             String destName = pcelink.getDestId().getValue();
230             // target TP
231             tpName = pcelink.getDestTP().getValue();
232             TerminationPoint dtp = new TerminationPointBuilder()
233                 .setTpNodeId(destName).setTpId(tpName)
234                 .build();
235
236             // Resource
237             AToZKey destTPKey = new AToZKey(index.toString());
238             Resource tpResource2 = new ResourceBuilder().setResource(dtp).setState(State.InService).build();
239             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
240             index += 1;
241             atozMap.put(ttpResource.key(),ttpResource);
242
243             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
244                 .resource.resource.resource.Node targetNode = new NodeBuilder()
245                 .setNodeId(destName)
246                 .build();
247
248             // Target Resource
249             AToZKey targetKey = new AToZKey(index.toString());
250             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).setState(State.InService).build();
251             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
252
253             lastLink = pcelink;
254         }
255
256         if (lastResource != null) {
257             atozMap.put(lastResource.key(),lastResource);
258         }
259
260         // build Z side Client TP
261         tpName = lastLink.getClient();
262         xname = lastLink.getDestId().getValue();
263         stp = new TerminationPointBuilder()
264                 .setTpNodeId(xname).setTpId(tpName)
265                 .build();
266
267         index += 1;
268         clientKey = new AToZKey(index.toString());
269         clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
270         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
271         atozMap.put(lastResource.key(),lastResource);
272
273     }
274
275     private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
276         Integer index = 0;
277         PceLink lastLink = null;
278         ZToA lastResource = null;
279
280         // build Z size Client TP
281         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
282         String tpName = pcelink.getClient();
283         String xname = pcelink.getSourceId().getValue();
284         TerminationPoint stp = new TerminationPointBuilder()
285                 .setTpNodeId(xname).setTpId(tpName)
286                 .build();
287
288         ZToAKey clientKey = new ZToAKey(index.toString());
289         Resource clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
290         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
291         ztoaList.put(firstResource.key(),firstResource);
292         index += 1;
293
294         for (PceLink pcelinkAtoZ : path) {
295
296             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
297             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
298
299             String srcName = pcelink.getSourceId().getValue();
300
301
302             // Nodes
303             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
304                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
305                 .setNodeId(srcName).build();
306
307
308             // Source Resource
309             ZToAKey sourceKey = new ZToAKey(index.toString());
310             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).setState(State.InService).build();
311             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
312             index += 1;
313             ztoaList.put(srcResource.key(),srcResource);
314
315             // source TP
316             tpName = pcelink.getSourceTP().getValue();
317             stp = new TerminationPointBuilder()
318                     .setTpNodeId(srcName).setTpId(tpName)
319                     .build();
320
321             // Resource
322             ZToAKey srcTPKey = new ZToAKey(index.toString());
323             Resource tpResource1 = new ResourceBuilder().setResource(stp).setState(State.InService).build();
324             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
325             index += 1;
326             ztoaList.put(stpResource.key(),stpResource);
327
328             String linkName = pcelink.getLinkId().getValue();
329             // Link
330             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
331                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
332                 .setLinkId(linkName).build();
333
334             // Link Resource
335             ZToAKey linkKey = new ZToAKey(index.toString());
336             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).setState(State.InService).build();
337             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
338             index += 1;
339             ztoaList.put(linkResource.key(),linkResource);
340
341             String destName = pcelink.getDestId().getValue();
342             // target TP
343             tpName = pcelink.getDestTP().getValue();
344             TerminationPoint ttp = new TerminationPointBuilder()
345                     .setTpNodeId(destName).setTpId(tpName).build();
346
347             // Resource
348             ZToAKey destTPKey = new ZToAKey(index.toString());
349             Resource tpResource2 = new ResourceBuilder().setResource(ttp).setState(State.InService).build();
350             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
351             index += 1;
352             ztoaList.put(ttpResource.key(),ttpResource);
353
354
355             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce
356                 .resource.resource.resource.Node targetNode = new NodeBuilder()
357                 .setNodeId(destName).build();
358             // Target Resource
359             ZToAKey targetKey = new ZToAKey(index.toString());
360             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).setState(State.InService).build();
361             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
362
363             lastLink = pcelink;
364         }
365         if (lastResource != null) {
366             ztoaList.put(lastResource.key(),lastResource);
367         }
368
369         // build Z side Client TP
370         tpName = lastLink.getClient();
371         xname = lastLink.getDestId().getValue();
372         stp = new TerminationPointBuilder()
373                 .setTpNodeId(xname).setTpId(tpName).build();
374
375         index += 1;
376         clientKey = new ZToAKey(index.toString());
377         clientResource = new ResourceBuilder().setResource(stp).setState(State.InService).build();
378         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
379         ztoaList.put(lastResource.key(),lastResource);
380     }
381
382     public PceResult getReturnStructure() {
383         return rc;
384     }
385 }