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