Merge "Randomize port to allow concurrent execution"
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeResponseObject.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.5">Path
20  *      Computation Reply (PCRep) Message</a> - &lt;response&gt;</br>
21  */
22 public class CompositeResponseObject {
23
24         private final PCEPRequestParameterObject requestParameter;
25
26         private final PCEPNoPathObject noPath;
27
28         private final PCEPLspObject lsp;
29
30         private final PCEPLspaObject lspa;
31
32         private final PCEPRequestedPathBandwidthObject bandwidth;
33
34         private List<PCEPMetricObject> metrics;
35
36         private final PCEPIncludeRouteObject includeRoute;
37
38         private List<CompositePathObject> paths;
39
40         /**
41          * Constructs basic composite object only with mandatory objects.
42          *
43          * @param requestParameter
44          *            PCEPRequestParameterObject. Can't be null.
45          */
46         public CompositeResponseObject(PCEPRequestParameterObject requestParameter) {
47                 this(requestParameter, null, null, null, null, null, null, null);
48         }
49
50         /**
51          * Constructs composite object with optional objects.
52          *
53          * @param requestParameter
54          *            PCEPRequestParameterObject. Can't be null.
55          * @param noPath
56          *            PCEPNoPathObject
57          * @param lsp
58          *            PCEPLspObject
59          * @param lspa
60          *            PCEPLspaObject
61          * @param bandwidth
62          *            PCEPRequestedPathBandwidthObject
63          * @param metrics
64          *            List<PCEPMetricObject>
65          * @param includeRoute
66          *            PCEPIncludeRouteObject
67          * @param paths
68          *            List<CompositePathObject>
69          */
70         public CompositeResponseObject(PCEPRequestParameterObject requestParameter, PCEPNoPathObject noPath, PCEPLspObject lsp, PCEPLspaObject lspa,
71                         PCEPRequestedPathBandwidthObject bandwidth, List<PCEPMetricObject> metrics, PCEPIncludeRouteObject includeRoute, List<CompositePathObject> paths) {
72                 if (requestParameter == null)
73                         throw new IllegalArgumentException("Request Parameter Object is mandatory.");
74                 this.requestParameter = requestParameter;
75                 this.noPath = noPath;
76                 this.lsp = lsp;
77                 this.lspa = lspa;
78                 this.bandwidth = bandwidth;
79                 if (metrics != null)
80                         this.metrics = metrics;
81                 else
82                         this.metrics = Collections.emptyList();
83                 this.includeRoute = includeRoute;
84                 if (paths != null)
85                         this.paths = paths;
86                 else
87                         this.paths = Collections.emptyList();
88         }
89
90         /**
91          * Gets list of all objects, which are in appropriate order.
92          *
93          * @return List<PCEPObject>
94          */
95         public List<PCEPObject> getCompositeAsList() {
96                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
97                 list.add(this.requestParameter);
98                 if (this.noPath != null)
99                         list.add(this.noPath);
100                 if (this.lsp != null)
101                         list.add(this.lsp);
102                 if (this.lspa != null)
103                         list.add(this.lspa);
104                 if (this.bandwidth != null)
105                         list.add(this.bandwidth);
106                 if (this.metrics != null && !this.metrics.isEmpty())
107                         list.addAll(this.metrics);
108                 if (this.includeRoute != null)
109                         list.add(this.includeRoute);
110                 if (this.paths != null && !this.paths.isEmpty())
111                         for (final CompositePathObject cpo : this.paths)
112                                 list.addAll(cpo.getCompositeAsList());
113                 return list;
114         }
115
116         /**
117          * Creates this object from a list of PCEPObjects.
118          * @param objects List<PCEPObject> list of PCEPObjects from whose this
119          * object should be created.
120          * @return CompositeResponseObject
121          */
122         public static CompositeResponseObject getCompositeFromList(List<PCEPObject> objects) {
123                 if (objects == null || objects.isEmpty()) {
124                         throw new IllegalArgumentException("List cannot be null or empty.");
125                 }
126                 PCEPRequestParameterObject requestParameter = null;
127                 if (objects.get(0) instanceof PCEPRequestParameterObject) {
128                         requestParameter = (PCEPRequestParameterObject) objects.get(0);
129                         objects.remove(requestParameter);
130                 } else
131                         return null;
132                 PCEPNoPathObject noPath = null;
133                 PCEPLspObject lsp = null;
134                 PCEPLspaObject lspa = null;
135                 PCEPRequestedPathBandwidthObject bandwidth = null;
136                 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
137                 PCEPIncludeRouteObject iro = null;
138                 final List<CompositePathObject> paths = new ArrayList<CompositePathObject>();
139
140                 int state = 1;
141                 while (!objects.isEmpty()) {
142                         final PCEPObject obj = objects.get(0);
143                         switch (state) {
144                                 case 1:
145                                         state = 2;
146                                         if (obj instanceof PCEPNoPathObject) {
147                                                 noPath = (PCEPNoPathObject) obj;
148                                                 break;
149                                         }
150                                 case 2:
151                                         state = 3;
152                                         if (obj instanceof PCEPLspObject) {
153                                                 lsp = (PCEPLspObject) obj;
154                                                 break;
155                                         }
156                                 case 3:
157                                         state = 4;
158                                         if (obj instanceof PCEPLspaObject) {
159                                                 lspa = (PCEPLspaObject) obj;
160                                                 break;
161                                         }
162                                 case 4:
163                                         state = 5;
164                                         if (obj instanceof PCEPRequestedPathBandwidthObject) {
165                                                 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
166                                                 break;
167                                         }
168                                 case 5:
169                                         if (obj instanceof PCEPMetricObject) {
170                                                 metrics.add((PCEPMetricObject) obj);
171                                                 state = 5;
172                                                 break;
173                                         } else
174                                                 state = 6;
175                                 case 6:
176                                         state = 8;
177                                         if (obj instanceof PCEPIncludeRouteObject) {
178                                                 iro = (PCEPIncludeRouteObject) obj;
179                                                 break;
180                                         }
181                                         state = 7;
182                         }
183
184                         if (state == 7) {
185                                 break;
186                         }
187                         objects.remove(obj);
188                         if (state == 8) {
189                                 break;
190                         }
191                 }
192                 if (!objects.isEmpty()) {
193                         CompositePathObject path = CompositePathObject.getCompositeFromList(objects);
194                         while (path != null) {
195                                 paths.add(path);
196                                 if (objects.isEmpty())
197                                         break;
198                                 path = CompositePathObject.getCompositeFromList(objects);
199                         }
200                 }
201                 return new CompositeResponseObject(requestParameter, noPath, lsp, lspa, bandwidth, metrics, iro, paths);
202         }
203
204         /**
205          * Gets {@link PCEPRequestParameterObject}.
206          *
207          * @return PCEPRequestParameterObject. Can't be null.
208          */
209         public PCEPRequestParameterObject getRequestParameter() {
210                 return this.requestParameter;
211         }
212
213         /**
214          * Gets {@link PCEPNoPathObject}.
215          *
216          * @return PCEPNoPathObject. May be null.
217          */
218         public PCEPNoPathObject getNoPath() {
219                 return this.noPath;
220         }
221
222         /**
223          * Gets {@link PCEPLspObject}
224          *
225          * @return PCEPLspObject. May be null.
226          */
227         public PCEPLspObject getLsp() {
228                 return this.lsp;
229         }
230
231         /**
232          * Gets {@link PCEPLspaObject}.
233          *
234          * @return PCEPLspaObject. May be null.
235          */
236         public PCEPLspaObject getLspa() {
237                 return this.lspa;
238         }
239
240         /**
241          * Gets {@link PCEPBandwidthObject}.
242          *
243          * @return PCEPBandwidthObject. May be null.
244          */
245         public PCEPBandwidthObject getBandwidth() {
246                 return this.bandwidth;
247         }
248
249         /**
250          * Gets list of {@link PCEPMetricObject}.
251          *
252          * @return List<PCEPMetricObject>. Can't be null, but may be empty.
253          */
254         public List<PCEPMetricObject> getMetrics() {
255                 return this.metrics;
256         }
257
258         /**
259          * Gets list of {@link CompositePathObject}.
260          *
261          * @return PCEPIncludeRouteObject. Can't be null, but may be empty.
262          */
263         public PCEPIncludeRouteObject getIncludeRoute() {
264                 return this.includeRoute;
265         }
266
267         /**
268          * Gets list of {@link CompositePathObject}.
269          *
270          * @return List<CompositePathObject>. Can't be null, but may be empty.
271          */
272         public List<CompositePathObject> getPaths() {
273                 return this.paths;
274         }
275
276         @Override
277         public int hashCode() {
278                 final int prime = 31;
279                 int result = 1;
280                 result = prime * result + ((this.bandwidth == null) ? 0 : this.bandwidth.hashCode());
281                 result = prime * result + ((this.includeRoute == null) ? 0 : this.includeRoute.hashCode());
282                 result = prime * result + ((this.lsp == null) ? 0 : this.lsp.hashCode());
283                 result = prime * result + ((this.lspa == null) ? 0 : this.lspa.hashCode());
284                 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
285                 result = prime * result + ((this.noPath == null) ? 0 : this.noPath.hashCode());
286                 result = prime * result + ((this.paths == null) ? 0 : this.paths.hashCode());
287                 result = prime * result + ((this.requestParameter == null) ? 0 : this.requestParameter.hashCode());
288                 return result;
289         }
290
291         @Override
292         public boolean equals(Object obj) {
293                 if (this == obj)
294                         return true;
295                 if (obj == null)
296                         return false;
297                 if (this.getClass() != obj.getClass())
298                         return false;
299                 final CompositeResponseObject other = (CompositeResponseObject) obj;
300                 if (this.bandwidth == null) {
301                         if (other.bandwidth != null)
302                                 return false;
303                 } else if (!this.bandwidth.equals(other.bandwidth))
304                         return false;
305                 if (this.includeRoute == null) {
306                         if (other.includeRoute != null)
307                                 return false;
308                 } else if (!this.includeRoute.equals(other.includeRoute))
309                         return false;
310                 if (this.lsp == null) {
311                         if (other.lsp != null)
312                                 return false;
313                 } else if (!this.lsp.equals(other.lsp))
314                         return false;
315                 if (this.lspa == null) {
316                         if (other.lspa != null)
317                                 return false;
318                 } else if (!this.lspa.equals(other.lspa))
319                         return false;
320                 if (this.metrics == null) {
321                         if (other.metrics != null)
322                                 return false;
323                 } else if (!this.metrics.equals(other.metrics))
324                         return false;
325                 if (this.noPath == null) {
326                         if (other.noPath != null)
327                                 return false;
328                 } else if (!this.noPath.equals(other.noPath))
329                         return false;
330                 if (this.paths == null) {
331                         if (other.paths != null)
332                                 return false;
333                 } else if (!this.paths.equals(other.paths))
334                         return false;
335                 if (this.requestParameter == null) {
336                         if (other.requestParameter != null)
337                                 return false;
338                 } else if (!this.requestParameter.equals(other.requestParameter))
339                         return false;
340                 return true;
341         }
342
343         @Override
344         public String toString() {
345                 final StringBuilder builder = new StringBuilder();
346                 builder.append("CompositeResponseObject [requestParameter=");
347                 builder.append(this.requestParameter);
348                 builder.append(", noPath=");
349                 builder.append(this.noPath);
350                 builder.append(", lsp=");
351                 builder.append(this.lsp);
352                 builder.append(", lspa=");
353                 builder.append(this.lspa);
354                 builder.append(", bandwidth=");
355                 builder.append(this.bandwidth);
356                 builder.append(", metrics=");
357                 builder.append(this.metrics);
358                 builder.append(", includeRoute=");
359                 builder.append(this.includeRoute);
360                 builder.append(", paths=");
361                 builder.append(this.paths);
362                 builder.append("]");
363                 return builder.toString();
364         }
365 }