Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / std / pap / StdPDP.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
27 import java.io.Serializable;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import org.onap.policy.xacml.api.pap.OnapPDP;
34
35 public class StdPDP extends StdPDPItemSetChangeNotifier implements OnapPDP, Comparable<StdPDP>, Serializable {
36     private static final long serialVersionUID = 1L;
37
38     private String id;
39
40     private String name;
41
42     private String description;
43
44     private Integer jmxport = 0;
45
46     private transient PDPStatus status = new StdPDPStatus();
47
48     private transient Set<PDPPolicy> policies = new HashSet<>();
49
50     private transient Set<PDPPIPConfig> pipConfigs = new HashSet<>();
51
52     public StdPDP() {
53         //
54         // Default constructor
55         //
56     }
57
58     public StdPDP(String id, Integer jmxport) {
59         this(id, null, null, jmxport);
60     }
61
62     public StdPDP(String id, String name, Integer jmxport) {
63         this(id, name, null, jmxport);
64     }
65
66     /**
67      * StdPDP.
68      *
69      * @param id String
70      * @param name String
71      * @param description String
72      * @param jmxport Integer
73      */
74     public StdPDP(String id, String name, String description, Integer jmxport) {
75         this.id = id;
76         this.name = name;
77         this.description = description;
78         if (jmxport != null) {
79             this.jmxport = jmxport;
80         }
81     }
82
83     /**
84      * StdPDP.
85      *
86      * @param id String
87      * @param properties Properties
88      */
89     public StdPDP(String id, Properties properties) {
90         this(id, 0);
91
92         this.initialize(properties);
93     }
94
95     /**
96      * initialize.
97      *
98      * @param properties Properties
99      */
100     public void initialize(Properties properties) {
101         for (Object key : properties.keySet()) {
102             if (key.toString().startsWith(this.id + ".")) {
103                 if (key.toString().endsWith(".name")) {
104                     this.name = properties.getProperty(key.toString());
105                 } else if (key.toString().endsWith(".description")) {
106                     this.description = properties.getProperty(key.toString());
107                 } else if (key.toString().endsWith(".jmxport")) {
108                     if (properties.getProperty(key.toString()) != null
109                             && properties.getProperty(key.toString()).trim().length() > 0) {
110                         this.jmxport = Integer.valueOf(properties.getProperty(key.toString()));
111                     } else {
112                         this.jmxport = 0;
113                     }
114                 }
115             }
116         }
117     }
118
119     @Override
120     public String getId() {
121         return this.id;
122     }
123
124     public void setId(String id) {
125         this.id = id;
126     }
127
128     @Override
129     public String getName() {
130         return this.name;
131     }
132
133     @Override
134     public void setName(String name) {
135         this.name = name;
136         this.firePDPChanged(this);
137     }
138
139     @Override
140     public String getDescription() {
141         return this.description;
142     }
143
144     @Override
145     public void setDescription(String description) {
146         this.description = description;
147         this.firePDPChanged(this);
148     }
149
150     @Override
151     public PDPStatus getStatus() {
152         return this.status;
153     }
154
155     public void setStatus(PDPStatus status) {
156         this.status = status;
157     }
158
159     @Override
160     public Set<PDPPolicy> getPolicies() {
161         return Collections.unmodifiableSet(this.policies);
162     }
163
164     public void setPolicies(Set<PDPPolicy> policies) {
165         this.policies = policies;
166     }
167
168     @Override
169     public Set<PDPPIPConfig> getPipConfigs() {
170         return Collections.unmodifiableSet(this.pipConfigs);
171     }
172
173     public void setPipConfigs(Set<PDPPIPConfig> pipConfigs) {
174         this.pipConfigs = pipConfigs;
175     }
176
177     @Override
178     public void setJmxPort(Integer jmxport) {
179         this.jmxport = jmxport;
180     }
181
182     @Override
183     public Integer getJmxPort() {
184         return this.jmxport;
185     }
186
187     @Override
188     public int hashCode() {
189         final int prime = 31;
190         int result = 1;
191         result = prime * result + ((id == null) ? 0 : id.hashCode());
192         return result;
193     }
194
195     @Override
196     public boolean equals(Object obj) {
197         if (this == obj) {
198             return true;
199         }
200         if (obj == null) {
201             return false;
202         }
203         if (getClass() != obj.getClass()) {
204             return false;
205         }
206         StdPDP other = (StdPDP) obj;
207         if (id == null) {
208             if (other.id != null) {
209                 return false;
210             }
211         } else if (!id.equals(other.id)) {
212             return false;
213         }
214         return true;
215     }
216
217     @Override
218     public String toString() {
219         return "StdPDP [id=" + id + ", name=" + name + ", description=" + description + ", jmxport=" + jmxport
220                 + ", status=" + status + ", policies=" + policies + ", pipConfigs=" + pipConfigs + "]";
221     }
222
223     //
224     // Comparable interface
225     //
226     @Override
227     public int compareTo(StdPDP object) {
228         if (object == null) {
229             return -1;
230         }
231         if (object.name == null) {
232             return -1;
233         }
234         if (name == null) {
235             return 1;
236         }
237         return name.compareTo(object.name);
238     }
239
240 }