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