Merge "Randomize port to allow concurrent execution"
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeNotifyObject.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.object;
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
16 /**
17  * Structure that combines set of related objects.
18  *
19  * @see <a href="http://tools.ietf.org/html/rfc5440#section-6.6">Notification
20  *      (PCNtf) Message</a> - &lt;notify&gt;
21  */
22 public class CompositeNotifyObject {
23
24         private List<PCEPRequestParameterObject> requestParameters;
25
26         private final List<PCEPNotificationObject> notifications;
27
28         /**
29          * Constructs basic composite object only with mandatory objects.
30          *
31          * @param notifications
32          *            List<PCEPNotificationObject>. Can't be null or empty.
33          */
34         public CompositeNotifyObject(List<PCEPNotificationObject> notifications) {
35                 this(null, notifications);
36         }
37
38         /**
39          * Constructs composite object with optional objects.
40          *
41          * @param requestParameters
42          *            List<PCEPRequestParameterObject>
43          * @param notifications
44          *            List<PCEPNotificationObject>. Can't be null or empty.
45          */
46         public CompositeNotifyObject(List<PCEPRequestParameterObject> requestParameters, List<PCEPNotificationObject> notifications) {
47                 if (notifications == null || notifications.isEmpty())
48                         throw new IllegalArgumentException("Notification Object is mandatory.");
49                 if (requestParameters != null)
50                         this.requestParameters = requestParameters;
51                 else
52                         this.requestParameters = Collections.emptyList();
53                 this.notifications = notifications;
54         }
55
56         /**
57          * Gets list of all objects, which are in appropriate order.
58          *
59          * @return List<PCEPObject>
60          */
61         public List<PCEPObject> getCompositeAsList() {
62                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
63                 if (this.requestParameters != null && !this.requestParameters.isEmpty())
64                         list.addAll(this.requestParameters);
65                 list.addAll(this.notifications);
66                 return list;
67         }
68
69         /**
70          * Creates this object from a list of PCEPObjects.
71          * @param objects List<PCEPObject> list of PCEPObjects from whose this
72          * object should be created.
73          * @return CompositeNotifyObject
74          */
75         public static CompositeNotifyObject getCompositeFromList(List<PCEPObject> objects) {
76                 if (objects == null || objects.isEmpty())
77                         throw new IllegalArgumentException("List cannot be null or empty.");
78
79                 final List<PCEPRequestParameterObject> requestParameters = new ArrayList<PCEPRequestParameterObject>();
80                 final List<PCEPNotificationObject> notifications = new ArrayList<PCEPNotificationObject>();
81
82                 int state = 1;
83                 while (!objects.isEmpty()) {
84                         final PCEPObject obj = objects.get(0);
85                         switch (state) {
86                                 case 1:
87                                         state = 2;
88                                         if (obj instanceof PCEPRequestParameterObject) {
89                                                 requestParameters.add((PCEPRequestParameterObject) obj);
90                                                 state = 1;
91                                                 break;
92                                         }
93                                 case 2:
94                                         state = 3;
95                                         if (obj instanceof PCEPNotificationObject) {
96                                                 notifications.add((PCEPNotificationObject) obj);
97                                                 state = 2;
98                                                 break;
99                                         }
100                         }
101
102                         if (state == 3) {
103                                 break;
104                         }
105
106                         objects.remove(obj);
107                 }
108
109                 if (notifications.isEmpty())
110                         return null;
111
112                 return new CompositeNotifyObject(requestParameters, notifications);
113         }
114
115         /**
116          * Gets list of {@link PCEPRequestParameterObject}.
117          *
118          * @return List<PCEPRequestParameterObject>. Can't be null, but may be
119          *         empty.
120          */
121         public List<PCEPRequestParameterObject> getRequestParameters() {
122                 return this.requestParameters;
123         }
124
125         /**
126          * Gets list of {@link PCEPNotificationObject}.
127          *
128          * @return List<PCEPNotificationObject>. Can't be null or empty.
129          */
130         public List<PCEPNotificationObject> getNotificationObjects() {
131                 return this.notifications;
132         }
133
134         @Override
135         public int hashCode() {
136                 final int prime = 31;
137                 int result = 1;
138                 result = prime * result + ((this.notifications == null) ? 0 : this.notifications.hashCode());
139                 result = prime * result + ((this.requestParameters == null) ? 0 : this.requestParameters.hashCode());
140                 return result;
141         }
142
143         @Override
144         public boolean equals(Object obj) {
145                 if (this == obj)
146                         return true;
147                 if (obj == null)
148                         return false;
149                 if (this.getClass() != obj.getClass())
150                         return false;
151                 final CompositeNotifyObject other = (CompositeNotifyObject) obj;
152                 if (this.notifications == null) {
153                         if (other.notifications != null)
154                                 return false;
155                 } else if (!this.notifications.equals(other.notifications))
156                         return false;
157                 if (this.requestParameters == null) {
158                         if (other.requestParameters != null)
159                                 return false;
160                 } else if (!this.requestParameters.equals(other.requestParameters))
161                         return false;
162                 return true;
163         }
164
165         @Override
166         public String toString() {
167                 final StringBuilder builder = new StringBuilder();
168                 builder.append("CompositeNotifyObject [requestParameters=");
169                 builder.append(this.requestParameters);
170                 builder.append(", notifications=");
171                 builder.append(this.notifications);
172                 builder.append("]");
173                 return builder.toString();
174         }
175 }