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