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);
59 * Constructs Error Message from list of {@link PCEPErrorObject} or {@link CompositeErrorObject}.
61 * @param errorObjects List<?> either objects of type: {@link PCEPErrorObject} or {@link CompositeErrorObject}
63 * @throws IllegalArgumentException if any other type is passed in the list, that cannot be processed
65 public PCEPErrorMessage(final List<?> errorObjects) {
66 this.objects = Lists.newArrayList();
67 if (errorObjects != null)
68 for (int i = 0; i < errorObjects.size(); i++) {
69 if (errorObjects.get(i) instanceof CompositeErrorObject) {
70 this.objects.addAll(((CompositeErrorObject) errorObjects.get(i)).getCompositeAsList());
71 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
72 this.objects.add((PCEPErrorObject) errorObjects.get(i));
75 this.errors = new ArrayList<CompositeErrorObject>();
76 this.errorObjects = new ArrayList<PCEPErrorObject>();
78 if (errorObjects != null) {
79 for (int i = 0; i < errorObjects.size(); i++) {
80 if (errorObjects.get(i) instanceof CompositeErrorObject) {
81 this.errors.add((CompositeErrorObject) errorObjects.get(i));
82 } else if (errorObjects.get(i) instanceof PCEPErrorObject) {
83 this.errorObjects.add((PCEPErrorObject) errorObjects.get(i));
85 throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
91 * Constructs Error Message from list of {@link PCEPErrorObject} and {@link CompositeErrorObject} and
92 * {@link PCEPOpenObject} that cannot be null. This constructor is used during PCEP handshake to suggest new session
93 * characteristics for the session that are listen in {@link PCEPOpenObject}.
95 * @param openObj {@link PCEPOpenObject} cannot be null
96 * @param errorObjects List<PCEPErrorObject> list of error objects
97 * @param errors List<CompositeErrorObject> list of composite error objects
99 public PCEPErrorMessage(final PCEPOpenObject openObj, final List<PCEPErrorObject> errorObjects, final List<CompositeErrorObject> errors) {
100 this.objects = Lists.newArrayList();
101 if (errorObjects != null)
102 this.objects.addAll(errorObjects);
104 this.objects.add(openObj);
106 for (final CompositeErrorObject ceo : errors) {
107 this.objects.addAll(ceo.getCompositeAsList());
110 this.openObj = openObj;
112 if (errorObjects == null)
113 throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
114 this.errorObjects = errorObjects;
117 this.errors = Collections.emptyList();
119 this.errors = errors;
123 * Gets {@link PCEPOpenObject} if this is included. If its included, it proposes alternative acceptable session
124 * characteristic values.
126 * @return PCEPOpenObject. May be null.
128 public PCEPOpenObject getOpenObject() {
133 * In unsolicited manner can be included List of <code>PCEPErrorObjects</code> <code>PCEPErrorMessage</code>, which
134 * is not sent in response to a request.
136 * @return List<PCEPErrorObject>
138 public List<PCEPErrorObject> getErrorObjects() {
139 return this.errorObjects;
143 * If the PCErr message is sent in response to a request, the PCErr message MUST include set of RP objects related
144 * to pending path computation requests that triggered the error condition. In this situation it is constructed as
145 * {@link org.opendaylight.protocol.pcep.object.CompositeErrorObject CompCompositeErrorObject}. That includes list
148 * @return CompositeErrorObject. May be null.
150 public List<CompositeErrorObject> getErrors() {
154 public List<PCEPObject> getAllObjects() {
159 public int hashCode() {
160 final int prime = 31;
161 int result = super.hashCode();
162 result = prime * result + ((this.errorObjects == null) ? 0 : this.errorObjects.hashCode());
163 result = prime * result + ((this.errors == null) ? 0 : this.errors.hashCode());
164 result = prime * result + ((this.openObj == null) ? 0 : this.openObj.hashCode());
169 public boolean equals(final Object obj) {
172 if (!super.equals(obj))
174 if (this.getClass() != obj.getClass())
176 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
177 if (this.errorObjects == null) {
178 if (other.errorObjects != null)
180 } else if (!this.errorObjects.equals(other.errorObjects))
182 if (this.errors == null) {
183 if (other.errors != null)
185 } else if (!this.errors.equals(other.errors))
187 if (this.openObj == null) {
188 if (other.openObj != null)
190 } else if (!this.openObj.equals(other.openObj))
196 public String toString() {
197 final StringBuilder builder = new StringBuilder();
198 builder.append("PCEPErrorMessage [openObj=");
199 builder.append(this.openObj);
200 builder.append(", errorObjects=");
201 builder.append(this.errorObjects);
202 builder.append(", errors=");
203 builder.append(this.errors);
205 return builder.toString();