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