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.message;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.protocol.pcep.PCEPMessage;
15 import org.opendaylight.protocol.pcep.PCEPObject;
16 import org.opendaylight.protocol.pcep.object.CompositeErrorObject;
17 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
18 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
21 * Structure of Error Message.
23 * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.7">Error
26 public class PCEPErrorMessage extends PCEPMessage {
28 private static final long serialVersionUID = 604782651368689577L;
30 private PCEPOpenObject openObj;
32 private final List<PCEPErrorObject> errorObjects;
34 private final List<CompositeErrorObject> errors;
36 public PCEPErrorMessage(final PCEPErrorObject errorObject) {
37 this(new ArrayList<PCEPErrorObject>() {
38 private static final long serialVersionUID = 72172137965955228L;
41 this.add(errorObject);
46 public PCEPErrorMessage(final CompositeErrorObject compositeErrorObject) {
47 this(new ArrayList<CompositeErrorObject>() {
48 private static final long serialVersionUID = 72172137965955228L;
51 if (compositeErrorObject != null)
52 this.add(compositeErrorObject);
58 * Constructs Error Message from list of {@link PCEPErrorObject} or
59 * {@link CompositeErrorObject}.
62 * List<?> either objects of type: {@link PCEPErrorObject} or
63 * {@link CompositeErrorObject}
65 * @throws IllegalArgumentException
66 * if any other type is passed in the list, that cannot be
69 public PCEPErrorMessage(final List<?> errorObjects) {
70 super(new ArrayList<PCEPObject>() {
72 private static final long serialVersionUID = -8607527575304297642L;
75 if (errorObjects != null)
76 for (int i = 0; i < errorObjects.size(); i++) {
77 if (errorObjects.get(i) instanceof CompositeErrorObject) {
78 this.addAll(((CompositeErrorObject) errorObjects.get(i)).getCompositeAsList());
79 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
80 this.add((PCEPErrorObject) errorObjects.get(i));
85 this.errors = new ArrayList<CompositeErrorObject>();
86 this.errorObjects = new ArrayList<PCEPErrorObject>();
88 if (errorObjects != null) {
89 for (int i = 0; i < errorObjects.size(); i++) {
90 if (errorObjects.get(i) instanceof CompositeErrorObject) {
91 this.errors.add((CompositeErrorObject) errorObjects.get(i));
92 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
93 this.errorObjects.add((PCEPErrorObject) errorObjects.get(i));
95 throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
101 * Constructs Error Message from list of {@link PCEPErrorObject} and
102 * {@link CompositeErrorObject} and {@link PCEPOpenObject} that cannot be
103 * null. This constructor is used during PCEP handshake to suggest new
104 * session characteristics for the session that are listen in
105 * {@link PCEPOpenObject}.
108 * {@link PCEPOpenObject} cannot be null
109 * @param errorObjects
110 * List<PCEPErrorObject> list of error objects
112 * List<CompositeErrorObject> list of composite error objects
114 public PCEPErrorMessage(final PCEPOpenObject openObj, final List<PCEPErrorObject> errorObjects, final List<CompositeErrorObject> errors) {
115 super(new ArrayList<PCEPObject>() {
117 private static final long serialVersionUID = -4238105145756981972L;
120 if (errorObjects != null)
121 this.addAll(errorObjects);
125 for (final CompositeErrorObject ceo : errors) {
126 this.addAll(ceo.getCompositeAsList());
130 this.openObj = openObj;
132 if (errorObjects == null)
133 throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
134 this.errorObjects = errorObjects;
137 this.errors = Collections.emptyList();
139 this.errors = errors;
143 * Gets {@link PCEPOpenObject} if this is included. If its included, it
144 * proposes alternative acceptable session characteristic values.
146 * @return PCEPOpenObject. May be null.
148 public PCEPOpenObject getOpenObject() {
153 * In unsolicited manner can be included List of
154 * <code>PCEPErrorObjects</code> <code>PCEPErrorMessage</code>, which is not
155 * sent in response to a request.
157 * @return List<PCEPErrorObject>
159 public List<PCEPErrorObject> getErrorObjects() {
160 return this.errorObjects;
164 * If the PCErr message is sent in response to a request, the PCErr message
165 * MUST include set of RP objects related to pending path computation
166 * requests that triggered the error condition. In this situation it is
167 * constructed as {@link org.opendaylight.protocol.pcep.object.CompositeErrorObject
168 * CompCompositeErrorObject}. That includes list of RP objects.
170 * @return CompositeErrorObject. May be null.
172 public List<CompositeErrorObject> getErrors() {
177 public int hashCode() {
178 final int prime = 31;
179 int result = super.hashCode();
180 result = prime * result + ((this.errorObjects == null) ? 0 : this.errorObjects.hashCode());
181 result = prime * result + ((this.errors == null) ? 0 : this.errors.hashCode());
182 result = prime * result + ((this.openObj == null) ? 0 : this.openObj.hashCode());
187 public boolean equals(Object obj) {
190 if (!super.equals(obj))
192 if (this.getClass() != obj.getClass())
194 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
195 if (this.errorObjects == null) {
196 if (other.errorObjects != null)
198 } else if (!this.errorObjects.equals(other.errorObjects))
200 if (this.errors == null) {
201 if (other.errors != null)
203 } else if (!this.errors.equals(other.errors))
205 if (this.openObj == null) {
206 if (other.openObj != null)
208 } else if (!this.openObj.equals(other.openObj))
214 public String toString() {
215 final StringBuilder builder = new StringBuilder();
216 builder.append("PCEPErrorMessage [openObj=");
217 builder.append(this.openObj);
218 builder.append(", errorObjects=");
219 builder.append(this.errorObjects);
220 builder.append(", errors=");
221 builder.append(this.errors);
223 return builder.toString();