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