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