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.PCEPObject;
15 import org.opendaylight.protocol.pcep.object.CompositeErrorObject;
16 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
17 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
20 import com.google.common.collect.Lists;
23 * Structure of Error Message.
25 * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.7">Error Message</a>
27 public class PCEPErrorMessage implements Message {
29 private PCEPOpenObject openObj;
31 private final List<PCEPErrorObject> errorObjects;
33 private final List<CompositeErrorObject> errors;
35 private final List<PCEPObject> objects;
37 public PCEPErrorMessage(final PCEPErrorObject errorObject) {
38 this(new ArrayList<PCEPErrorObject>() {
39 private static final long serialVersionUID = 72172137965955228L;
42 this.add(errorObject);
47 public PCEPErrorMessage(final CompositeErrorObject compositeErrorObject) {
48 this(new ArrayList<CompositeErrorObject>() {
49 private static final long serialVersionUID = 72172137965955228L;
52 if (compositeErrorObject != null) {
53 this.add(compositeErrorObject);
60 * Constructs Error Message from list of {@link PCEPErrorObject} or {@link CompositeErrorObject}.
62 * @param errorObjects List<?> either objects of type: {@link PCEPErrorObject} or {@link CompositeErrorObject}
64 * @throws IllegalArgumentException if any other type is passed in the list, that cannot be processed
66 public PCEPErrorMessage(final List<?> errorObjects) {
67 this.objects = Lists.newArrayList();
68 if (errorObjects != null) {
69 for (int i = 0; i < errorObjects.size(); i++) {
70 if (errorObjects.get(i) instanceof CompositeErrorObject) {
71 this.objects.addAll(((CompositeErrorObject) errorObjects.get(i)).getCompositeAsList());
72 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
73 this.objects.add((PCEPErrorObject) errorObjects.get(i));
77 this.errors = new ArrayList<CompositeErrorObject>();
78 this.errorObjects = new ArrayList<PCEPErrorObject>();
80 if (errorObjects != null) {
81 for (int i = 0; i < errorObjects.size(); i++) {
82 if (errorObjects.get(i) instanceof CompositeErrorObject) {
83 this.errors.add((CompositeErrorObject) errorObjects.get(i));
84 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
85 this.errorObjects.add((PCEPErrorObject) errorObjects.get(i));
87 throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
94 * Constructs Error Message from list of {@link PCEPErrorObject} and {@link CompositeErrorObject} and
95 * {@link PCEPOpenObject} that cannot be null. This constructor is used during PCEP handshake to suggest new session
96 * characteristics for the session that are listen in {@link PCEPOpenObject}.
98 * @param openObj {@link PCEPOpenObject} cannot be null
99 * @param errorObjects List<PCEPErrorObject> list of error objects
100 * @param errors List<CompositeErrorObject> list of composite error objects
102 public PCEPErrorMessage(final PCEPOpenObject openObj, final List<PCEPErrorObject> errorObjects, final List<CompositeErrorObject> errors) {
103 this.objects = Lists.newArrayList();
104 if (errorObjects != null) {
105 this.objects.addAll(errorObjects);
107 if (openObj != null) {
108 this.objects.add(openObj);
110 if (errors != null) {
111 for (final CompositeErrorObject ceo : errors) {
112 this.objects.addAll(ceo.getCompositeAsList());
116 this.openObj = openObj;
118 if (errorObjects == null) {
119 throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
121 this.errorObjects = errorObjects;
123 if (errors == null) {
124 this.errors = Collections.emptyList();
126 this.errors = errors;
131 * Gets {@link PCEPOpenObject} if this is included. If its included, it proposes alternative acceptable session
132 * characteristic values.
134 * @return PCEPOpenObject. May be null.
136 public PCEPOpenObject getOpenObject() {
141 * In unsolicited manner can be included List of <code>PCEPErrorObjects</code> <code>PCEPErrorMessage</code>, which
142 * is not sent in response to a request.
144 * @return List<PCEPErrorObject>
146 public List<PCEPErrorObject> getErrorObjects() {
147 return this.errorObjects;
151 * If the PCErr message is sent in response to a request, the PCErr message MUST include set of RP objects related
152 * to pending path computation requests that triggered the error condition. In this situation it is constructed as
153 * {@link org.opendaylight.protocol.pcep.object.CompositeErrorObject CompCompositeErrorObject}. That includes list
156 * @return CompositeErrorObject. May be null.
158 public List<CompositeErrorObject> getErrors() {
162 public List<PCEPObject> getAllObjects() {
167 public int hashCode() {
168 final int prime = 31;
169 int result = super.hashCode();
170 result = prime * result + ((this.errorObjects == null) ? 0 : this.errorObjects.hashCode());
171 result = prime * result + ((this.errors == null) ? 0 : this.errors.hashCode());
172 result = prime * result + ((this.openObj == null) ? 0 : this.openObj.hashCode());
177 public boolean equals(final Object obj) {
181 if (!super.equals(obj)) {
184 if (this.getClass() != obj.getClass()) {
187 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
188 if (this.errorObjects == null) {
189 if (other.errorObjects != null) {
192 } else if (!this.errorObjects.equals(other.errorObjects)) {
195 if (this.errors == null) {
196 if (other.errors != null) {
199 } else if (!this.errors.equals(other.errors)) {
202 if (this.openObj == null) {
203 if (other.openObj != null) {
206 } else if (!this.openObj.equals(other.openObj)) {
213 public String toString() {
214 final StringBuilder builder = new StringBuilder();
215 builder.append("PCEPErrorMessage [openObj=");
216 builder.append(this.openObj);
217 builder.append(", errorObjects=");
218 builder.append(this.errorObjects);
219 builder.append(", errors=");
220 builder.append(this.errors);
222 return builder.toString();
226 public Class<Message> getImplementedInterface() {
227 return Message.class;