Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeRequestObject.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, 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.protocol.pcep.object;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPObject;
15
16 /**
17  * Structure that combines set of related objects.
18  * 
19  * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.4">Path
20  *      Computation Request (PCReq) Message</a> - &lt;request&gt;</br>
21  * 
22  * @see <a href="tools.ietf.org/html/rfc5455#section-3.2">Path Computation
23  *      Request Message with CLASSTYPE Object</a>
24  */
25 public class CompositeRequestObject {
26
27         private final PCEPRequestParameterObject requestParameter;
28
29         private final PCEPEndPointsObject<?> endPoints;
30
31         private final PCEPClassTypeObject classType;
32
33         private final PCEPLspObject lsp;
34
35         private final PCEPLspaObject lspa;
36
37         private final PCEPRequestedPathBandwidthObject bandwidth;
38
39         private final List<PCEPMetricObject> metrics;
40
41         private final PCEPReportedRouteObject reportedRoute;
42
43         private final PCEPExistingPathBandwidthObject rroBandwidth;
44
45         private final PCEPIncludeRouteObject includeRoute;
46
47         private final PCEPLoadBalancingObject loadBalancing;
48
49         /**
50          * Constructs basic composite object only with mandatory objects.
51          * 
52          * @param requestParameter
53          *            PCEPRequestParameterObject. Can't be null.
54          * @param endPoints
55          *            PCEPEndPointsObject<?>. Can't be null.
56          */
57         public CompositeRequestObject(PCEPRequestParameterObject requestParameter, PCEPEndPointsObject<?> endPoints) {
58                 this(requestParameter, endPoints, null, null, null, null, null, null, null, null, null);
59         }
60
61         /**
62          * Constructs composite object with optional objects.
63          * 
64          * @param requestParameter
65          *            PCEPRequestParameterObject. Can't be null.
66          * @param endPoints
67          *            PCEPEndPointsObject<?>. Can't be null.
68          * @param classType
69          *            PCEPClassTypeObject
70          * @param lsp
71          *            PCEPLspObject
72          * @param lspa
73          *            PCEPLspaObject
74          * @param bandwidth
75          *            PCEPRequestedPathBandwidthObject
76          * @param metrics
77          *            List<PCEPMetricObject>
78          * @param reportedRoute
79          *            PCEPReportedRouteObject
80          * @param rroBandwidth
81          *            PCEPExistingPathBandwidthObject
82          * @param includeRoute
83          *            PCEPIncludeRouteObject
84          * @param loadBalancing
85          *            PCEPLoadBalancingObject
86          */
87         public CompositeRequestObject(PCEPRequestParameterObject requestParameter, PCEPEndPointsObject<?> endPoints, PCEPClassTypeObject classType,
88                         PCEPLspObject lsp, PCEPLspaObject lspa, PCEPRequestedPathBandwidthObject bandwidth, List<PCEPMetricObject> metrics,
89                         PCEPReportedRouteObject reportedRoute, PCEPExistingPathBandwidthObject rroBandwidth, PCEPIncludeRouteObject includeRoute,
90                         PCEPLoadBalancingObject loadBalancing) {
91                 if (requestParameter == null)
92                         throw new IllegalArgumentException("Request Parameter Object is mandatory.");
93                 if (endPoints == null)
94                         throw new IllegalArgumentException("End-Points Object is mandatory.");
95                 this.requestParameter = requestParameter;
96                 this.endPoints = endPoints;
97                 this.classType = classType;
98                 this.lsp = lsp;
99                 this.lspa = lspa;
100                 this.bandwidth = bandwidth;
101                 if (metrics != null)
102                         this.metrics = metrics;
103                 else
104                         this.metrics = Collections.emptyList();
105                 this.reportedRoute = reportedRoute;
106                 this.rroBandwidth = rroBandwidth;
107                 this.includeRoute = includeRoute;
108                 this.loadBalancing = loadBalancing;
109         }
110
111         /**
112          * Gets list of all objects, which are in appropriate order.
113          * 
114          * @return List<PCEPObject>. Can't be null or empty.
115          */
116         public List<PCEPObject> getCompositeAsList() {
117                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
118                 list.add(this.requestParameter);
119                 list.add(this.endPoints);
120                 if (this.classType != null)
121                         list.add(this.classType);
122                 if (this.lsp != null)
123                         list.add(this.lsp);
124                 if (this.lspa != null)
125                         list.add(this.lspa);
126                 if (this.bandwidth != null)
127                         list.add(this.bandwidth);
128                 if (this.metrics != null && !this.metrics.isEmpty())
129                         list.addAll(this.metrics);
130                 if (this.reportedRoute != null) {
131                         list.add(this.reportedRoute);
132                         if (this.rroBandwidth != null)
133                                 list.add(this.rroBandwidth);
134                 }
135                 if (this.includeRoute != null)
136                         list.add(this.includeRoute);
137                 if (this.loadBalancing != null)
138                         list.add(this.loadBalancing);
139                 return list;
140         }
141
142         /**
143          * Creates this object from a list of PCEPObjects.
144          * 
145          * @param objects
146          *            List<PCEPObject> list of PCEPObjects from whose this object
147          *            should be created.
148          * @return CompositeRequestObject
149          */
150         public static CompositeRequestObject getCompositeFromList(List<PCEPObject> objects) {
151                 if (objects == null || objects.isEmpty()) {
152                         throw new IllegalArgumentException("List cannot be null or empty.");
153                 }
154                 PCEPRequestParameterObject requestParameter = null;
155                 if (objects.get(0) instanceof PCEPRequestParameterObject) {
156                         requestParameter = (PCEPRequestParameterObject) objects.get(0);
157                         objects.remove(requestParameter);
158                 } else
159                         return null;
160
161                 PCEPEndPointsObject<?> endPoints = null;
162                 if (objects.get(0) instanceof PCEPEndPointsObject<?>) {
163                         endPoints = (PCEPEndPointsObject<?>) objects.get(0);
164                         objects.remove(endPoints);
165                 } else
166                         throw new IllegalArgumentException("End Points object must be second.");
167
168                 PCEPClassTypeObject classType = null;
169                 PCEPLspObject lsp = null;
170                 PCEPLspaObject lspa = null;
171                 PCEPRequestedPathBandwidthObject bandwidth = null;
172                 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
173                 PCEPReportedRouteObject rro = null;
174                 PCEPExistingPathBandwidthObject rroBandwidth = null;
175                 PCEPIncludeRouteObject iro = null;
176                 PCEPLoadBalancingObject loadBalancing = null;
177
178                 int state = 1;
179                 while (!objects.isEmpty()) {
180                         final PCEPObject obj = objects.get(0);
181                         switch (state) {
182                                 case 1:
183                                         state = 2;
184                                         if (obj instanceof PCEPClassTypeObject) {
185                                                 classType = (PCEPClassTypeObject) obj;
186                                                 break;
187                                         }
188                                 case 2:
189                                         state = 3;
190                                         if (obj instanceof PCEPLspObject) {
191                                                 lsp = (PCEPLspObject) obj;
192                                                 break;
193                                         }
194                                 case 3:
195                                         state = 4;
196                                         if (obj instanceof PCEPLspaObject) {
197                                                 lspa = (PCEPLspaObject) obj;
198                                                 break;
199                                         }
200                                 case 4:
201                                         state = 5;
202                                         if (obj instanceof PCEPRequestedPathBandwidthObject) {
203                                                 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
204                                                 break;
205                                         }
206                                 case 5:
207                                         state = 6;
208                                         if (obj instanceof PCEPMetricObject) {
209                                                 metrics.add((PCEPMetricObject) obj);
210                                                 state = 5;
211
212                                                 break;
213                                         }
214                                 case 6:
215                                         state = 8;
216                                         if (obj instanceof PCEPReportedRouteObject) {
217                                                 rro = (PCEPReportedRouteObject) obj;
218                                                 state = 7;
219                                                 break;
220                                         }
221                                 case 7:
222                                         state = 8;
223                                         if (obj instanceof PCEPExistingPathBandwidthObject) {
224                                                 rroBandwidth = (PCEPExistingPathBandwidthObject) obj;
225                                                 break;
226                                         }
227                                 case 8:
228                                         state = 9;
229                                         if (obj instanceof PCEPIncludeRouteObject) {
230                                                 iro = (PCEPIncludeRouteObject) obj;
231                                                 break;
232                                         }
233                                 case 9:
234                                         if (obj instanceof PCEPLoadBalancingObject) {
235                                                 loadBalancing = (PCEPLoadBalancingObject) obj;
236                                                 break;
237                                         }
238                                         state = 10;
239                         }
240
241                         if (state == 10) {
242                                 break;
243                         }
244
245                         objects.remove(obj);
246                 }
247
248                 return new CompositeRequestObject(requestParameter, endPoints, classType, lsp, lspa, bandwidth, metrics, rro, rroBandwidth, iro, loadBalancing);
249         }
250
251         /**
252          * Gets {@link PCEPRequestParameterObject}.
253          * 
254          * @return PCEPRequestParameterObject. Can't be null.
255          */
256         public PCEPRequestParameterObject getRequestParameter() {
257                 return this.requestParameter;
258         }
259
260         /**
261          * Gets {@link PCEPEndPointsObject}.
262          * 
263          * @return PCEPEndPointsObject<?>. Can't be null.
264          */
265         public PCEPEndPointsObject<?> getEndPoints() {
266                 return this.endPoints;
267         }
268
269         /**
270          * Gets {@link PCEPClassTypeObject}.
271          * 
272          * @return PCEPClassTypeObject. May be null.
273          */
274         public PCEPClassTypeObject getClassType() {
275                 return this.classType;
276         }
277
278         /**
279          * Gets {@link PCEPLspObject}.
280          * 
281          * @return PCEPLspObject. May be null.
282          */
283         public PCEPLspObject getLsp() {
284                 return this.lsp;
285         }
286
287         /**
288          * Gets {@link PCEPLspaObject}.
289          * 
290          * @return PCEPLspaObject. May be null.
291          */
292         public PCEPLspaObject getLspa() {
293                 return this.lspa;
294         }
295
296         /**
297          * Gets {@link PCEPBandwidthObject}.
298          * 
299          * @return PCEPBandwidthObject. May be null.
300          */
301         public PCEPBandwidthObject getBandwidth() {
302                 return this.bandwidth;
303         }
304
305         /**
306          * Gets list of {@link PCEPMetricObject}.
307          * 
308          * @return List<PCEPMetricObject>
309          */
310         public List<PCEPMetricObject> getMetrics() {
311                 return this.metrics;
312         }
313
314         /**
315          * Gets {@link PCEPReportedRouteObject}.
316          * 
317          * @return PCEPReportedRouteObject. May be null.
318          */
319         public PCEPReportedRouteObject getReportedRoute() {
320                 return this.reportedRoute;
321         }
322
323         /**
324          * Gets {@link PCEPBandwidthObject}.
325          * 
326          * @return PCEPBandwidthObject. May be null.
327          */
328         public PCEPBandwidthObject getRroBandwidth() {
329                 return this.rroBandwidth;
330         }
331
332         /**
333          * Gets {@link PCEPIncludeRouteObject}.
334          * 
335          * @return PCEPIncludeRouteObject. May be null.
336          */
337         public PCEPIncludeRouteObject getIncludeRoute() {
338                 return this.includeRoute;
339         }
340
341         /**
342          * Gets {@link PCEPLoadBalancingObject}.
343          * 
344          * @return PCEPLoadBalancingObject. May be null.
345          */
346         public PCEPLoadBalancingObject getLoadBalancing() {
347                 return this.loadBalancing;
348         }
349
350         @Override
351         public int hashCode() {
352                 final int prime = 31;
353                 int result = 1;
354                 result = prime * result + ((this.bandwidth == null) ? 0 : this.bandwidth.hashCode());
355                 result = prime * result + ((this.classType == null) ? 0 : this.classType.hashCode());
356                 result = prime * result + ((this.endPoints == null) ? 0 : this.endPoints.hashCode());
357                 result = prime * result + ((this.includeRoute == null) ? 0 : this.includeRoute.hashCode());
358                 result = prime * result + ((this.loadBalancing == null) ? 0 : this.loadBalancing.hashCode());
359                 result = prime * result + ((this.lsp == null) ? 0 : this.lsp.hashCode());
360                 result = prime * result + ((this.lspa == null) ? 0 : this.lspa.hashCode());
361                 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
362                 result = prime * result + ((this.reportedRoute == null) ? 0 : this.reportedRoute.hashCode());
363                 result = prime * result + ((this.requestParameter == null) ? 0 : this.requestParameter.hashCode());
364                 result = prime * result + ((this.rroBandwidth == null) ? 0 : this.rroBandwidth.hashCode());
365                 return result;
366         }
367
368         @Override
369         public boolean equals(Object obj) {
370                 if (this == obj)
371                         return true;
372                 if (obj == null)
373                         return false;
374                 if (!(obj instanceof CompositeRequestObject))
375                         return false;
376                 final CompositeRequestObject other = (CompositeRequestObject) obj;
377                 if (this.bandwidth == null) {
378                         if (other.bandwidth != null)
379                                 return false;
380                 } else if (!this.bandwidth.equals(other.bandwidth))
381                         return false;
382                 if (this.classType == null) {
383                         if (other.classType != null)
384                                 return false;
385                 } else if (!this.classType.equals(other.classType))
386                         return false;
387                 if (this.endPoints == null) {
388                         if (other.endPoints != null)
389                                 return false;
390                 } else if (!this.endPoints.equals(other.endPoints))
391                         return false;
392                 if (this.includeRoute == null) {
393                         if (other.includeRoute != null)
394                                 return false;
395                 } else if (!this.includeRoute.equals(other.includeRoute))
396                         return false;
397                 if (this.loadBalancing == null) {
398                         if (other.loadBalancing != null)
399                                 return false;
400                 } else if (!this.loadBalancing.equals(other.loadBalancing))
401                         return false;
402                 if (this.lsp == null) {
403                         if (other.lsp != null)
404                                 return false;
405                 } else if (!this.lsp.equals(other.lsp))
406                         return false;
407                 if (this.lspa == null) {
408                         if (other.lspa != null)
409                                 return false;
410                 } else if (!this.lspa.equals(other.lspa))
411                         return false;
412                 if (this.metrics == null) {
413                         if (other.metrics != null)
414                                 return false;
415                 } else if (!this.metrics.equals(other.metrics))
416                         return false;
417                 if (this.reportedRoute == null) {
418                         if (other.reportedRoute != null)
419                                 return false;
420                 } else if (!this.reportedRoute.equals(other.reportedRoute))
421                         return false;
422                 if (this.requestParameter == null) {
423                         if (other.requestParameter != null)
424                                 return false;
425                 } else if (!this.requestParameter.equals(other.requestParameter))
426                         return false;
427                 if (this.rroBandwidth == null) {
428                         if (other.rroBandwidth != null)
429                                 return false;
430                 } else if (!this.rroBandwidth.equals(other.rroBandwidth))
431                         return false;
432                 return true;
433         }
434
435         @Override
436         public String toString() {
437                 final StringBuilder builder = new StringBuilder();
438                 builder.append("CompositeRequestObject [requestParameter=");
439                 builder.append(this.requestParameter);
440                 builder.append(", endPoints=");
441                 builder.append(this.endPoints);
442                 builder.append(", classType=");
443                 builder.append(this.classType);
444                 builder.append(", lsp=");
445                 builder.append(this.lsp);
446                 builder.append(", lspa=");
447                 builder.append(this.lspa);
448                 builder.append(", bandwidth=");
449                 builder.append(this.bandwidth);
450                 builder.append(", metrics=");
451                 builder.append(this.metrics);
452                 builder.append(", reportedRoute=");
453                 builder.append(this.reportedRoute);
454                 builder.append(", rroBandwidth=");
455                 builder.append(this.rroBandwidth);
456                 builder.append(", includeRoute=");
457                 builder.append(this.includeRoute);
458                 builder.append(", loadBalancing=");
459                 builder.append(this.loadBalancing);
460                 builder.append("]");
461                 return builder.toString();
462         }
463 }