Upgrade to OpenROADM 7.1.0 network models
[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 import org.opendaylight.transportpce.common.fixedflex.FixedGridConstant;
18 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
19 import org.opendaylight.transportpce.pce.SortPortsByName;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.TerminationPoint1;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.Node1;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.networks.network.node.termination.point.pp.attributes.UsedWavelength;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmNodeType;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmTpType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
29 import org.opendaylight.yangtools.yang.common.Uint16;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class PceOpticalNode implements PceNode {
34     private static final Logger LOG = LoggerFactory.getLogger(PceOpticalNode.class);
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 ServiceFormat serviceFormat;
42     private final String pceNodeType;
43
44     // wavelength calculation per node type
45     private List<Long> availableWLindex = new ArrayList<>();
46     private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<>();
47     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<>();
48     private List<String> usedXpndrNWTps = new ArrayList<>();
49     private List<PceLink> outgoingLinks = new ArrayList<>();
50     private Map<String, String> clientPerNwTp = new HashMap<>();
51     private final AvailFreqMapsKey freqMapKey = new AvailFreqMapsKey(GridConstant.C_BAND);
52
53     public PceOpticalNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId, ServiceFormat serviceFormat,
54         String pceNodeType) {
55         this.node = node;
56         this.nodeId = nodeId;
57         this.nodeType = nodeType;
58         this.serviceFormat = serviceFormat;
59         this.pceNodeType = pceNodeType;
60
61         if ((node == null) || (nodeId == null) || (nodeType == null)) {
62             LOG.error("PceNode: one of parameters is not populated : nodeId, node type");
63             this.valid = false;
64         }
65     }
66
67     public void initSrgTps() {
68         this.availableSrgPp.clear();
69         this.availableSrgCp.clear();
70         if (!isValid()) {
71             return;
72         }
73         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
74         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
75                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
76                     .ietf.network.topology.rev180226.Node1.class);
77         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
78             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
79         if (allTps.isEmpty()) {
80             LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this);
81             this.valid = false;
82             return;
83         }
84         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
85             .node.TerminationPoint tp : allTps) {
86             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
87             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.TerminationPoint1 nttp1 = tp
88                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529
89                 .TerminationPoint1.class);
90             OpenroadmTpType type = cntp1.getTpType();
91             LOG.info("type = {} for tp {}", type.getName(), tp);
92
93             switch (type) {
94                 case SRGTXRXCP:
95                 case SRGRXCP:
96                 case SRGTXCP:
97                     LOG.info("initSrgTpList: adding SRG-CP tp = {} ", tp.getTpId().getValue());
98                     this.availableSrgCp.put(tp.getTpId().getValue(), cntp1.getTpType());
99                     break;
100                 case SRGRXPP:
101                 case SRGTXPP:
102                 case SRGTXRXPP:
103                     boolean used = true;
104                     LOG.info("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
105                     try {
106                         List<UsedWavelength> usedWavelengths =
107                             new ArrayList<>(nttp1.getPpAttributes().getUsedWavelength().values());
108                         if (usedWavelengths.isEmpty()) {
109                             used = false;
110                         }
111                     } catch (NullPointerException e) {
112                         LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
113                         used = false;
114                     }
115                     if (!used) {
116                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
117                         this.availableSrgPp.put(tp.getTpId().getValue(), cntp1.getTpType());
118                     } else {
119                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!", tp.getTpId().getValue());
120                     }
121                     break;
122                 default:
123                     break;
124             }
125         }
126         if (this.availableSrgPp.isEmpty() || this.availableSrgCp.isEmpty()) {
127             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this);
128             this.valid = false;
129             return;
130         }
131         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}",
132             this.availableSrgPp.size(), this.availableSrgCp.size(), this);
133     }
134
135     public void initWLlist() {
136         this.availableWLindex.clear();
137         if (!isValid()) {
138             return;
139         }
140         Node1 node1 = this.node.augmentation(Node1.class);
141         byte[] freqMap;
142
143         switch (this.nodeType) {
144             case SRG :
145                 if (!node1.getSrgAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
146                     LOG.error("initWLlist: SRG no cband available freq maps for node  {}", this);
147                     this.valid = false;
148                     return;
149                 }
150                 freqMap = node1.getSrgAttributes().nonnullAvailFreqMaps().get(freqMapKey).getFreqMap();
151                 updateAvailableWlIndex(freqMap);
152                 break;
153             case DEGREE :
154                 if (!node1.getDegreeAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
155                     LOG.error("initWLlist: DEG no cband available freq maps for node  {}", this);
156                     this.valid = false;
157                     return;
158                 }
159                 freqMap = node1.getDegreeAttributes().nonnullAvailFreqMaps().get(freqMapKey).getFreqMap();
160                 updateAvailableWlIndex(freqMap);
161                 break;
162             case XPONDER :
163                 // HARD CODED 96
164                 for (long i = 1; i <= FixedGridConstant.NB_CHANNELS; i++) {
165                     this.availableWLindex.add(i);
166                 }
167                 break;
168             default:
169                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this);
170                 break;
171         }
172         if (this.availableWLindex.isEmpty()) {
173             LOG.debug("initWLlist: There are no available wavelengths in node {}", this);
174             this.valid = false;
175         }
176         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this);
177     }
178
179     public void initXndrTps() {
180         LOG.info("PceNod: initXndrTps for node : {}", this.nodeId);
181         if (!isValid()) {
182             return;
183         }
184         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
185                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
186                     .ietf.network.topology.rev180226.Node1.class);
187         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
188             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
189         if (allTps.isEmpty()) {
190             this.valid = false;
191             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
192             return;
193         }
194         this.valid = false;
195         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
196             .node.TerminationPoint tp : allTps) {
197             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
198             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.TerminationPoint1 nttp1 = tp
199                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529
200                 .TerminationPoint1.class);
201             if (cntp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
202                 if (nttp1 != null && nttp1.getXpdrNetworkAttributes().getWavelength() != null) {
203                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
204                     LOG.info("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
205                 } else {
206                     this.valid = true;
207                 }
208                 // find Client of this network TP
209                 String client;
210                 org.opendaylight.yang.gen.v1.http.transportpce.topology.rev201019.TerminationPoint1 tpceTp1 =
211                     tp.augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev201019
212                         .TerminationPoint1.class);
213                 if (tpceTp1 != null) {
214                     client = tpceTp1.getAssociatedConnectionMapPort();
215                     if (client != null) {
216                         this.clientPerNwTp.put(tp.getTpId().getValue(), client);
217                         this.valid = true;
218                     } else {
219                         LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}",
220                             this, tp.getTpId().getValue());
221                     }
222                 } else if (ServiceFormat.OTU.equals(this.serviceFormat)) {
223                     LOG.info("Infrastructure OTU4 connection");
224                     this.valid = true;
225                 } else {
226                     LOG.error("Service Format {} not managed yet", this.serviceFormat.getName());
227                 }
228             }
229         }
230         if (!isValid()) {
231             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this);
232         }
233     }
234
235     @Override
236     public String getRdmSrgClient(String tp) {
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", tp);
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, 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);
277             return null;
278         }
279     }
280
281
282     public void validateAZxponder(String anodeId, String znodeId) {
283         if (!isValid()) {
284             return;
285         }
286         if (this.nodeType != OpenroadmNodeType.XPONDER) {
287             return;
288         }
289         // Detect A and Z
290         if (this.getSupNetworkNodeId().equals(anodeId) || (this.getSupNetworkNodeId().equals(znodeId))) {
291             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
292             initXndrTps();
293             return;
294         }
295         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
296         valid = false;
297     }
298
299     @Override
300     public boolean checkTP(String tp) {
301         return !this.usedXpndrNWTps.contains(tp);
302     }
303
304     @Override
305     public boolean checkWL(long index) {
306         return (this.availableWLindex.contains(index));
307     }
308
309     public boolean isValid() {
310         if (node == null || nodeId == null || nodeType == null || this.getSupNetworkNodeId() == null
311             || this.getSupClliNodeId() == null) {
312             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
313             valid = false;
314         }
315         return valid;
316     }
317
318     @Override
319     public List<PceLink> getOutgoingLinks() {
320         return outgoingLinks;
321     }
322
323     @Override
324     public NodeId getNodeId() {
325         return nodeId;
326     }
327
328     @Override
329     public String toString() {
330         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
331     }
332
333     @Override
334     public String getPceNodeType() {
335         return this.pceNodeType;
336     }
337
338     @Override
339     public String getSupNetworkNodeId() {
340         return MapUtils.getSupNetworkNode(this.node);
341     }
342
343     @Override
344     public String getSupClliNodeId() {
345         return MapUtils.getSupClliNode(this.node);
346     }
347
348     @Override
349     public void addOutgoingLink(PceLink outLink) {
350         this.outgoingLinks.add(outLink);
351     }
352
353     @Override
354     public String getXpdrClient(String tp) {
355         return this.clientPerNwTp.get(tp);
356     }
357
358     @Override
359     public Map<String, List<Uint16>> getAvailableTribPorts() {
360         return null;
361     }
362
363     @Override
364     public Map<String, List<Uint16>> getAvailableTribSlots() {
365         return null;
366     }
367
368     /**
369      * Get available wave length from frequency map array.
370      * @param freqMap byte[]
371      */
372     private void updateAvailableWlIndex(byte[] freqMap) {
373         if (freqMap == null) {
374             LOG.warn("No frequency map for node {}", node);
375             this.valid = false;
376             return;
377         }
378         long wlIndex = 1;
379         for (int i = 0; i < freqMap.length; i++) {
380             if (freqMap[i] == (byte)GridConstant.AVAILABLE_SLOT_VALUE) {
381                 LOG.debug("Adding channel {} to available wave length index",wlIndex);
382                 this.availableWLindex.add(wlIndex);
383             }
384             wlIndex++;
385         }
386     }
387 }