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