2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.object;
10 import java.util.ArrayList;
11 import java.util.List;
13 import org.opendaylight.protocol.pcep.PCEPObject;
15 import com.google.common.base.Preconditions;
18 * Structure that combines set of related objects.
20 * @see <a href="http://www.ietf.org/id/draft-crabbe-pce-pce-initiated-lsp-00.txt">PCCreate Message</a>
22 public class CompositeInstantiationObject {
24 private final PCEPEndPointsObject<?> endPoints;
26 private final PCEPLspaObject lspa;
28 private final PCEPExplicitRouteObject ero;
30 private final PCEPRequestedPathBandwidthObject bandwidth;
32 private final List<PCEPMetricObject> metrics;
35 * Constructs basic composite object only with mandatory objects.
37 * @param endPoints PCEPEndPointsObject<?>. Can't be null.
38 * @param lspa PCEPLspaObject. Can't be null.
40 public CompositeInstantiationObject(final PCEPEndPointsObject<?> endPoints, final PCEPLspaObject lspa) {
41 this(endPoints, lspa, null, null, null);
45 * Constructs composite object with optional objects.
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>
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);
58 this.bandwidth = bandwidth;
59 this.metrics = metrics;
63 * Gets list of all objects, which are in appropriate order.
65 * @return List<PCEPObject>. Can't be null or empty.
67 public List<PCEPObject> getCompositeAsList() {
68 final List<PCEPObject> list = new ArrayList<PCEPObject>();
69 list.add(this.endPoints);
73 if (this.bandwidth != null)
74 list.add(this.bandwidth);
75 if (this.metrics != null && !this.metrics.isEmpty())
76 list.addAll(this.metrics);
81 * Creates this object from a list of PCEPObjects.
83 * @param objects List<PCEPObject> list of PCEPObjects from whose this object should be created.
84 * @return CompositeInstantiationObject
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.");
91 PCEPEndPointsObject<?> endPoints = null;
92 if (objects.get(0) instanceof PCEPEndPointsObject<?>) {
93 endPoints = (PCEPEndPointsObject<?>) objects.get(0);
94 objects.remove(endPoints);
96 throw new IllegalArgumentException("End Points object must be first.");
98 PCEPLspaObject lspa = null;
99 if (objects.get(0) instanceof PCEPLspaObject) {
100 lspa = (PCEPLspaObject) objects.get(0);
101 objects.remove(lspa);
103 throw new IllegalArgumentException("LSPA object must be second.");
105 PCEPExplicitRouteObject ero = null;
106 PCEPRequestedPathBandwidthObject bandwidth = null;
107 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
110 while (!objects.isEmpty()) {
111 final PCEPObject obj = objects.get(0);
115 if (obj instanceof PCEPExplicitRouteObject) {
116 ero = (PCEPExplicitRouteObject) obj;
121 if (obj instanceof PCEPRequestedPathBandwidthObject) {
122 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
127 if (obj instanceof PCEPMetricObject) {
128 metrics.add((PCEPMetricObject) obj);
142 return new CompositeInstantiationObject(endPoints, lspa, ero, bandwidth, metrics);
146 * @return the endPoints
148 public final PCEPEndPointsObject<?> getEndPoints() {
149 return this.endPoints;
155 public final PCEPLspaObject getLspa() {
162 public final PCEPExplicitRouteObject getEro() {
167 * @return the bandwidth
169 public final PCEPRequestedPathBandwidthObject getBandwidth() {
170 return this.bandwidth;
174 * @return the metrics
176 public final List<PCEPMetricObject> getMetrics() {
181 * @see java.lang.Object#hashCode()
184 public int hashCode() {
185 final int prime = 31;
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());
196 * @see java.lang.Object#equals(java.lang.Object)
199 public boolean equals(final Object obj) {
204 if (!(obj instanceof CompositeInstantiationObject))
206 final CompositeInstantiationObject other = (CompositeInstantiationObject) obj;
207 if (this.bandwidth == null) {
208 if (other.bandwidth != null)
210 } else if (!this.bandwidth.equals(other.bandwidth))
212 if (this.endPoints == null) {
213 if (other.endPoints != null)
215 } else if (!this.endPoints.equals(other.endPoints))
217 if (this.ero == null) {
218 if (other.ero != null)
220 } else if (!this.ero.equals(other.ero))
222 if (this.lspa == null) {
223 if (other.lspa != null)
225 } else if (!this.lspa.equals(other.lspa))
227 if (this.metrics == null) {
228 if (other.metrics != null)
230 } else if (!this.metrics.equals(other.metrics))
236 * @see java.lang.Object#toString()
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);
252 return builder.toString();