Format ONAP-XACML and add JUnit
[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-2019 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
21 package org.onap.policy.xacml.std.pap;
22
23 import com.att.research.xacml.api.pap.PDPPIPConfig;
24 import com.att.research.xacml.api.pap.PDPPolicy;
25 import com.att.research.xacml.api.pap.PDPStatus;
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27
28 import java.io.Serializable;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.Set;
32 import lombok.ToString;
33
34 @ToString
35 public class StdPDPStatus implements Serializable, PDPStatus {
36     private static final long serialVersionUID = 1L;
37
38     private Status status = Status.UNKNOWN;
39
40     private Set<String> loadErrors = new HashSet<>();
41
42     private Set<String> loadWarnings = new HashSet<>();
43
44     private transient Set<PDPPolicy> loadedPolicies = new HashSet<>();
45
46     private transient Set<PDPPolicy> loadedRootPolicies = new HashSet<>();
47
48     private transient Set<PDPPolicy> failedPolicies = new HashSet<>();
49
50     private transient Set<PDPPIPConfig> loadedPIPConfigs = new HashSet<>();
51
52     private transient Set<PDPPIPConfig> failedPIPConfigs = new HashSet<>();
53
54     public StdPDPStatus() {
55         //
56         // Default constructor
57         //
58     }
59
60     public void set(StdPDPStatus newStatus) {
61         this.status = newStatus.status;
62         this.loadErrors.clear();
63         this.loadErrors.addAll(newStatus.getLoadErrors());
64         this.loadWarnings.clear();
65         this.loadWarnings.addAll(newStatus.getLoadWarnings());
66         this.loadedPolicies.clear();
67         this.loadedPolicies.addAll(newStatus.getLoadedPolicies());
68         this.loadedRootPolicies.clear();
69         this.loadedRootPolicies.addAll(newStatus.getLoadedRootPolicies());
70         this.failedPolicies.clear();
71         this.failedPolicies.addAll(newStatus.getFailedPolicies());
72         this.loadedPIPConfigs.clear();
73         this.loadedPIPConfigs.addAll(newStatus.getLoadedPipConfigs());
74         this.failedPIPConfigs.clear();
75         this.failedPIPConfigs.addAll(newStatus.getFailedPipConfigs());
76     }
77
78     @Override
79     public Status getStatus() {
80         return this.status;
81     }
82
83     public void setStatus(Status status) {
84         this.status = status;
85     }
86
87     @Override
88     public Set<String> getLoadErrors() {
89         return Collections.unmodifiableSet(this.loadErrors);
90     }
91
92     public void setLoadErrors(Set<String> errors) {
93         this.loadErrors = errors;
94     }
95
96     public void addLoadError(String error) {
97         this.loadErrors.add(error);
98     }
99
100     @Override
101     public Set<String> getLoadWarnings() {
102         return Collections.unmodifiableSet(this.loadWarnings);
103     }
104
105     public void setLoadWarnings(Set<String> warnings) {
106         this.loadWarnings = warnings;
107     }
108
109     public void addLoadWarning(String warning) {
110         this.loadWarnings.add(warning);
111     }
112
113     @Override
114     public Set<PDPPolicy> getLoadedPolicies() {
115         return Collections.unmodifiableSet(this.loadedPolicies);
116     }
117
118     public void setLoadedPolicies(Set<PDPPolicy> policies) {
119         this.loadedPolicies = policies;
120     }
121
122     public void addLoadedPolicy(PDPPolicy policy) {
123         this.loadedPolicies.add(policy);
124     }
125
126     @Override
127     public Set<PDPPolicy> getLoadedRootPolicies() {
128         return Collections.unmodifiableSet(this.loadedRootPolicies);
129     }
130
131     public void setLoadedRootPolicies(Set<PDPPolicy> policies) {
132         this.loadedRootPolicies = policies;
133     }
134
135     public void addRootPolicy(PDPPolicy policy) {
136         this.loadedRootPolicies.add(policy);
137     }
138
139     public void addAllLoadedRootPolicies(Set<PDPPolicy> policies) {
140         this.loadedRootPolicies.addAll(policies);
141     }
142
143     @Override
144     public Set<PDPPolicy> getFailedPolicies() {
145         return Collections.unmodifiableSet(this.failedPolicies);
146     }
147
148     public void setFailedPolicies(Set<PDPPolicy> policies) {
149         this.failedPolicies = policies;
150     }
151
152     public void addFailedPolicy(PDPPolicy policy) {
153         this.failedPolicies.add(policy);
154     }
155
156     @Override
157     public boolean policiesOK() {
158         return this.failedPolicies.isEmpty();
159     }
160
161     @Override
162     public Set<PDPPIPConfig> getLoadedPipConfigs() {
163         return Collections.unmodifiableSet(this.loadedPIPConfigs);
164     }
165
166     public void setLoadedPipConfigs(Set<PDPPIPConfig> configs) {
167         this.loadedPIPConfigs = configs;
168     }
169
170     public void addLoadedPipConfig(PDPPIPConfig config) {
171         this.loadedPIPConfigs.add(config);
172     }
173
174     @Override
175     public Set<PDPPIPConfig> getFailedPipConfigs() {
176         return Collections.unmodifiableSet(this.failedPIPConfigs);
177     }
178
179     public void setFailedPipConfigs(Set<PDPPIPConfig> configs) {
180         this.failedPIPConfigs = configs;
181     }
182
183     public void addFailedPipConfig(PDPPIPConfig config) {
184         this.failedPIPConfigs.add(config);
185     }
186
187     @Override
188     public boolean pipConfigOK() {
189         return this.failedPIPConfigs.isEmpty();
190     }
191
192     @Override
193     public int hashCode() {
194         final int prime = 31;
195         int result = 1;
196         result = prime * result + (failedPIPConfigs.hashCode());
197         result = prime * result + (failedPolicies.hashCode());
198         result = prime * result + (loadErrors.hashCode());
199         result = prime * result + (loadWarnings.hashCode());
200         result = prime * result + (loadedPIPConfigs.hashCode());
201         result = prime * result + (loadedPolicies.hashCode());
202         result = prime * result + (status.hashCode());
203         return result;
204     }
205
206     @Override
207     public boolean equals(Object obj) {
208         if (this == obj) {
209             return true;
210         }
211         if (obj == null) {
212             return false;
213         }
214         if (getClass() != obj.getClass()) {
215             return false;
216         }
217         StdPDPStatus other = (StdPDPStatus) obj;
218         if (!failedPIPConfigs.equals(other.failedPIPConfigs)) {
219             return false;
220         }
221         if (!failedPolicies.equals(other.failedPolicies)) {
222             return false;
223         }
224         if (!loadErrors.equals(other.loadErrors)) {
225             return false;
226         }
227         if (!loadWarnings.equals(other.loadWarnings)) {
228             return false;
229         }
230         if (!loadedPIPConfigs.equals(other.loadedPIPConfigs)) {
231             return false;
232         }
233         if (!loadedPolicies.equals(other.loadedPolicies)) {
234             return false;
235         }
236         if (!loadedRootPolicies.equals(other.loadedRootPolicies)) {
237             return false;
238         }
239         if (status != other.status) {
240             return false;
241         }
242         return true;
243     }
244
245     @Override
246     @JsonIgnore
247     public boolean isOk() {
248         if (!this.policiesOK()) {
249             return false;
250         }
251         if (!this.pipConfigOK()) {
252             return false;
253         }
254         return this.status == Status.UP_TO_DATE;
255     }
256 }