2 * Copyright © 2017 AT&T, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.pce;
10 import com.google.common.collect.ImmutableList;
11 import java.util.HashMap;
12 import java.util.List;
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;
38 public class PcePathDescription {
40 private static final Logger LOG = LoggerFactory.getLogger(PcePathDescription.class);
42 private List<PceLink> pathAtoZ = null;
44 private Map<LinkId, PceLink> allPceLinks = null;
46 public PcePathDescription(List<PceLink> pathAtoZ, Map<LinkId, PceLink> allPceLinks, PceResult rc) {
48 this.allPceLinks = allPceLinks;
49 this.pathAtoZ = pathAtoZ;
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");
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);
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");
73 buildZtoA(ztoaMap, pathZtoA);
74 rc.setZtoADirection(buildZtoADirection(ztoaMap).build());
80 * Create a builder for AtoZDirection object.
81 * @param atozMap Map of AToZ object
82 * @return a builder for AtoZDirection object
84 private AToZDirectionBuilder buildAtoZDirection(Map<AToZKey, AToZ> atozMap) {
85 AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
86 .setRate(Uint32.valueOf(rc.getRate()))
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));
101 LOG.error("Trib port and trib slot number should be present");
102 atoZDirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
105 return atoZDirectionBldr;
109 * Create a builder for ZtoADirection object.
110 * @param ztoaMap Map of ZToA object
111 * @return a builder for ZtoADirection object
113 private ZToADirectionBuilder buildZtoADirection(Map<ZToAKey, ZToA> ztoaMap) {
114 ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
115 .setRate(Uint32.valueOf(rc.getRate()))
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));
130 LOG.error("Trib port and trib slot number should be present");
131 ztoADirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
134 return ztoADirectionBldr;
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) {
142 PceLink lastLink = null;
143 AToZ lastResource = null;
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)
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);
157 for (PceLink pcelink : path) {
158 String srcName = pcelink.getSourceId().getValue();
160 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
161 .resource.resource.resource.Node sourceNode = new NodeBuilder()
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();
170 atozMap.put(srcResource.key(),srcResource);
173 tpName = pcelink.getSourceTP().toString();
174 stp = new TerminationPointBuilder()
175 .setTpNodeId(srcName).setTpId(tpName)
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();
183 atozMap.put(stpResource.key(),stpResource);
185 String linkName = pcelink.getLinkId().getValue();
187 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
188 .resource.resource.resource.Link atozLink = new LinkBuilder()
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();
197 atozMap.put(linkResource.key(),linkResource);
199 String destName = pcelink.getDestId().getValue();
201 tpName = pcelink.getDestTP().toString();
202 TerminationPoint dtp = new TerminationPointBuilder()
203 .setTpNodeId(destName).setTpId(tpName)
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();
211 atozMap.put(ttpResource.key(),ttpResource);
213 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce
214 .resource.resource.resource.Node targetNode = new NodeBuilder()
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();
226 if (lastResource != null) {
227 atozMap.put(lastResource.key(),lastResource);
230 // build Z side Client TP
231 tpName = lastLink.getClient();
232 xname = lastLink.getDestId().getValue();
233 stp = new TerminationPointBuilder()
234 .setTpNodeId(xname).setTpId(tpName)
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);
245 private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
247 PceLink lastLink = null;
248 ZToA lastResource = null;
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)
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);
264 for (PceLink pcelinkAtoZ : path) {
266 pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
267 LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
269 String srcName = pcelink.getSourceId().getValue();
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();
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();
283 ztoaList.put(srcResource.key(),srcResource);
286 tpName = pcelink.getSourceTP().toString();
287 stp = new TerminationPointBuilder()
288 .setTpNodeId(srcName).setTpId(tpName)
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();
296 ztoaList.put(stpResource.key(),stpResource);
298 String linkName = pcelink.getLinkId().getValue();
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();
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();
309 ztoaList.put(linkResource.key(),linkResource);
311 String destName = pcelink.getDestId().getValue();
313 tpName = pcelink.getDestTP().toString();
314 TerminationPoint ttp = new TerminationPointBuilder()
315 .setTpNodeId(destName).setTpId(tpName).build();
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();
322 ztoaList.put(ttpResource.key(),ttpResource);
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();
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();
335 if (lastResource != null) {
336 ztoaList.put(lastResource.key(),lastResource);
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();
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);
352 public PceResult getReturnStructure() {