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