Pathdescription Update
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / topology / NodePath.java
1 /*
2  * Copyright © 2017 Orange, 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
9 package org.opendaylight.transportpce.stubpce.topology;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.apache.commons.lang3.StringUtils;
15 import org.opendaylight.transportpce.stubpce.TpNodeTp;
16 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Link;
17 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.LinkBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.Node;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.NodeBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.TerminationPoint;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.TerminationPointBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.link.LinkIdentifierBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.node.NodeIdentifierBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.pce.resource.resource.resource.termination.point.TerminationPointIdentifierBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * build all path with one resource
31  * from Supernode.
32  *
33  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
34  *         behalf of Orange
35  */
36 public class NodePath {
37     /** Logging. */
38     private static final Logger LOG = LoggerFactory.getLogger(NodePath.class);
39     /** element of Supernode. */
40     private org.opendaylight.transportpce.stubpce.topology.Resource resource;
41     /** <code>TpNodeTp</code> list. */
42     private List<TpNodeTp> tpNodeTps;
43     /** <code>Link</code> list. */
44     private List<Link> links;
45     /** SuperNode element nodeId. */
46     private String nodeId;
47     /** Supernode Id. */
48     private String superNodeId;
49     /** boolean to determine if Supernode contains an XPDR. */
50     private boolean isXpdrSrgAbsent;
51     /** Path List. */
52     private List<Path> path;
53
54     /**
55      *NodePath constructor.
56      *
57      * @param res element of Supernode
58      * @param superNodeId supernode Id
59      * @param isXpdrSrgAbsent boolean to determine if Supernode contains an XPDR
60      */
61     public NodePath(org.opendaylight.transportpce.stubpce.topology.Resource res,
62             String superNodeId, boolean isXpdrSrgAbsent) {
63         setResource(res);
64         setNodeId(res.getNodeId());
65         setSuperNodeId(superNodeId);
66         setXpdrSrgAbsent(isXpdrSrgAbsent);
67         setLinks(new ArrayList<Link>());
68         setTpNodeTps(new ArrayList<TpNodeTp>());
69         setPath(new ArrayList<Path>());
70     }
71
72     @Override
73     public String toString() {
74         java.lang.String name = "NodePath [";
75         java.lang.StringBuilder builder = new java.lang.StringBuilder(name);
76         if (nodeId != null) {
77             builder.append("nodeId= ");
78             builder.append(nodeId);
79             builder.append(", ");
80         }
81         if (path.size() > 0) {
82             builder.append("Paths [");
83             builder.append(path.toString());
84             builder.append(" ]");
85         }
86         return builder.append(']').toString();
87     }
88
89     /**
90      *get all resources (links,
91      *logicalConnectionpoints, nodeId)
92      *from Supernode element.
93      */
94     public void fill() {
95         Boolean xpdr = false;
96         if (resource != null) {
97             String nodeid = resource.getNodeId();
98             List<String> resLinks = resource.getLinks();
99             List<LogicalConnectionPoint> resLcps = resource.getLcps();
100             for (String tmp : resLinks) {
101                 Link link = new LinkBuilder()
102                         .setLinkIdentifier(new LinkIdentifierBuilder()
103                                 .setLinkId(tmp)
104                                 .build())
105                         .build();
106                 links.add(link);
107             }
108             if (nodeid.contains("XPDR")) {
109                 LOG.info("building xpdr resource ...");
110                 xpdr = true;
111                 buildXpdrTopo(nodeid,resLinks,resLcps,xpdr);
112                 links.add(new LinkBuilder()
113                         .setLinkIdentifier(new LinkIdentifierBuilder()
114                                 .setLinkId("TAIL-LINKS")
115                                 .build())
116                         .build());
117             }
118             if (nodeid.contains("SRG")) {
119                 LOG.info("building SRG resource ...");
120                 buildSRGTopo(nodeid, resLinks, resLcps);
121             }
122             if (nodeid.contains("DEG")) {
123                 LOG.info("building DEG resource ...");
124                 xpdr = false;
125                 buildXpdrTopo(nodeid,resLinks,resLcps,xpdr);
126                 links.add(new LinkBuilder()
127                         .setLinkIdentifier(new LinkIdentifierBuilder()
128                                 .setLinkId("EXTERNAL-LINKS")
129                                 .build())
130                         .build());
131             }
132         }
133         createDirection();
134     }
135
136     /**
137      * reverse a TpNodeTp
138      * List.
139      *
140      * @return TpNodeTp list
141      */
142     private List<TpNodeTp> reverseTpNodetpList() {
143         List<TpNodeTp> result = new ArrayList<TpNodeTp>();
144         for (TpNodeTp tpNodetp : tpNodeTps) {
145             TpNodeTp tmp = tpNodetp.reverse();
146             result.add(tmp);
147         }
148         return result;
149     }
150
151     /**
152      *create list of Path
153      *(TpNodeTp/Link).
154      */
155     private void createDirection() {
156         /** create direction */
157         LOG.info("creating direction ...");
158         Path resourcePath = null;
159         List<TpNodeTp> direction = new ArrayList<TpNodeTp>();
160         for (Link link : links) {
161             String linkId = link.getLinkIdentifier().getLinkId();
162             LOG.info("LinkId : {}", linkId);
163             if (!isXpdrSrgAbsent) {
164                 if (StringUtils.countMatches(link.getLinkIdentifier().getLinkId(), "ROADM") < 2) {
165                     if ((linkId.contains("XPDR") && linkId.startsWith("ROADM"))
166                         || ((linkId.startsWith("DEG") && linkId.contains("SRG")))
167                         || (nodeId.contains("XPDR") && linkId.contains("TAIL-LINKS"))) {
168                         LOG.info("reversing TpNodetp list for link '{}'", linkId);
169                         direction = reverseTpNodetpList();
170                     } else {
171                         direction = tpNodeTps;
172                     }
173                 } else {
174                     LOG.info("link is deg to deg link !");
175                 }
176             } else {
177                 if (StringUtils.countMatches(link.getLinkIdentifier().getLinkId(), "ROADM") == 2) {
178                     direction = reverseTpNodetpList();
179                 } else {
180                     direction = tpNodeTps;
181                 }
182             }
183             if (tpNodeTps.size() > 0) {
184                 for (TpNodeTp tpNodeTp : direction) {
185                     resourcePath = new Path(tpNodeTp, link);
186                     LOG.info(resourcePath.toString());
187                     path.add(resourcePath);
188                 }
189             } else {
190                 LOG.info("tpNodeTps is empty !");
191             }
192         }
193     }
194
195     /**
196      * build a list of
197      * Termination Point in
198      * XPDR or DEG
199      * ordered in AToZdirection
200      * on a Supernode (XPDR to SRG)
201      * (ROADMA-DEG to ROADMZ-DEG).
202      *
203      * @param xpdr determine if it is an XPDR or not
204      * @param resLcps list of LogicalConnectionPoint
205      * @return String list of TerminationPoints
206      */
207     private List<String> getOrderedTps(boolean xpdr, List<LogicalConnectionPoint> resLcps) {
208         List<String> result = new ArrayList<String>();
209         if (xpdr) {
210             for (LogicalConnectionPoint lcp : resLcps) {
211                 String tmp = lcp.getTpId();
212                 if (tmp.contains("CLIENT")) {
213                     result.add(0, tmp);
214                 } else if (tmp.contains("NETWORK")) {
215                     result.add(1, tmp);
216                 }
217             }
218         } else {
219             for (LogicalConnectionPoint lcp : resLcps) {
220                 String tmp = lcp.getTpId();
221                 if (tmp.contains("CTP")) {
222                     result.add(0, tmp);
223                 } else if (tmp.contains("TTP")) {
224                     result.add(1, tmp);
225                 }
226             }
227         }
228         return result;
229     }
230
231     /**
232      * build TpNodeTp
233      * structure in an
234      * XPDR.
235      *
236      * @param nodeid XPDR Id
237      * @param resLinks list of links
238      * @param resLcps list of LogicalConnectionPoints
239      * @param xpdr determine if it is an XPDR or not
240      */
241     private void buildXpdrTopo(String nodeid, List<String> resLinks, List<LogicalConnectionPoint> resLcps,
242             boolean xpdr) {
243        /** build TpNodetp .*/
244         TerminationPointBuilder in = null;
245         TerminationPointBuilder out = null;
246         List<String> lcps = getOrderedTps(xpdr,resLcps);
247         if (lcps.size() == 2) {
248             in = new TerminationPointBuilder()
249                     .setTerminationPointIdentifier(new TerminationPointIdentifierBuilder()
250                             .setNodeId(nodeid)
251                             .setTpId(lcps.get(0))
252                             .build());
253             out = new TerminationPointBuilder()
254                     .setTerminationPointIdentifier(new TerminationPointIdentifierBuilder()
255                             .setNodeId(nodeid)
256                             .setTpId(lcps.get(1))
257                             .build());
258         } else {
259             LOG.info("lcps size not equal to 2");
260
261         }
262         Node node = new NodeBuilder()
263                 .setNodeIdentifier(new NodeIdentifierBuilder()
264                         .setNodeId(nodeid)
265                         .build())
266                 .build();
267         TpNodeTp tmp = new TpNodeTp(in.build(), out.build(), node);
268         tpNodeTps.add(tmp);
269     }
270
271     /**
272      * build TpNodeTp
273      * structure in an
274      * SGR.
275      *
276      * @param nodeid SRG nodeId
277      * @param resLinks list of links
278      * @param resLcps list of LogicalConnectionPoints
279      */
280     private void buildSRGTopo(String nodeid, List<String> resLinks,List<LogicalConnectionPoint> resLcps) {
281         /** build TpNodetp .*/
282         Node node = new NodeBuilder()
283                 .setNodeIdentifier(new NodeIdentifierBuilder()
284                         .setNodeId(nodeid)
285                         .build())
286                 .build();
287         TerminationPoint out = new TerminationPointBuilder()
288                 .setTerminationPointIdentifier(new TerminationPointIdentifierBuilder()
289                         .setNodeId(nodeid)
290                         .setTpId("SRG1-CP-TXRX")
291                         .build())
292                 .build();
293         for (LogicalConnectionPoint lcp : resLcps) {
294             if (lcp.getTpId().compareTo("SRG1-CP-TXRX") != 0) {
295                 TerminationPoint in = new TerminationPointBuilder()
296                         .setTerminationPointIdentifier(new TerminationPointIdentifierBuilder()
297                                 .setNodeId(nodeid)
298                                 .setTpId(lcp.getTpId())
299                                 .build())
300                         .build();
301                 tpNodeTps.add(new TpNodeTp(in, out, node));
302             }
303         }
304     }
305
306     public static void main(String[] args) {
307         Topology topo = new Topology();
308         topo.start();
309         Network net = topo.getNetwork();
310         if (net != null) {
311             SuperNode superNode = net.getSuperNodes().get(0);
312             LOG.info("SuperNode : {}", superNode.getSuperNodeId());
313             for (org.opendaylight.transportpce.stubpce.topology.Resource res :
314                 superNode.getResources()) {
315                 LOG.info(res.toString());
316                 NodePath path = new NodePath(res, superNode.getSuperNodeId(), superNode.isXpdrSrgAbsent());
317                 path.fill();
318             }
319         }
320     }
321
322     public org.opendaylight.transportpce.stubpce.topology.Resource getResource() {
323         return resource;
324     }
325
326     public void setResource(org.opendaylight.transportpce.stubpce.topology.Resource resource) {
327         this.resource = resource;
328     }
329
330     public List<TpNodeTp> getTpNodeTps() {
331         return tpNodeTps;
332     }
333
334     public void setTpNodeTps(List<TpNodeTp> tpNodeTps) {
335         this.tpNodeTps = tpNodeTps;
336     }
337
338     public List<Link> getLinks() {
339         return links;
340     }
341
342     public void setLinks(List<Link> links) {
343         this.links = links;
344     }
345
346     public List<Path> getPath() {
347         return path;
348     }
349
350     public void setPath(List<Path> path) {
351         this.path = path;
352     }
353
354     public String getNodeId() {
355         return nodeId;
356     }
357
358     public void setNodeId(String nodeId) {
359         this.nodeId = nodeId;
360     }
361
362     public String getSuperNodeId() {
363         return superNodeId;
364     }
365
366     public void setSuperNodeId(String superNodeId) {
367         this.superNodeId = superNodeId;
368     }
369
370     public boolean isXpdrSrgAbsent() {
371         return isXpdrSrgAbsent;
372     }
373
374     public void setXpdrSrgAbsent(boolean isXpdrSrgAbsent) {
375         this.isXpdrSrgAbsent = isXpdrSrgAbsent;
376     }
377 }