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