Merge "Randomize port to allow concurrent execution"
[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.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;
19
20 import com.google.common.collect.Lists;
21
22 /**
23  * Structure of Error Message.
24  * 
25  * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.7">Error Message</a>
26  */
27 public class PCEPErrorMessage implements Message {
28
29         private PCEPOpenObject openObj;
30
31         private final List<PCEPErrorObject> errorObjects;
32
33         private final List<CompositeErrorObject> errors;
34
35         private final List<PCEPObject> objects;
36
37         public PCEPErrorMessage(final PCEPErrorObject errorObject) {
38                 this(new ArrayList<PCEPErrorObject>() {
39                         private static final long serialVersionUID = 72172137965955228L;
40
41                         {
42                                 this.add(errorObject);
43                         }
44                 });
45         }
46
47         public PCEPErrorMessage(final CompositeErrorObject compositeErrorObject) {
48                 this(new ArrayList<CompositeErrorObject>() {
49                         private static final long serialVersionUID = 72172137965955228L;
50
51                         {
52                                 if (compositeErrorObject != null) {
53                                         this.add(compositeErrorObject);
54                                 }
55                         }
56                 });
57         }
58
59         /**
60          * Constructs Error Message from list of {@link PCEPErrorObject} or {@link CompositeErrorObject}.
61          * 
62          * @param errorObjects List<?> either objects of type: {@link PCEPErrorObject} or {@link CompositeErrorObject}
63          * 
64          * @throws IllegalArgumentException if any other type is passed in the list, that cannot be processed
65          */
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));
74                                 }
75                         }
76                 }
77                 this.errors = new ArrayList<CompositeErrorObject>();
78                 this.errorObjects = new ArrayList<PCEPErrorObject>();
79
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));
86                                 } else {
87                                         throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
88                                 }
89                         }
90                 }
91         }
92
93         /**
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}.
97          * 
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
101          */
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);
106                 }
107                 if (openObj != null) {
108                         this.objects.add(openObj);
109                 }
110                 if (errors != null) {
111                         for (final CompositeErrorObject ceo : errors) {
112                                 this.objects.addAll(ceo.getCompositeAsList());
113                         }
114                 }
115
116                 this.openObj = openObj;
117
118                 if (errorObjects == null) {
119                         throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
120                 }
121                 this.errorObjects = errorObjects;
122
123                 if (errors == null) {
124                         this.errors = Collections.emptyList();
125                 } else {
126                         this.errors = errors;
127                 }
128         }
129
130         /**
131          * Gets {@link PCEPOpenObject} if this is included. If its included, it proposes alternative acceptable session
132          * characteristic values.
133          * 
134          * @return PCEPOpenObject. May be null.
135          */
136         public PCEPOpenObject getOpenObject() {
137                 return this.openObj;
138         }
139
140         /**
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.
143          * 
144          * @return List<PCEPErrorObject>
145          */
146         public List<PCEPErrorObject> getErrorObjects() {
147                 return this.errorObjects;
148         }
149
150         /**
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
154          * of RP objects.
155          * 
156          * @return CompositeErrorObject. May be null.
157          */
158         public List<CompositeErrorObject> getErrors() {
159                 return this.errors;
160         }
161
162         public List<PCEPObject> getAllObjects() {
163                 return this.objects;
164         }
165
166         @Override
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());
173                 return result;
174         }
175
176         @Override
177         public boolean equals(final Object obj) {
178                 if (this == obj) {
179                         return true;
180                 }
181                 if (!super.equals(obj)) {
182                         return false;
183                 }
184                 if (this.getClass() != obj.getClass()) {
185                         return false;
186                 }
187                 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
188                 if (this.errorObjects == null) {
189                         if (other.errorObjects != null) {
190                                 return false;
191                         }
192                 } else if (!this.errorObjects.equals(other.errorObjects)) {
193                         return false;
194                 }
195                 if (this.errors == null) {
196                         if (other.errors != null) {
197                                 return false;
198                         }
199                 } else if (!this.errors.equals(other.errors)) {
200                         return false;
201                 }
202                 if (this.openObj == null) {
203                         if (other.openObj != null) {
204                                 return false;
205                         }
206                 } else if (!this.openObj.equals(other.openObj)) {
207                         return false;
208                 }
209                 return true;
210         }
211
212         @Override
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);
221                 builder.append("]");
222                 return builder.toString();
223         }
224
225         @Override
226         public Class<Message> getImplementedInterface() {
227                 return Message.class;
228         }
229 }