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