Fixes for sonar critical issues
[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         }
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         public StdPDP(String id, String name, String description, Integer jmxport) {
67                 this.id = id;
68                 this.name = name;
69                 this.description = description;
70                 if(jmxport != null){
71                         this.jmxport = jmxport;
72                 } 
73         }
74         
75         public StdPDP(String id, Properties properties) {
76                 this(id, 0);
77                 
78                 this.initialize(properties);
79         }
80         
81         public void initialize(Properties properties) {
82                 for (Object key : properties.keySet()) {
83                         if (key.toString().startsWith(this.id + ".")) {
84                                 if (logger.isDebugEnabled()) {
85                                         logger.debug("Found: " + key);
86                                 }
87                                 if (key.toString().endsWith(".name")) {
88                                         this.name = properties.getProperty(key.toString());
89                                 } else if (key.toString().endsWith(".description")) {
90                                         this.description = properties.getProperty(key.toString());
91                                 }else if (key.toString().endsWith(".jmxport")) {
92                                         if (properties.getProperty(key.toString()) != null && properties.getProperty(key.toString()).trim().length() > 0){
93                                                 logger.debug("initialize before: " + this.jmxport);
94                                                 this.jmxport = Integer.valueOf( properties.getProperty(key.toString()));
95                                                 logger.debug("initialize after: " + this.jmxport);
96                                         }else{
97                                                 this.jmxport = 0;
98                                         }
99                                 }
100                         }
101                 }
102         }
103
104         @Override
105         public String getId() {
106                 return this.id;
107         }
108
109         public void setId(String id) {
110                 this.id=id;
111         }
112
113         @Override
114         public String getName() {
115                 return this.name;
116         }
117         
118         @Override
119         public void setName(String name) {
120                 this.name = name;
121                 this.firePDPChanged(this);
122         }
123
124         @Override
125         public String getDescription() {
126                 return this.description;
127         }
128         
129         @Override
130         public void setDescription(String description) {
131                 this.description = description;
132                 this.firePDPChanged(this);
133         }
134
135         @Override
136         public PDPStatus getStatus() {
137                 return this.status;
138         }
139
140         public void setStatus(PDPStatus status) {
141                 this.status = status;
142         }
143         
144         @Override
145         public Set<PDPPolicy> getPolicies() {
146                 return Collections.unmodifiableSet(this.policies);
147         }
148         
149         public void setPolicies(Set<PDPPolicy> policies) {
150                 this.policies = policies;
151         }
152
153         @Override
154         public Set<PDPPIPConfig> getPipConfigs() {
155                 return Collections.unmodifiableSet(this.pipConfigs);
156         }
157         
158         public void setPipConfigs(Set<PDPPIPConfig> pipConfigs) {
159                 this.pipConfigs = pipConfigs;
160         }
161         public void setJmxPort(Integer jmxport) {
162                 this.jmxport = jmxport;
163         }
164         @Override
165         public Integer getJmxPort() {
166                 return this.jmxport;
167         }
168         
169         @Override
170         public int hashCode() {
171                 final int prime = 31;
172                 int result = 1;
173                 result = prime * result + ((id == null) ? 0 : id.hashCode());
174                 return result;
175         }
176
177         @Override
178         public boolean equals(Object obj) {
179                 if (this == obj)
180                         return true;
181                 if (obj == null)
182                         return false;
183                 if (getClass() != obj.getClass())
184                         return false;
185                 StdPDP other = (StdPDP) obj;
186                 if (id == null) {
187                         if (other.id != null)
188                                 return false;
189                 } else if (!id.equals(other.id))
190                         return false;
191                 return true;
192         }
193
194         @Override
195         public String toString() {
196                 return "StdPDP [id=" + id + ", name=" + name + ", description="
197                                 + description + ", jmxport=" + jmxport + ", status=" + status + ", policies=" + policies
198                                 + ", pipConfigs=" + pipConfigs + "]";
199         }
200         
201         //
202         // Comparable interface
203         //
204         @Override
205         public int compareTo(StdPDP o) {
206                 if (o == null) {
207                         return -1;
208                 }
209                 if ( ! (o instanceof StdPDP)) {
210                         return -1;
211                 }
212                 if (((StdPDP)o).name == null) {
213                         return -1;
214                 }
215                 if (name == null) {
216                         return 1;
217                 }
218                 return name.compareTo(((StdPDP)o).name);
219         }
220         
221 }