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