Merge "New JUnit for PolicyEngineAPI"
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / std / pap / StdPDPStatus.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.xacml.std.pap;
21
22 import java.io.Serializable;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import com.att.research.xacml.api.pap.PDPPIPConfig;
28 import com.att.research.xacml.api.pap.PDPPolicy;
29 import com.att.research.xacml.api.pap.PDPStatus;
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31
32 public class StdPDPStatus implements Serializable, PDPStatus {
33         private static final long serialVersionUID = 1L;
34         
35         private Status status = Status.UNKNOWN;
36
37         private Set<String>     loadErrors = new HashSet<>();
38         
39         private Set<String> loadWarnings = new HashSet<>();
40         
41         private transient Set<PDPPolicy> loadedPolicies = new HashSet<>();
42         
43         private transient Set<PDPPolicy> loadedRootPolicies = new HashSet<>();
44         
45         private transient Set<PDPPolicy> failedPolicies = new HashSet<>();
46         
47         private transient Set<PDPPIPConfig>     loadedPIPConfigs = new HashSet<>();
48         
49         private transient Set<PDPPIPConfig>     failedPIPConfigs = new HashSet<>();
50         
51         public StdPDPStatus() {
52                 //
53                 // Default constructor
54                 //
55         }
56
57         public void set(StdPDPStatus newStatus) {
58                 this.status                             =       newStatus.status;
59                 this.loadErrors.clear();
60                 this.loadErrors.addAll(newStatus.getLoadErrors());
61                 this.loadWarnings.clear();
62                 this.loadWarnings.addAll(newStatus.getLoadWarnings());
63                 this.loadedPolicies.clear();
64                 this.loadedPolicies.addAll(newStatus.getLoadedPolicies());
65                 this.loadedRootPolicies.clear();
66                 this.loadedRootPolicies.addAll(newStatus.getLoadedRootPolicies());
67                 this.failedPolicies.clear();
68                 this.failedPolicies.addAll(newStatus.getFailedPolicies());
69                 this.loadedPIPConfigs.clear();
70                 this.loadedPIPConfigs.addAll(newStatus.getLoadedPipConfigs());
71                 this.failedPIPConfigs.clear();
72                 this.failedPIPConfigs.addAll(newStatus.getFailedPipConfigs());
73         }
74         
75         
76
77         @Override
78         public Status getStatus() {
79                 return this.status;
80         }
81         
82         public void setStatus(Status status) {
83                 this.status = status;
84         }
85         
86         @Override
87         public Set<String> getLoadErrors() {
88                 return Collections.unmodifiableSet(this.loadErrors);
89         }
90
91         public void setLoadErrors(Set<String> errors) {
92                 this.loadErrors = errors;
93         }
94         
95         public void addLoadError(String error) {
96                 this.loadErrors.add(error);
97         }
98
99         @Override
100         public Set<String> getLoadWarnings() {
101                 return Collections.unmodifiableSet(this.loadWarnings);
102         }
103
104         public void setLoadWarnings(Set<String> warnings) {
105                 this.loadWarnings = warnings;
106         }
107         
108         public void addLoadWarning(String warning) {
109                 this.loadWarnings.add(warning);
110         }
111
112         @Override
113         public Set<PDPPolicy> getLoadedPolicies() {
114                 return Collections.unmodifiableSet(this.loadedPolicies);
115         }
116         
117         public void setLoadedPolicies(Set<PDPPolicy> policies) {
118                 this.loadedPolicies = policies;
119         }
120         
121         public void addLoadedPolicy(PDPPolicy policy) {
122                 this.loadedPolicies.add(policy);
123         }
124         
125         @Override
126         public Set<PDPPolicy> getLoadedRootPolicies() {
127                 return Collections.unmodifiableSet(this.loadedRootPolicies);
128         }
129         
130         public void setLoadedRootPolicies(Set<PDPPolicy> policies) {
131                 this.loadedRootPolicies = policies;
132         }
133         
134         public void addRootPolicy(PDPPolicy policy) {
135                 this.loadedRootPolicies.add(policy);
136         }
137         
138         public void addAllLoadedRootPolicies(Set<PDPPolicy> policies) {
139                 this.loadedRootPolicies.addAll(policies);
140         }
141         
142         @Override
143         public Set<PDPPolicy> getFailedPolicies() {
144                 return Collections.unmodifiableSet(this.failedPolicies);
145         }
146         
147         public void     setFailedPolicies(Set<PDPPolicy> policies) {
148                 this.failedPolicies = policies;
149         }
150         
151         public void addFailedPolicy(PDPPolicy policy) {
152                 this.failedPolicies.add(policy);
153         }
154
155         @Override
156         public boolean policiesOK() {
157                 if (!this.failedPolicies.isEmpty()) {
158                         return false;
159                 }
160                 return true;
161         }
162
163         @Override
164         public Set<PDPPIPConfig> getLoadedPipConfigs() {
165                 return Collections.unmodifiableSet(this.loadedPIPConfigs);
166         }
167         
168         public void setLoadedPipConfigs(Set<PDPPIPConfig> configs) {
169                 this.loadedPIPConfigs = configs;
170         }
171         
172         public void addLoadedPipConfig(PDPPIPConfig config) {
173                 this.loadedPIPConfigs.add(config);
174         }
175
176         @Override
177         public Set<PDPPIPConfig> getFailedPipConfigs() {
178                 return Collections.unmodifiableSet(this.failedPIPConfigs);
179         }
180
181         public void setFailedPipConfigs(Set<PDPPIPConfig> configs) {
182                 this.failedPIPConfigs = configs;
183         }
184         
185         public void addFailedPipConfig(PDPPIPConfig config) {
186                 this.failedPIPConfigs.add(config);
187         }
188
189         @Override
190         public boolean pipConfigOK() {
191                 if (!this.failedPIPConfigs.isEmpty()) {
192                         return false;
193                 }
194                 return true;
195         }
196
197         @Override
198         @JsonIgnore
199         public boolean isOk() {
200                 if (!this.policiesOK()) {
201                         return false;
202                 }
203                 if (!this.pipConfigOK()) {
204                         return false;
205                 }
206                 return this.status == Status.UP_TO_DATE;
207         }
208
209         @Override
210         public int hashCode() {
211                 final int prime = 31;
212                 int result = 1;
213                 result = prime
214                                 * result
215                                 + (failedPIPConfigs.hashCode());
216                 result = prime * result
217                                 + (failedPolicies.hashCode());
218                 result = prime * result
219                                 + (loadErrors.hashCode());
220                 result = prime * result
221                                 + (loadWarnings.hashCode());
222                 result = prime
223                                 * result
224                                 + (loadedPIPConfigs.hashCode());
225                 result = prime * result
226                                 + (loadedPolicies.hashCode());
227                 result = prime * result + (status.hashCode());
228                 return result;
229         }
230
231         @Override
232         public boolean equals(Object obj) {
233                 if (this == obj)
234                         return true;
235                 if (obj == null)
236                         return false;
237                 if (getClass() != obj.getClass())
238                         return false;
239                 StdPDPStatus other = (StdPDPStatus) obj;
240                 if (!failedPIPConfigs.equals(other.failedPIPConfigs))
241                         return false;
242                 if (!failedPolicies.equals(other.failedPolicies))
243                         return false;
244                 if (!loadErrors.equals(other.loadErrors))
245                         return false;
246                 if (!loadWarnings.equals(other.loadWarnings))
247                         return false;
248                 if (!loadedPIPConfigs.equals(other.loadedPIPConfigs))
249                         return false;
250                 if (!loadedPolicies.equals(other.loadedPolicies))
251                         return false;
252         if (!loadedRootPolicies.equals(other.loadedRootPolicies))
253             return false;
254                 if (status != other.status)
255                         return false;
256                 return true;
257         }
258
259         @Override
260         public String toString() {
261                 return "StdPDPStatus [status=" + status + ", loadErrors=" + loadErrors
262                                 + ", loadWarnings=" + loadWarnings + ", loadedPolicies="
263                                 + loadedPolicies + ", loadedRootPolicies=" + loadedRootPolicies 
264                                 + ", failedPolicies=" + failedPolicies
265                                 + ", loadedPIPConfigs=" + loadedPIPConfigs
266                                 + ", failedPIPConfigs=" + failedPIPConfigs + "]";
267         }
268
269
270 }