Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositePathObject.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;path&gt;</br> Same for every
21  *      usage of path object.
22  */
23 public class CompositePathObject {
24
25         private final PCEPExplicitRouteObject explicitRoute;
26
27         private final PCEPLspaObject lspa;
28
29         private final PCEPRequestedPathBandwidthObject bandwidth;
30
31         private List<PCEPMetricObject> metrics;
32
33         private final PCEPIncludeRouteObject includeRoute;
34
35         /**
36          * Constructs basic composite object only with mandatory objects.
37          *
38          * @param explicitRoute
39          *            PCEPExplicitRouteObject. Can't be null or empty.
40          */
41         public CompositePathObject(PCEPExplicitRouteObject explicitRoute) {
42                 this(explicitRoute, null, null, null, null);
43         }
44
45         /**
46          * Constructs composite object with optional objects.
47          *
48          * @param explicitRoute
49          *            PCEPExplicitRouteObject. Can't be null or empty.
50          * @param lspa
51          *            PCEPLspaObject
52          * @param bandwidth
53          *            PCEPRequestedPathBandwidthObject
54          * @param metrics
55          *            List<PCEPMetricObject>
56          * @param includeRoute
57          *            PCEPIncludeRouteObject
58          */
59         public CompositePathObject(PCEPExplicitRouteObject explicitRoute, PCEPLspaObject lspa, PCEPRequestedPathBandwidthObject bandwidth,
60                         List<PCEPMetricObject> metrics, PCEPIncludeRouteObject includeRoute) {
61                 if (explicitRoute == null)
62                         throw new IllegalArgumentException("Explicit Route Object is mandatory.");
63                 this.explicitRoute = explicitRoute;
64                 this.lspa = lspa;
65                 this.bandwidth = bandwidth;
66                 if (metrics != null)
67                         this.metrics = metrics;
68                 else
69                         this.metrics = Collections.emptyList();
70                 this.includeRoute = includeRoute;
71         }
72
73         /**
74          * Gets list of all objects, which are in appropriate order.
75          *
76          * @return List<PCEPObject>. Can't be null or empty.
77          */
78         public List<PCEPObject> getCompositeAsList() {
79                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
80                 list.add(this.explicitRoute);
81                 if (this.lspa != null)
82                         list.add(this.lspa);
83                 if (this.bandwidth != null)
84                         list.add(this.bandwidth);
85                 if (this.metrics != null && !this.metrics.isEmpty())
86                         list.addAll(this.metrics);
87                 if (this.includeRoute != null)
88                         list.add(this.includeRoute);
89                 return list;
90         }
91
92         /**
93          * Creates this object from a list of PCEPObjects.
94          * @param objects List<PCEPObject> list of PCEPObjects from whose this
95          * object should be created.
96          * @return CompositePathObject
97          */
98         public static CompositePathObject getCompositeFromList(List<PCEPObject> objects) {
99                 if (objects == null || objects.isEmpty()) {
100                         throw new IllegalArgumentException("List cannot be null or empty.");
101                 }
102
103                 PCEPExplicitRouteObject explicitRoute = null;
104                 if (objects.get(0) instanceof PCEPExplicitRouteObject) {
105                         explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
106                         objects.remove(explicitRoute);
107                 } else
108                         return null;
109
110                 PCEPLspaObject lspa = null;
111                 PCEPRequestedPathBandwidthObject bandwidth = null;
112                 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
113                 PCEPIncludeRouteObject iro = null;
114
115                 int state = 1;
116                 while (!objects.isEmpty()) {
117                         final PCEPObject obj = objects.get(0);
118
119                         switch (state) {
120                                 case 1:
121                                         state = 2;
122                                         if (obj instanceof PCEPLspaObject) {
123                                                 lspa = (PCEPLspaObject) obj;
124                                                 break;
125                                         }
126                                 case 2:
127                                         state = 3;
128                                         if (obj instanceof PCEPRequestedPathBandwidthObject) {
129                                                 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
130                                                 break;
131                                         }
132                                 case 3:
133                                         if (obj instanceof PCEPMetricObject) {
134                                                 metrics.add((PCEPMetricObject) obj);
135                                                 state = 3;
136                                                 break;
137                                         } else
138                                                 state = 4;
139                                 case 4:
140                                         if (obj instanceof PCEPIncludeRouteObject) {
141                                                 iro = (PCEPIncludeRouteObject) obj;
142                                                 break;
143                                         }
144                                         state = 5;
145                         }
146
147                         if (state == 5) {
148                                 break;
149                         }
150
151                         objects.remove(obj);
152                 }
153
154                 return new CompositePathObject(explicitRoute, lspa, bandwidth, metrics, iro);
155         }
156
157         /**
158          * Gets {@link PCEPExplicitRouteObject}.
159          *
160          * @return PCEPExplicitRouteObject. Can't be null.
161          */
162         public PCEPExplicitRouteObject getExcludedRoute() {
163                 return this.explicitRoute;
164         }
165
166         /**
167          * Gets {@link PCEPLspaObject}.
168          *
169          * @return PCEPLspaObject. May be null.
170          */
171         public PCEPLspaObject getLspa() {
172                 return this.lspa;
173         }
174
175         /**
176          * Gets {@link PCEPBandwidthObject}.
177          *
178          * @return PCEPBandwidthObject. May be null.
179          */
180         public PCEPBandwidthObject getBandwidth() {
181                 return this.bandwidth;
182         }
183
184         /**
185          * Gets list of {@link PCEPMetricObject}.
186          *
187          * @return List<PCEPMetricObject>. Can't be null, but may be empty.
188          */
189         public List<PCEPMetricObject> getMetrics() {
190                 return this.metrics;
191         }
192
193         /**
194          * Gets {@link PCEPIncludeRouteObject}.
195          *
196          * @return PCEPIncludeRouteObject. May be null.
197          */
198         public PCEPIncludeRouteObject getIncludeRoute() {
199                 return this.includeRoute;
200         }
201
202         @Override
203         public int hashCode() {
204                 final int prime = 31;
205                 int result = 1;
206                 result = prime * result + ((this.bandwidth == null) ? 0 : this.bandwidth.hashCode());
207                 result = prime * result + ((this.explicitRoute == null) ? 0 : this.explicitRoute.hashCode());
208                 result = prime * result + ((this.includeRoute == null) ? 0 : this.includeRoute.hashCode());
209                 result = prime * result + ((this.lspa == null) ? 0 : this.lspa.hashCode());
210                 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
211                 return result;
212         }
213
214         @Override
215         public boolean equals(Object obj) {
216                 if (this == obj)
217                         return true;
218                 if (obj == null)
219                         return false;
220                 if (this.getClass() != obj.getClass())
221                         return false;
222                 final CompositePathObject other = (CompositePathObject) obj;
223                 if (this.bandwidth == null) {
224                         if (other.bandwidth != null)
225                                 return false;
226                 } else if (!this.bandwidth.equals(other.bandwidth))
227                         return false;
228                 if (this.explicitRoute == null) {
229                         if (other.explicitRoute != null)
230                                 return false;
231                 } else if (!this.explicitRoute.equals(other.explicitRoute))
232                         return false;
233                 if (this.includeRoute == null) {
234                         if (other.includeRoute != null)
235                                 return false;
236                 } else if (!this.includeRoute.equals(other.includeRoute))
237                         return false;
238                 if (this.lspa == null) {
239                         if (other.lspa != null)
240                                 return false;
241                 } else if (!this.lspa.equals(other.lspa))
242                         return false;
243                 if (this.metrics == null) {
244                         if (other.metrics != null)
245                                 return false;
246                 } else if (!this.metrics.equals(other.metrics))
247                         return false;
248                 return true;
249         }
250
251         @Override
252         public String toString() {
253                 final StringBuilder builder = new StringBuilder();
254                 builder.append("CompositePathObject [explicitRoute=");
255                 builder.append(this.explicitRoute);
256                 builder.append(", lspa=");
257                 builder.append(this.lspa);
258                 builder.append(", bandwidth=");
259                 builder.append(this.bandwidth);
260                 builder.append(", metrics=");
261                 builder.append(this.metrics);
262                 builder.append(", includeRoute=");
263                 builder.append(this.includeRoute);
264                 builder.append("]");
265                 return builder.toString();
266         }
267 }