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