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