BUG-47 : removed PCEPMessage interface, switched to generated Message.
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / message / PCEPReplyMessage.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.Collections;
11 import java.util.List;
12
13 import org.opendaylight.protocol.pcep.PCEPObject;
14 import org.opendaylight.protocol.pcep.object.CompositeReplySvecObject;
15 import org.opendaylight.protocol.pcep.object.CompositeResponseObject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
17
18 import com.google.common.collect.Lists;
19
20 /**
21  * Structure for Reply Message.
22  * 
23  * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.5">Reply Message</a>
24  */
25 public class PCEPReplyMessage implements Message {
26
27         private final List<CompositeReplySvecObject> svecList;
28
29         private final List<CompositeResponseObject> responses;
30
31         private final List<PCEPObject> objects;
32
33         /**
34          * Constructs new Reply Message.
35          * 
36          * @throws IllegalArgumentException if there is not even one {@link CompositeResponseObject} in the list.
37          * 
38          * @param responses List<CompositeResponseObject>. Can't be empty or null.
39          */
40         public PCEPReplyMessage(final List<CompositeResponseObject> responses) {
41                 this(responses, null);
42         }
43
44         /**
45          * Constructs {@link PCEPReplyMessage}.
46          * 
47          * @throws IllegalArgumentException if there is not even one {@link CompositeResponseObject} in the list.
48          * 
49          * @param svecList List<CompositeSvecObject>
50          * @param responses List<CompositeResponseObject>. Can't be empty or null.
51          */
52         public PCEPReplyMessage(final List<CompositeResponseObject> responses, final List<CompositeReplySvecObject> svecList) {
53                 this.objects = Lists.newArrayList();
54                 if (svecList != null)
55                         for (final CompositeReplySvecObject cno : svecList) {
56                                 this.objects.addAll(cno.getCompositeAsList());
57                         }
58                 if (responses != null)
59                         for (final CompositeResponseObject cno : responses) {
60                                 this.objects.addAll(cno.getCompositeAsList());
61                         }
62
63                 if (responses == null || responses.isEmpty())
64                         throw new IllegalArgumentException("At least one CompositeResponseObject is mandatory.");
65                 this.responses = responses;
66
67                 if (svecList != null)
68                         this.svecList = svecList;
69                 else
70                         this.svecList = Collections.emptyList();
71         }
72
73         /**
74          * Gets list of {@link CompositeResponseObject}.
75          * 
76          * @return List<CompositeResponseObject>. Can't be null or empty.
77          */
78         public List<CompositeResponseObject> getResponses() {
79                 return this.responses;
80         }
81
82         /**
83          * Gets list of {@link CompositeReplySvecObject}.
84          * 
85          * @return List<CompositeReplySvecObject>. Can't be null but may be empty.
86          */
87         public List<CompositeReplySvecObject> getSvecList() {
88                 return this.svecList;
89         }
90
91         public List<PCEPObject> getAllObjects() {
92                 return this.objects;
93         }
94
95         @Override
96         public int hashCode() {
97                 final int prime = 31;
98                 int result = super.hashCode();
99                 result = prime * result + ((this.responses == null) ? 0 : this.responses.hashCode());
100                 result = prime * result + ((this.svecList == null) ? 0 : this.svecList.hashCode());
101                 return result;
102         }
103
104         @Override
105         public boolean equals(final Object obj) {
106                 if (this == obj)
107                         return true;
108                 if (!super.equals(obj))
109                         return false;
110                 if (this.getClass() != obj.getClass())
111                         return false;
112                 final PCEPReplyMessage other = (PCEPReplyMessage) obj;
113                 if (this.responses == null) {
114                         if (other.responses != null)
115                                 return false;
116                 } else if (!this.responses.equals(other.responses))
117                         return false;
118                 if (this.svecList == null) {
119                         if (other.svecList != null)
120                                 return false;
121                 } else if (!this.svecList.equals(other.svecList))
122                         return false;
123                 return true;
124         }
125
126         @Override
127         public String toString() {
128                 final StringBuilder builder = new StringBuilder();
129                 builder.append("PCEPReplyMessage [svecList=");
130                 builder.append(this.svecList);
131                 builder.append(", responses=");
132                 builder.append(this.responses);
133                 builder.append("]");
134                 return builder.toString();
135         }
136 }