9a65d50b2896146c07e10f32fd487d9b5168d1b8
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / std / pap / StdPDPPIPConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import com.att.research.xacml.api.pap.PDPPIPConfig;
29 import com.fasterxml.jackson.annotation.JsonIgnore;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger; 
31 import org.onap.policy.common.logging.flexlogger.Logger;
32
33 public class StdPDPPIPConfig implements PDPPIPConfig, Serializable {
34         private static final long serialVersionUID = 1L;
35         private static Logger   logger  = FlexLogger.getLogger(StdPDPPIPConfig.class);
36         
37         private String id;
38         
39         private String name;
40         
41         private String description;
42         
43         private String classname;
44         
45         private Map<String,String> config = new HashMap<>();
46         
47         public StdPDPPIPConfig() {
48                 //
49                 // Default constructor
50                 //
51         }
52
53         public StdPDPPIPConfig(String id) {
54                 this.id = id;
55         }
56         
57         public StdPDPPIPConfig(String id, String name, String description) {
58                 this(id);
59                 this.name = name;
60                 this.description = description;
61         }
62
63         public StdPDPPIPConfig(String id, Properties properties) {
64                 this(id);
65                 if ( ! this.initialize(properties) ) {
66                         throw new IllegalArgumentException("PIP Engine '" + id + "' has no classname property in config");
67                 }
68         }
69         
70         public boolean initialize(Properties properties) {
71                 boolean classnameSeen = false;
72                 for (Object key : properties.keySet()) {
73                         if (key.toString().startsWith(this.id + ".")) {
74                                 if (logger.isDebugEnabled()) {
75                                         logger.debug("Found: " + key);
76                                 }
77                                 if (key.toString().equals(this.id + ".name")) {
78                                         this.name = properties.getProperty(key.toString());
79                                 } else if (key.toString().equals(this.id + ".description")) {
80                                         this.description = properties.getProperty(key.toString());
81                                 } else if (key.toString().equals(this.id + ".classname")) {
82                                         this.classname = properties.getProperty(key.toString());
83                                         classnameSeen = true;
84                                 }
85                                 // all properties, including the special ones located above, are included in the properties list
86                                 this.config.put(key.toString(), properties.getProperty(key.toString()));
87                         }
88                 }
89                 return classnameSeen;
90         }
91
92         @Override
93         public String getId() {
94                 return this.id;
95         }
96         
97         public void setId(String id) {
98                 this.id = id;
99         }
100
101         @Override
102         public String getName() {
103                 return name;
104         }
105
106         public void setName(String name) {
107                 this.name = name;
108         }
109
110         @Override
111         public String getDescription() {
112                 return this.description;
113         }
114         
115         public void setDescription(String description) {
116                 this.description = description;
117         }
118
119         @Override
120         public String getClassname() {
121                 return classname;
122         }
123
124         public void setClassname(String classname) {
125                 this.classname = classname;
126         }
127
128         @Override
129         @JsonIgnore
130         public Map<String,String> getConfiguration() {
131                 return Collections.unmodifiableMap(this.config);
132         }
133
134         public void setValues(Map<String,String> config) {
135                 this.config = config;
136         }
137
138         @Override
139         @JsonIgnore
140         public boolean isConfigured() {
141                 //
142                 // Also include this in the JSON I/O if it is a data field rather than calculated
143                 //
144                 return true;
145         }
146
147         @Override
148         public int hashCode() {
149                 final int prime = 31;
150                 int result = 1;
151                 result = prime * result
152                                 + ((classname == null) ? 0 : classname.hashCode());
153                 result = prime * result + ((config == null) ? 0 : config.hashCode());
154                 result = prime * result
155                                 + ((description == null) ? 0 : description.hashCode());
156                 result = prime * result + ((id == null) ? 0 : id.hashCode());
157                 result = prime * result + ((name == null) ? 0 : name.hashCode());
158                 return result;
159         }
160
161         @Override
162         public boolean equals(Object obj) {
163                 if (this == obj)
164                         return true;
165                 if (obj == null)
166                         return false;
167                 if (getClass() != obj.getClass())
168                         return false;
169                 StdPDPPIPConfig other = (StdPDPPIPConfig) obj;
170                 if (classname == null) {
171                         if (other.classname != null)
172                                 return false;
173                 } else if (!classname.equals(other.classname))
174                         return false;
175                 if (config == null) {
176                         if (other.config != null)
177                                 return false;
178                 } else if (!config.equals(other.config))
179                         return false;
180                 if (description == null) {
181                         if (other.description != null)
182                                 return false;
183                 } else if (!description.equals(other.description))
184                         return false;
185                 if (id == null) {
186                         if (other.id != null)
187                                 return false;
188                 } else if (!id.equals(other.id))
189                         return false;
190                 if (name == null) {
191                         if (other.name != null)
192                                 return false;
193                 } else if (!name.equals(other.name))
194                         return false;
195                 return true;
196         }
197
198         @Override
199         public String toString() {
200                 return "StdPDPPIPConfig [id=" + id + ", name=" + name
201                                 + ", description=" + description + ", classname=" + classname
202                                 + ", config=" + config + "]";
203         }
204
205         
206         
207         //
208         // Methods needed for JSON serialization/deserialization
209         //
210
211         public Map<String, String> getConfig() {
212                 return config;
213         }
214         public void setConfig(Map<String, String> config) {
215                 this.config = config;
216         }
217         
218 }