b067745918879a7e84949cc05540db2cae96416e
[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.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.opendaylight.yangtools.yang.common.Uint16;
33 import org.opendaylight.yangtools.yang.common.Uint32;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class PcePathDescription {
38     /* Logging. */
39     private static final Logger LOG = LoggerFactory.getLogger(PcePathDescription.class);
40
41     private List<PceLink> pathAtoZ = null;
42     private List<PceLink> pathZtoA = 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> atozList = 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(atozList, pathAtoZ);
63         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
64             .setRate(Uint32.valueOf(rc.getRate()))
65             .setAToZ(atozList);
66         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
67             atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
68         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
69             || "ODU4".equals(rc.getServiceType())) {
70             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
71                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
72                 atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(0))
73                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
74                     .setTribSlotNumber(tribSlotList.get(0));
75             } else {
76                 LOG.error("Trib port and trib slot number should be present");
77                 atoZDirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
78             }
79         }
80         rc.setAtoZDirection(atoZDirectionBldr.build());
81         pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
82         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
83
84         Map<ZToAKey,ZToA> ztoaList = new HashMap<>();
85         if (pathZtoA == null) {
86             rc.setRC(ResponseCodes.RESPONSE_FAILED);
87             LOG.error("In buildDescriptions: there is empty ZtoA path");
88             return rc;
89         }
90         buildZtoA(ztoaList, pathZtoA);
91         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
92             .setRate(Uint32.valueOf(rc.getRate()))
93             .setZToA(ztoaList);
94         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
95             ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
96         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
97             || "ODU4".equals(rc.getServiceType())) {
98             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
99                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
100                 ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(0))
101                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
102                     .setTribSlotNumber(tribSlotList.get(0));
103             } else {
104                 LOG.error("Trib port and trib slot number should be present");
105                 ztoADirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
106             }
107         }
108         rc.setZtoADirection(ztoADirectionBldr.build());
109
110         return rc;
111     }
112
113     private void buildAtoZ(Map<AToZKey, AToZ> atozList, List<PceLink> path) {
114         Integer index = 0;
115         PceLink lastLink = null;
116         AToZ lastResource = null;
117
118         // build A side Client TP
119         String tpName = path.get(0).getClient();
120         String xname = path.get(0).getSourceId().getValue();
121         TerminationPoint stp = new TerminationPointBuilder()
122                 .setTpId(tpName).setTpNodeId(xname)
123                 .build();
124
125         AToZKey clientKey = new AToZKey(index.toString());
126         Resource clientResource = new ResourceBuilder().setResource(stp).build();
127         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
128         atozList.put(firstResource.key(),firstResource);
129         index += 1;
130         for (PceLink pcelink : path) {
131             String srcName = pcelink.getSourceId().getValue();
132             // Nodes
133             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
134                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
135                 .setNodeId(srcName)
136                 .build();
137
138             // Source Resource
139             AToZKey sourceKey = new AToZKey(index.toString());
140             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
141             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
142             index += 1;
143             atozList.put(srcResource.key(),srcResource);
144
145             // source TP
146             tpName = pcelink.getSourceTP().toString();
147             stp = new TerminationPointBuilder()
148                     .setTpNodeId(srcName).setTpId(tpName)
149                     .build();
150
151             // Resource
152             AToZKey srcTPKey = new AToZKey(index.toString());
153             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
154             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
155             index += 1;
156             atozList.put(stpResource.key(),stpResource);
157
158             String linkName = pcelink.getLinkId().getValue();
159             // Link
160             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
161                 .resource.resource.resource.Link atozLink = new LinkBuilder()
162                 .setLinkId(linkName)
163                 .build();
164
165             // Link Resource
166             AToZKey linkKey = new AToZKey(index.toString());
167             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
168             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
169             index += 1;
170             atozList.put(linkResource.key(),linkResource);
171
172             String destName = pcelink.getDestId().getValue();
173             // target TP
174             tpName = pcelink.getDestTP().toString();
175             TerminationPoint dtp = new TerminationPointBuilder()
176                 .setTpNodeId(destName).setTpId(tpName)
177                 .build();
178
179             // Resource
180             AToZKey destTPKey = new AToZKey(index.toString());
181             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
182             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
183             index += 1;
184             atozList.put(ttpResource.key(),ttpResource);
185
186             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
187                 .resource.resource.resource.Node targetNode = new NodeBuilder()
188                 .setNodeId(destName)
189                 .build();
190
191             // Target Resource
192             AToZKey targetKey = new AToZKey(index.toString());
193             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
194             lastResource = new AToZBuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
195
196             lastLink = pcelink;
197         }
198
199         if (lastResource != null) {
200             atozList.put(lastResource.key(),lastResource);
201         }
202
203         // build Z side Client TP
204         tpName = lastLink.getClient();
205         xname = lastLink.getDestId().getValue();
206         stp = new TerminationPointBuilder()
207                 .setTpNodeId(xname).setTpId(tpName)
208                 .build();
209
210         index += 1;
211         clientKey = new AToZKey(index.toString());
212         clientResource = new ResourceBuilder().setResource(stp).build();
213         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
214         atozList.put(lastResource.key(),lastResource);
215
216     }
217
218     private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
219         Integer index = 0;
220         PceLink lastLink = null;
221         ZToA lastResource = null;
222
223         // build Z size Client TP
224         PceLink pcelink = this.allPceLinks.get(path.get(0).getOppositeLink());
225         String tpName = pcelink.getClient();
226         String xname = pcelink.getSourceId().getValue();
227         TerminationPoint stp = new TerminationPointBuilder()
228                 .setTpNodeId(xname).setTpId(tpName)
229                 .build();
230
231         ZToAKey clientKey = new ZToAKey(index.toString());
232         Resource clientResource = new ResourceBuilder().setResource(stp).build();
233         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
234         ztoaList.put(firstResource.key(),firstResource);
235         index += 1;
236
237         for (PceLink pcelinkAtoZ : path) {
238
239             pcelink = this.allPceLinks.get(pcelinkAtoZ.getOppositeLink());
240             LOG.debug("link to oppsite: {} to {}", pcelinkAtoZ, pcelink);
241
242             String srcName = pcelink.getSourceId().getValue();
243
244
245             // Nodes
246             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
247                 .resource.resource.resource.Node sourceNode = new NodeBuilder()
248                 .setNodeId(srcName).build();
249
250
251             // Source Resource
252             ZToAKey sourceKey = new ZToAKey(index.toString());
253             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
254             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
255             index += 1;
256             ztoaList.put(srcResource.key(),srcResource);
257
258             // source TP
259             tpName = pcelink.getSourceTP().toString();
260             stp = new TerminationPointBuilder()
261                     .setTpNodeId(srcName).setTpId(tpName)
262                     .build();
263
264             // Resource
265             ZToAKey srcTPKey = new ZToAKey(index.toString());
266             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
267             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
268             index += 1;
269             ztoaList.put(stpResource.key(),stpResource);
270
271             String linkName = pcelink.getLinkId().getValue();
272             // Link
273             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
274                 .resource.resource.resource.Link ztoaLink = new LinkBuilder()
275                 .setLinkId(linkName).build();
276
277             // Link Resource
278             ZToAKey linkKey = new ZToAKey(index.toString());
279             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).build();
280             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
281             index += 1;
282             ztoaList.put(linkResource.key(),linkResource);
283
284             String destName = pcelink.getDestId().getValue();
285             // target TP
286             tpName = pcelink.getDestTP().toString();
287             TerminationPoint ttp = new TerminationPointBuilder()
288                     .setTpNodeId(destName).setTpId(tpName).build();
289
290             // Resource
291             ZToAKey destTPKey = new ZToAKey(index.toString());
292             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
293             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
294             index += 1;
295             ztoaList.put(ttpResource.key(),ttpResource);
296
297
298             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
299                 .resource.resource.resource.Node targetNode = new NodeBuilder()
300                 .setNodeId(destName).build();
301             // Target Resource
302             ZToAKey targetKey = new ZToAKey(index.toString());
303             Resource nodeResource3 = new ResourceBuilder().setResource(targetNode).build();
304             lastResource = new ZToABuilder().setId(destName).withKey(targetKey).setResource(nodeResource3).build();
305
306             lastLink = pcelink;
307         }
308         if (lastResource != null) {
309             ztoaList.put(lastResource.key(),lastResource);
310         }
311
312         // build Z side Client TP
313         tpName = lastLink.getClient();
314         xname = lastLink.getDestId().getValue();
315         stp = new TerminationPointBuilder()
316                 .setTpNodeId(xname).setTpId(tpName).build();
317
318         index += 1;
319         clientKey = new ZToAKey(index.toString());
320         clientResource = new ResourceBuilder().setResource(stp).build();
321         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
322         ztoaList.put(lastResource.key(),lastResource);
323     }
324
325     public PceResult getReturnStructure() {
326         return rc;
327     }
328 }