modify pce module folder structure
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceNode.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
9 package org.opendaylight.transportpce.pce.networkanalyzer;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import java.util.TreeMap;
17
18 import org.opendaylight.transportpce.pce.SortPortsByName;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Node1;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.TerminationPoint1;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.node.termination.point.pp.attributes.UsedWavelength;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmTpType;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class PceNode {
30     /* Logging. */
31     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
32     ////////////////////////// NODES ///////////////////////////
33     /*
34      */
35
36     private boolean valid = true;
37
38     private final Node node;
39     private final NodeId nodeId;
40     private final OpenroadmNodeType nodeType;
41     private final String supNodeId;
42     private final String clli;
43
44     // wavelength calculation per node type
45     private List<Long> availableWLindex = new ArrayList<Long>();
46     private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<String, OpenroadmTpType>();
47     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<String, OpenroadmTpType>();
48     private List<String> usedXpndrNWTps = new ArrayList<String>();
49     private List<PceLink> outgoingLinks = new ArrayList<PceLink>();
50     private Map<String, String> clientPerNwTp = new HashMap<String, String>();
51
52     public PceNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId) {
53         this.node = node;
54         this.nodeId = nodeId;
55         this.nodeType = nodeType;
56         this.supNodeId = getSupNodeId(node);
57         this.clli = MapUtils.getCLLI(node);
58
59         if ((node == null) || (nodeId == null) || (nodeType == null)) {
60             LOG.error("PceNode: one of parameters is not populated : nodeId, node type");
61             this.valid = false;
62         }
63     }
64
65     public void initSrgTps() {
66         this.availableSrgPp.clear();
67         this.availableSrgCp.clear();
68         if (!isValid()) {
69             return;
70         }
71         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
72         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
73                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
74                                 .rev180226.Node1.class);
75         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
76             .node.TerminationPoint> allTps =
77                 nodeTp.getTerminationPoint();
78         if (allTps == null) {
79             LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this.toString());
80             this.valid = false;
81             return;
82         }
83         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
84             .node.TerminationPoint tp : allTps) {
85             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
86             OpenroadmTpType type = tp1.getTpType();
87             switch (type) {
88                 case SRGTXRXCP:
89                 case SRGRXCP:
90                 case SRGTXCP:
91                     LOG.info("initSrgTpList: adding SRG-CP tp = {} ", tp.getTpId().getValue());
92                     this.availableSrgCp.put(tp.getTpId().getValue(), tp1.getTpType());
93                     break;
94                 case SRGRXPP:
95                 case SRGTXPP:
96                 case SRGTXRXPP:
97                     boolean used = true;
98                     LOG.info("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
99                     try {
100                         List<UsedWavelength> usedWavelengths = tp1.getPpAttributes().getUsedWavelength();
101                         if (usedWavelengths.isEmpty()) {
102                             used = false;
103                         }
104                     } catch (NullPointerException e) {
105                         LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
106                         used = false;
107                     }
108                     if (!used) {
109                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
110                         this.availableSrgPp.put(tp.getTpId().getValue(), tp1.getTpType());
111                     } else {
112                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!");
113                     }
114                     break;
115                 default:
116                     break;
117             }
118         }
119         if (this.availableSrgPp.isEmpty() && this.availableSrgCp.isEmpty()) {
120             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this.toString());
121             this.valid = false;
122             return;
123         }
124         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}", this.availableSrgPp
125                 .size(), this.availableSrgCp.size(), this.toString());
126         return;
127     }
128
129
130
131 /*    public PceNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId,
132             String supNodeId, String clli) {
133         this.node = node;
134         this.nodeId = nodeId;
135         this.nodeType = nodeType;
136         this.supNodeId = supNodeId;
137         this.clli = clli;
138
139         if ((node == null) || (nodeId == null) || (nodeType == null)
140                 || (supNodeId == null) || (clli == null)) {
141             LOG.error(
142                     "PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
143             valid = false;
144         }
145
146         LOG.debug(" PceNode built :{}", this.toString());
147     }
148 */
149     public void initWLlist() {
150         this.availableWLindex.clear();
151         if (!isValid()) {
152             return;
153         }
154         Node1 node1 = this.node.augmentation(Node1.class);
155         switch (this.nodeType) {
156             case SRG :
157                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
158                     .AvailableWavelengths> srgAvailableWL =
159                         node1.getSrgAttributes().getAvailableWavelengths();
160                 if (srgAvailableWL == null) {
161                     this.valid = false;
162                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this.toString());
163                     return;
164                 }
165                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
166                         .AvailableWavelengths awl : srgAvailableWL) {
167                     this.availableWLindex.add(awl.getIndex());
168                     LOG.debug("initWLlist: SRG next = {} in {}", awl.getIndex(), this.toString());
169                 }
170                 break;
171             case DEGREE :
172                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
173                     .AvailableWavelengths> degAvailableWL = node1.getDegreeAttributes().getAvailableWavelengths();
174                 if (degAvailableWL == null) {
175                     this.valid = false;
176                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this.toString());
177                     return;
178                 }
179                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
180                             .AvailableWavelengths awl : degAvailableWL) {
181                     this.availableWLindex.add(awl.getIndex());
182                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this.toString());
183                 }
184                 break;
185             case XPONDER :
186                 // HARD CODED 96
187                 for (long i = 1; i <= 96; i++) {
188                     this.availableWLindex.add(i);
189                 }
190                 break;
191             default:
192                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this.toString());
193                 break;
194         }
195         if (this.availableWLindex.size() == 0) {
196             LOG.debug("initWLlist: There are no available wavelengths in node {}", this.toString());
197             this.valid = false;
198         }
199         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this.toString());
200         return;
201     }
202
203     public void initXndrTps() {
204         LOG.info("initXndrTps for node : {}", this.nodeId);
205         if (!isValid()) {
206             return;
207         }
208         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
209                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
210                         .rev180226.Node1.class);
211         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
212             .node.TerminationPoint> allTps = nodeTp.getTerminationPoint();
213         if (allTps == null) {
214             this.valid = false;
215             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this.toString());
216             return;
217         }
218         this.valid = false;
219         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
220             .node.TerminationPoint tp : allTps) {
221             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
222             if (tp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
223                 if (tp1.getXpdrNetworkAttributes().getWavelength() != null) {
224                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
225                     LOG.debug("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
226                 } else {
227                     this.valid = true;
228                 }
229                 // find Client of this network TP
230                 String client = tp1.getXpdrNetworkAttributes().getTailEquipmentId();
231                 if ((client.equals("")) || (client == null)) {
232                     LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}", this.toString(), tp
233                             .getTpId().getValue());
234                     this.valid = false;
235                 }
236                 this.clientPerNwTp.put(tp.getTpId().getValue(), client);
237             }
238         }
239         if (!isValid()) {
240             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this.toString());
241             return;
242         }
243     }
244
245     public String getRdmSrgClient(String tp, Boolean aend) {
246         LOG.info("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
247         OpenroadmTpType srgType = null;
248         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
249         if (cpType == null) {
250             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list");
251             return null;
252         }
253         switch (cpType) {
254             case SRGTXRXCP:
255                 LOG.info("getRdmSrgClient: Getting BI Directional PP port ...");
256                 srgType = OpenroadmTpType.SRGTXRXPP;
257                 break;
258             case SRGTXCP:
259                 LOG.info("getRdmSrgClient: Getting UNI Rx PP port ...");
260                 srgType = OpenroadmTpType.SRGRXPP;
261                 break;
262             case SRGRXCP:
263                 LOG.info("getRdmSrgClient: Getting UNI Tx PP port ...");
264                 srgType = OpenroadmTpType.SRGTXPP;
265                 break;
266             default:
267                 break;
268         }
269         LOG.info("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
270         if (!this.availableSrgPp.isEmpty()) {
271             Optional<String> client = null;
272             final OpenroadmTpType openType = srgType;
273             client = this.availableSrgPp.entrySet().stream().filter(pp -> pp.getValue().getName() == openType.getName())
274                     .map(Map.Entry::getKey)
275                     .sorted(new SortPortsByName())
276                     .findFirst();
277             if (!client.isPresent()) {
278                 LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this.toString(), tp);
279                 return null;
280             }
281             LOG.info("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
282             return client.get();
283         } else {
284             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this.toString());
285             return null;
286         }
287     }
288
289     private String getSupNodeId(Node inputNode) {
290         String tempSupId = "";
291         // TODO: supporting IDs exist as a List. this code takes just the
292         // first element
293         tempSupId = MapUtils.getSupNode(inputNode);
294         if (tempSupId.equals("")) {
295             LOG.error("getSupNodeId: Empty Supporting node for node: [{}]. Node is ignored", inputNode.getNodeId());
296         }
297         return tempSupId;
298     }
299
300     public void validateAZxponder(String anodeId, String znodeId) {
301         if (!isValid()) {
302             return;
303         }
304
305         if (this.nodeType != OpenroadmNodeType.XPONDER) {
306             return;
307         }
308
309         // Detect A and Z
310         if (this.supNodeId.equals(anodeId) || (this.supNodeId.equals(znodeId))) {
311
312             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
313             initXndrTps();
314             return;
315         }
316
317         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
318         valid = false;
319     }
320
321     public String getXpdrClient(String tp) {
322         return this.clientPerNwTp.get(tp);
323     }
324
325     public boolean checkTP(String tp) {
326         return !(this.usedXpndrNWTps.contains(tp));
327     }
328
329     public boolean checkWL(long index) {
330         return (this.availableWLindex.contains(index));
331     }
332
333     public boolean isValid() {
334         if ((node == null) || (nodeId == null) || (nodeType == null) || (supNodeId == null) || (clli == null)) {
335             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
336             valid = false;
337         }
338         return valid;
339     }
340
341     public List<Long> getAvailableWLs() {
342         return availableWLindex;
343     }
344
345     public void addOutgoingLink(PceLink outLink) {
346         this.outgoingLinks.add(outLink);
347     }
348
349     public List<PceLink> getOutgoingLinks() {
350         return outgoingLinks;
351     }
352
353     public String getClient(String tp) {
354         return clientPerNwTp.get(tp);
355     }
356
357     public NodeId getNodeId() {
358         return nodeId;
359     }
360
361     public String getSupNodeIdPceNode() {
362         return supNodeId;
363     }
364
365     public String getCLLI() {
366         return clli;
367     }
368
369     public String toString() {
370         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + clli;
371     }
372
373     public void printLinksOfNode() {
374         LOG.info(" outgoing links of node {} : {} ", nodeId.getValue(), this.getOutgoingLinks().toString());
375     }
376
377 }