27d8d1c30eed24a4a3cb96502968f7d56cf19cd4
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceNode.java
1 /*
2  * Copyright © 2017 AT&T, 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 package org.opendaylight.transportpce.pce;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Optional;
15 import java.util.TreeMap;
16
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.Node1;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.TerminationPoint1;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.network.node.termination.point.cp.attributes.UsedWavelengths;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev170929.OpenroadmNodeType;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev170929.OpenroadmTpType;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.network.Node;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class PceNode {
28     /* Logging. */
29     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
30     ////////////////////////// NODES ///////////////////////////
31     /*
32      */
33     private boolean valid = true;
34     private final Node node;
35     private final NodeId nodeId;
36     private final OpenroadmNodeType nodeType;
37     // wavelength calculation per node type
38     private List<Long> availableWLindex = new ArrayList<Long>();
39     // private Set<String> availableSrgPp = new TreeSet<String>();
40     private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<String, OpenroadmTpType>();
41     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<String, OpenroadmTpType>();
42     private List<String> usedXpndrNWTps = new ArrayList<String>();
43     private List<String> usedSrgPP = new ArrayList<String>();
44     private List<PceLink> outgoingLinks = new ArrayList<PceLink>();
45     private Map<String, String> clientPerNwTp = new HashMap<String, String>();
46     private Map<String, String> clientPerPpTp = new HashMap<String, String>();
47
48     public PceNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId) {
49         this.node = node;
50         this.nodeId = nodeId;
51         this.nodeType = nodeType;
52         if ((node == null) || (nodeId == null) || (nodeType == null)) {
53             LOG.error("PceNode: one of parameters is not populated : nodeId, node type");
54             this.valid = false;
55         }
56     }
57
58     public void initSrgTpList() {
59         this.availableSrgPp.clear();
60         this.availableSrgCp.clear();
61         if (!isValid()) {
62             return;
63         }
64         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
65         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Node1 nodeTp =
66                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
67                                 .rev150608.Node1.class);
68         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
69             .TerminationPoint> allTps =
70                 nodeTp.getTerminationPoint();
71         if (allTps == null) {
72             LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this.toString());
73             this.valid = false;
74             return;
75         }
76         boolean used;
77         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
78                 .TerminationPoint tp : allTps) {
79             used = true;
80             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
81             try {
82                 List<UsedWavelengths> usedWavelengths = tp1.getCpAttributes().getUsedWavelengths();
83                 if (usedWavelengths.isEmpty()) {
84                     LOG.info("initSrgTpList: SRG-CP tp = {} found", tp.getTpId().getValue());
85                     used = false;
86                 }
87             } catch (NullPointerException e) {
88                 LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
89                 used = false;
90             }
91             if (!used) {
92                 if (tp1.getTpType().getName().contains("-PP")) {
93                     LOG.info("initSrgTpList: adding tp '{}'", tp1.getTpType());
94                     this.availableSrgPp.put(tp.getTpId().getValue(), tp1.getTpType());
95                 } else if (tp1.getTpType().getName().contains("-CP")) {
96                     this.availableSrgCp.put(tp.getTpId().getValue(), tp1.getTpType());
97                 }
98             }
99         }
100         if (this.availableSrgPp.isEmpty() && this.availableSrgCp.isEmpty()) {
101             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this.toString());
102             this.valid = false;
103             return;
104         }
105         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}", this.availableSrgPp
106                 .size(), this.availableSrgCp.size(), this.toString());
107         return;
108     }
109
110     public void initWLlist() {
111         this.availableWLindex.clear();
112         if (!isValid()) {
113             return;
114         }
115         Node1 node1 = this.node.augmentation(Node1.class);
116         switch (this.nodeType) {
117             case SRG :
118                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev170929.srg.node.attributes
119                     .AvailableWavelengths> srgAvailableWL =
120                         node1.getSrgAttributes().getAvailableWavelengths();
121                 if (srgAvailableWL == null) {
122                     this.valid = false;
123                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this.toString());
124                     return;
125                 }
126                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev170929.srg.node.attributes
127                         .AvailableWavelengths awl : srgAvailableWL) {
128                     this.availableWLindex.add(awl.getIndex());
129                     LOG.debug("initWLlist: SRG next = {} in {}", awl.getIndex(), this.toString());
130                 }
131                 break;
132             case DEGREE :
133                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev170929.degree.node.attributes
134                     .AvailableWavelengths> degAvailableWL = node1.getDegreeAttributes().getAvailableWavelengths();
135                 if (degAvailableWL == null) {
136                     this.valid = false;
137                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this.toString());
138                     return;
139                 }
140                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev170929.degree.node.attributes
141                             .AvailableWavelengths awl : degAvailableWL) {
142                     this.availableWLindex.add(awl.getIndex());
143                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this.toString());
144                 }
145                 break;
146             case XPONDER :
147                 // HARD CODED 96
148                 for (long i = 1; i <= 96; i++) {
149                     this.availableWLindex.add(i);
150                 }
151                 break;
152             default:
153                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this.toString());
154                 break;
155         }
156         if (this.availableWLindex.size() == 0) {
157             LOG.debug("initWLlist: There are no available wavelengths in node {}", this.toString());
158             this.valid = false;
159         }
160         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this.toString());
161         return;
162     }
163
164     public void initXndrTps() {
165         LOG.info("initXndrTps for node : {}", this.nodeId);
166         if (!isValid()) {
167             return;
168         }
169         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Node1 nodeTp =
170                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
171                         .rev150608.Node1.class);
172         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
173             .TerminationPoint> allTps = nodeTp.getTerminationPoint();
174         if (allTps == null) {
175             this.valid = false;
176             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this.toString());
177             return;
178         }
179         this.valid = false;
180         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
181                     .TerminationPoint tp : allTps) {
182             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
183             if (tp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
184                 if (tp1.getXpdrNetworkAttributes().getWavelength() != null) {
185                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
186                     LOG.debug("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
187                 } else {
188                     this.valid = true;
189                 }
190                 // find Client of this network TP
191                 String client = tp1.getXpdrNetworkAttributes().getTailEquipmentId();
192                 if ((client.equals("")) || (client == null)) {
193                     LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}", this.toString(), tp
194                             .getTpId().getValue());
195                     this.valid = false;
196                 }
197                 this.clientPerNwTp.put(tp.getTpId().getValue(), client);
198             }
199         }
200         if (!isValid()) {
201             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this.toString());
202             return;
203         }
204     }
205
206     public void initRdmSrgTps(Boolean aend) {
207         LOG.info("initRdmSrgTps for node : {}", this.nodeId);
208         initSrgTpList();
209         if (!isValid()) {
210             return;
211         }
212         this.valid = false;
213         Optional<String> optTp = null;
214         OpenroadmTpType srgType = OpenroadmTpType.SRGTXRXPP;
215         OpenroadmTpType oppositeSrgType = null;
216         Optional<String> oppositeTp = null;
217         boolean unidir = false;
218         optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXRXCP)
219                 .map(Map.Entry::getKey).findFirst();
220         if (!optTp.isPresent()) {
221             srgType = null;
222             unidir = true;
223             LOG.info("UNI Directional ports ...");
224             if (aend) {
225                 LOG.info("Tx port ...");
226                 optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXCP)
227                         .map(Map.Entry::getKey).findFirst();
228                 srgType = OpenroadmTpType.SRGRXPP;
229                 oppositeSrgType = OpenroadmTpType.SRGTXPP;
230                 oppositeTp = this.availableSrgCp.entrySet().stream()
231                         .filter(cp -> cp.getValue() == OpenroadmTpType.SRGRXCP).map(Map.Entry::getKey).findFirst();
232             } else {
233                 LOG.info("Rx port ...");
234                 optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGRXCP)
235                         .map(Map.Entry::getKey).findFirst();
236                 srgType = OpenroadmTpType.SRGTXPP;
237                 oppositeSrgType = OpenroadmTpType.SRGRXPP;
238                 oppositeTp = this.availableSrgCp.entrySet().stream()
239                         .filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXCP).map(Map.Entry::getKey).findFirst();
240             }
241         } else {
242             LOG.info("BI Directional ports ...");
243         }
244         if (optTp.isPresent() && (srgType != null)) {
245             String tp = optTp.get();
246             if (!this.availableSrgPp.isEmpty()) {
247                 LOG.info("finding PP for CP {}", optTp.get());
248                 Optional<String> client = null;
249                 final OpenroadmTpType openType = srgType;
250                 client = this.availableSrgPp.entrySet().stream().filter(pp -> pp.getValue() == openType)
251                         .map(Map.Entry::getKey).findFirst();
252                 if (!client.isPresent()) {
253                     LOG.error("initRdmSrgTps: ROADM {} doesn't have defined Client {}", this.toString(), tp);
254                     this.valid = false;
255                     return;
256                 }
257                 if (unidir) {
258                     final OpenroadmTpType oppositeOpType = oppositeSrgType;
259                     String opTp = oppositeTp.get();
260                     Optional<String> oppositeClient = this.availableSrgPp.entrySet().stream()
261                             .filter(pp -> pp.getValue() == oppositeOpType)
262                             .map(Map.Entry::getKey).findFirst();
263                     if (!oppositeClient.isPresent()) {
264                         LOG.error("initRdmSrgTps: ROADM {} doesn't have defined opposite Client {}",
265                                 this.toString(), tp);
266                         this.valid = false;
267                         return;
268                     }
269                     this.clientPerPpTp.put(opTp, oppositeClient.get());
270                     LOG.info("initRdmSrgTps: client PP {} for oposite CP {} found !", client, tp);
271                 }
272                 this.valid = true;
273                 this.clientPerPpTp.put(tp, client.get());
274                 LOG.info("initRdmSrgTps: client PP {} for CP {} found !", client, tp);
275             }
276         }
277         if (!isValid()) {
278             this.valid = false;
279             LOG.error("initRdmSrgTps: SRG TerminationPoint list is empty for node {}", this.toString());
280             return;
281         }
282     }
283
284     public boolean checkTP(String tp) {
285         if (this.usedXpndrNWTps.contains(tp)) {
286             return false;
287         }
288         return true;
289     }
290
291     public boolean checkWL(long index) {
292         if (this.availableWLindex.contains(index)) {
293             return true;
294         }
295         return false;
296     }
297
298     public boolean isValid() {
299         return this.valid;
300     }
301
302     public List<Long> getAvailableWLs() {
303         return this.availableWLindex;
304     }
305
306     public void addOutgoingLink(PceLink outLink) {
307         this.outgoingLinks.add(outLink);
308     }
309
310     public List<PceLink> getOutgoingLinks() {
311         return this.outgoingLinks;
312     }
313
314     public String getXpdrClient(String tp) {
315         return this.clientPerNwTp.get(tp);
316     }
317
318     public String getRdmSrgClient(String tp) {
319         LOG.info("Getting ROADM Client PP for CP {} : {}", tp, this.clientPerPpTp.get(tp));
320         return this.clientPerPpTp.get(tp);
321     }
322
323     @Override
324     public String toString() {
325         return "PceNode type=" + this.nodeType + " ID=" + this.nodeId.getValue();
326     }
327 }