BUG-47 : removed PCEPMessage interface, switched to generated Message.
[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          * Constructs Error Message from list of {@link PCEPErrorObject} or {@link CompositeErrorObject}.
60          * 
61          * @param errorObjects List<?> either objects of type: {@link PCEPErrorObject} or {@link CompositeErrorObject}
62          * 
63          * @throws IllegalArgumentException if any other type is passed in the list, that cannot be processed
64          */
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));
73                                 }
74                         }
75                 this.errors = new ArrayList<CompositeErrorObject>();
76                 this.errorObjects = new ArrayList<PCEPErrorObject>();
77
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));
84                                 } else
85                                         throw new IllegalArgumentException("Wrong instance passed in list. Acceptable is only CompositeErrorObject or PCEPErrorObject.");
86                         }
87                 }
88         }
89
90         /**
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}.
94          * 
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
98          */
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);
103                 if (openObj != null)
104                         this.objects.add(openObj);
105                 if (errors != null)
106                         for (final CompositeErrorObject ceo : errors) {
107                                 this.objects.addAll(ceo.getCompositeAsList());
108                         }
109
110                 this.openObj = openObj;
111
112                 if (errorObjects == null)
113                         throw new IllegalArgumentException("At least one PCEPErrorObject is mandatory.");
114                 this.errorObjects = errorObjects;
115
116                 if (errors == null)
117                         this.errors = Collections.emptyList();
118                 else
119                         this.errors = errors;
120         }
121
122         /**
123          * Gets {@link PCEPOpenObject} if this is included. If its included, it proposes alternative acceptable session
124          * characteristic values.
125          * 
126          * @return PCEPOpenObject. May be null.
127          */
128         public PCEPOpenObject getOpenObject() {
129                 return this.openObj;
130         }
131
132         /**
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.
135          * 
136          * @return List<PCEPErrorObject>
137          */
138         public List<PCEPErrorObject> getErrorObjects() {
139                 return this.errorObjects;
140         }
141
142         /**
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
146          * of RP objects.
147          * 
148          * @return CompositeErrorObject. May be null.
149          */
150         public List<CompositeErrorObject> getErrors() {
151                 return this.errors;
152         }
153
154         public List<PCEPObject> getAllObjects() {
155                 return this.objects;
156         }
157
158         @Override
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());
165                 return result;
166         }
167
168         @Override
169         public boolean equals(final Object obj) {
170                 if (this == obj)
171                         return true;
172                 if (!super.equals(obj))
173                         return false;
174                 if (this.getClass() != obj.getClass())
175                         return false;
176                 final PCEPErrorMessage other = (PCEPErrorMessage) obj;
177                 if (this.errorObjects == null) {
178                         if (other.errorObjects != null)
179                                 return false;
180                 } else if (!this.errorObjects.equals(other.errorObjects))
181                         return false;
182                 if (this.errors == null) {
183                         if (other.errors != null)
184                                 return false;
185                 } else if (!this.errors.equals(other.errors))
186                         return false;
187                 if (this.openObj == null) {
188                         if (other.openObj != null)
189                                 return false;
190                 } else if (!this.openObj.equals(other.openObj))
191                         return false;
192                 return true;
193         }
194
195         @Override
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);
204                 builder.append("]");
205                 return builder.toString();
206         }
207 }