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