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