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