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