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