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