Initial code drop
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / message / PCEPErrorMessage.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.message;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
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;
19
20 /**
21  * Structure of Error Message.
22  *
23  * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.7">Error
24  *      Message</a>
25  */
26 public class PCEPErrorMessage extends PCEPMessage {
27
28         private static final long serialVersionUID = 604782651368689577L;
29
30         private PCEPOpenObject openObj;
31
32         private final List<PCEPErrorObject> errorObjects;
33
34         private final List<CompositeErrorObject> errors;
35
36         public PCEPErrorMessage(final PCEPErrorObject errorObject) {
37                 this(new ArrayList<PCEPErrorObject>() {
38                         private static final long serialVersionUID = 72172137965955228L;
39
40                         {
41                                 this.add(errorObject);
42                         }
43                 });
44         }
45
46         public PCEPErrorMessage(final CompositeErrorObject compositeErrorObject) {
47                 this(new ArrayList<CompositeErrorObject>() {
48                         private static final long serialVersionUID = 72172137965955228L;
49
50                         {
51                                 if (compositeErrorObject != null)
52                                         this.add(compositeErrorObject);
53                         }
54                 });
55         }
56
57         /**
58          * Constructs Error Message from list of {@link PCEPErrorObject} or
59          * {@link CompositeErrorObject}.
60          *
61          * @param errorObjects
62          *            List<?> either objects of type: {@link PCEPErrorObject} or
63          *            {@link CompositeErrorObject}
64          *
65          * @throws IllegalArgumentException
66          *             if any other type is passed in the list, that cannot be
67          *             processed
68          */
69         public PCEPErrorMessage(final List<?> errorObjects) {
70                 super(new ArrayList<PCEPObject>() {
71
72                         private static final long serialVersionUID = -8607527575304297642L;
73
74                         {
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));
81                                                 }
82                                         }
83                         }
84                 });
85                 this.errors = new ArrayList<CompositeErrorObject>();
86                 this.errorObjects = new ArrayList<PCEPErrorObject>();
87
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));
94                                 } else
95                                         throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
96                         }
97                 }
98         }
99
100         /**
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}.
106          *
107          * @param openObj
108          *            {@link PCEPOpenObject} cannot be null
109          * @param errorObjects
110          *            List<PCEPErrorObject> list of error objects
111          * @param errors
112          *            List<CompositeErrorObject> list of composite error objects
113          */
114         public PCEPErrorMessage(final PCEPOpenObject openObj, final List<PCEPErrorObject> errorObjects, final List<CompositeErrorObject> errors) {
115                 super(new ArrayList<PCEPObject>() {
116
117                         private static final long serialVersionUID = -4238105145756981972L;
118
119                         {
120                                 if (errorObjects != null)
121                                         this.addAll(errorObjects);
122                                 if (openObj != null)
123                                         this.add(openObj);
124                                 if (errors != null)
125                                         for (final CompositeErrorObject ceo : errors) {
126                                                 this.addAll(ceo.getCompositeAsList());
127                                         }
128                         }
129                 });
130                 this.openObj = openObj;
131
132                 if (errorObjects == null)
133                         throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
134                 this.errorObjects = errorObjects;
135
136                 if (errors == null)
137                         this.errors = Collections.emptyList();
138                 else
139                         this.errors = errors;
140         }
141
142         /**
143          * Gets {@link PCEPOpenObject} if this is included. If its included, it
144          * proposes alternative acceptable session characteristic values.
145          *
146          * @return PCEPOpenObject. May be null.
147          */
148         public PCEPOpenObject getOpenObject() {
149                 return this.openObj;
150         }
151
152         /**
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.
156          *
157          * @return List<PCEPErrorObject>
158          */
159         public List<PCEPErrorObject> getErrorObjects() {
160                 return this.errorObjects;
161         }
162
163         /**
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.
169          *
170          * @return CompositeErrorObject. May be null.
171          */
172         public List<CompositeErrorObject> getErrors() {
173                 return this.errors;
174         }
175
176         @Override
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());
183                 return result;
184         }
185
186         @Override
187         public boolean equals(Object obj) {
188                 if (this == obj)
189                         return true;
190                 if (!super.equals(obj))
191                         return false;
192                 if (this.getClass() != obj.getClass())
193                         return false;
194                 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
195                 if (this.errorObjects == null) {
196                         if (other.errorObjects != null)
197                                 return false;
198                 } else if (!this.errorObjects.equals(other.errorObjects))
199                         return false;
200                 if (this.errors == null) {
201                         if (other.errors != null)
202                                 return false;
203                 } else if (!this.errors.equals(other.errors))
204                         return false;
205                 if (this.openObj == null) {
206                         if (other.openObj != null)
207                                 return false;
208                 } else if (!this.openObj.equals(other.openObj))
209                         return false;
210                 return true;
211         }
212
213         @Override
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);
222                 builder.append("]");
223                 return builder.toString();
224         }
225 }