57797640ea7e1bfe5bf26e0dc69a61afa79acf3c
[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.rev181130.Node1;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.TerminationPoint1;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.node.termination.point.pp.attributes.UsedWavelength;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmTpType;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.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 Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<String, OpenroadmTpType>();
40     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<String, OpenroadmTpType>();
41     private List<String> usedXpndrNWTps = new ArrayList<String>();
42     private List<PceLink> outgoingLinks = new ArrayList<PceLink>();
43     private Map<String, String> clientPerNwTp = new HashMap<String, String>();
44
45     public PceNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId) {
46         this.node = node;
47         this.nodeId = nodeId;
48         this.nodeType = nodeType;
49         if ((node == null) || (nodeId == null) || (nodeType == null)) {
50             LOG.error("PceNode: one of parameters is not populated : nodeId, node type");
51             this.valid = false;
52         }
53     }
54
55     public void initSrgTps() {
56         this.availableSrgPp.clear();
57         this.availableSrgCp.clear();
58         if (!isValid()) {
59             return;
60         }
61         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
62         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
63                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
64                                 .rev180226.Node1.class);
65         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
66             .node.TerminationPoint> allTps =
67                 nodeTp.getTerminationPoint();
68         if (allTps == null) {
69             LOG.error("initSrgTpList: 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.rev180226.networks.network
74             .node.TerminationPoint tp : allTps) {
75             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
76             OpenroadmTpType type = tp1.getTpType();
77             switch (type) {
78                 case SRGTXRXCP:
79                 case SRGRXCP:
80                 case SRGTXCP:
81                     LOG.info("initSrgTpList: adding SRG-CP tp = {} ", tp.getTpId().getValue());
82                     this.availableSrgCp.put(tp.getTpId().getValue(), tp1.getTpType());
83                     break;
84                 case SRGRXPP:
85                 case SRGTXPP:
86                 case SRGTXRXPP:
87                     boolean used = true;
88                     LOG.info("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
89                     try {
90                         List<UsedWavelength> usedWavelengths = tp1.getPpAttributes().getUsedWavelength();
91                         if (usedWavelengths.isEmpty()) {
92                             used = false;
93                         }
94                     } catch (NullPointerException e) {
95                         LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
96                         used = false;
97                     }
98                     if (!used) {
99                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
100                         this.availableSrgPp.put(tp.getTpId().getValue(), tp1.getTpType());
101                     } else {
102                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!");
103                     }
104                     break;
105                 default:
106                     break;
107             }
108         }
109         if (this.availableSrgPp.isEmpty() && this.availableSrgCp.isEmpty()) {
110             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this.toString());
111             this.valid = false;
112             return;
113         }
114         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}", this.availableSrgPp
115                 .size(), this.availableSrgCp.size(), this.toString());
116         return;
117     }
118
119     public void initWLlist() {
120         this.availableWLindex.clear();
121         if (!isValid()) {
122             return;
123         }
124         Node1 node1 = this.node.augmentation(Node1.class);
125         switch (this.nodeType) {
126             case SRG :
127                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
128                     .AvailableWavelengths> srgAvailableWL =
129                         node1.getSrgAttributes().getAvailableWavelengths();
130                 if (srgAvailableWL == null) {
131                     this.valid = false;
132                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this.toString());
133                     return;
134                 }
135                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
136                         .AvailableWavelengths awl : srgAvailableWL) {
137                     this.availableWLindex.add(awl.getIndex());
138                     LOG.debug("initWLlist: SRG next = {} in {}", awl.getIndex(), this.toString());
139                 }
140                 break;
141             case DEGREE :
142                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
143                     .AvailableWavelengths> degAvailableWL = node1.getDegreeAttributes().getAvailableWavelengths();
144                 if (degAvailableWL == null) {
145                     this.valid = false;
146                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this.toString());
147                     return;
148                 }
149                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
150                             .AvailableWavelengths awl : degAvailableWL) {
151                     this.availableWLindex.add(awl.getIndex());
152                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this.toString());
153                 }
154                 break;
155             case XPONDER :
156                 // HARD CODED 96
157                 for (long i = 1; i <= 96; i++) {
158                     this.availableWLindex.add(i);
159                 }
160                 break;
161             default:
162                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this.toString());
163                 break;
164         }
165         if (this.availableWLindex.size() == 0) {
166             LOG.debug("initWLlist: There are no available wavelengths in node {}", this.toString());
167             this.valid = false;
168         }
169         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this.toString());
170         return;
171     }
172
173     public void initXndrTps() {
174         LOG.info("initXndrTps for node : {}", this.nodeId);
175         if (!isValid()) {
176             return;
177         }
178         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
179                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
180                         .rev180226.Node1.class);
181         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
182             .node.TerminationPoint> allTps = nodeTp.getTerminationPoint();
183         if (allTps == null) {
184             this.valid = false;
185             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this.toString());
186             return;
187         }
188         this.valid = false;
189         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
190             .node.TerminationPoint tp : allTps) {
191             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
192             if (tp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
193                 if (tp1.getXpdrNetworkAttributes().getWavelength() != null) {
194                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
195                     LOG.debug("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
196                 } else {
197                     this.valid = true;
198                 }
199                 // find Client of this network TP
200                 String client = tp1.getXpdrNetworkAttributes().getTailEquipmentId();
201                 if ((client.equals("")) || (client == null)) {
202                     LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}", this.toString(), tp
203                             .getTpId().getValue());
204                     this.valid = false;
205                 }
206                 this.clientPerNwTp.put(tp.getTpId().getValue(), client);
207             }
208         }
209         if (!isValid()) {
210             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this.toString());
211             return;
212         }
213     }
214
215     public String getRdmSrgClient(String tp, Boolean aend) {
216         LOG.info("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
217         OpenroadmTpType srgType = null;
218         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
219         if (cpType == null) {
220             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list");
221             return null;
222         }
223         switch (cpType) {
224             case SRGTXRXCP:
225                 LOG.info("getRdmSrgClient: Getting BI Directional PP port ...");
226                 srgType = OpenroadmTpType.SRGTXRXPP;
227                 break;
228             case SRGTXCP:
229                 LOG.info("getRdmSrgClient: Getting UNI Rx PP port ...");
230                 srgType = OpenroadmTpType.SRGRXPP;
231                 break;
232             case SRGRXCP:
233                 LOG.info("getRdmSrgClient: Getting UNI Tx PP port ...");
234                 srgType = OpenroadmTpType.SRGTXPP;
235                 break;
236             default:
237                 break;
238         }
239         LOG.info("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
240         if (!this.availableSrgPp.isEmpty()) {
241             Optional<String> client = null;
242             final OpenroadmTpType openType = srgType;
243             client = this.availableSrgPp.entrySet().stream().filter(pp -> pp.getValue().getName() == openType.getName())
244                     .map(Map.Entry::getKey)
245                     .sorted(new SortPortsByName())
246                     .findFirst();
247             if (!client.isPresent()) {
248                 LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this.toString(), tp);
249                 return null;
250             }
251             LOG.info("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
252             return client.get();
253         } else {
254             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this.toString());
255             return null;
256         }
257     }
258
259     public boolean checkTP(String tp) {
260         return !(this.usedXpndrNWTps.contains(tp));
261     }
262
263     public boolean checkWL(long index) {
264         return (this.availableWLindex.contains(index));
265     }
266
267     public boolean isValid() {
268         return this.valid;
269     }
270
271     public List<Long> getAvailableWLs() {
272         return this.availableWLindex;
273     }
274
275     public void addOutgoingLink(PceLink outLink) {
276         this.outgoingLinks.add(outLink);
277     }
278
279     public List<PceLink> getOutgoingLinks() {
280         return this.outgoingLinks;
281     }
282
283     public String getXpdrClient(String tp) {
284         return this.clientPerNwTp.get(tp);
285     }
286
287     @Override
288     public String toString() {
289         return "PceNode type=" + this.nodeType + " ID=" + this.nodeId.getValue();
290     }
291 }