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