daab6d6589e386857ee3840012e80388991990fc
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / PIPResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
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
21 package org.onap.policy.rest.jpa;
22
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import javax.persistence.CascadeType;
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.GeneratedValue;
37 import javax.persistence.GenerationType;
38 import javax.persistence.Id;
39 import javax.persistence.JoinColumn;
40 import javax.persistence.ManyToOne;
41 import javax.persistence.NamedQuery;
42 import javax.persistence.OneToMany;
43 import javax.persistence.PrePersist;
44 import javax.persistence.PreUpdate;
45 import javax.persistence.Table;
46 import javax.persistence.Temporal;
47 import javax.persistence.TemporalType;
48 import javax.persistence.Transient;
49
50 import com.att.research.xacml.api.pip.PIPException;
51 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
52 import com.google.common.base.Splitter;
53
54
55 /**
56  * The persistent class for the PIPResolver database table.
57  * 
58  */
59 @Entity
60 @Table(name="PIPResolver")
61 @NamedQuery(name="PIPResolver.findAll", query="SELECT p FROM PIPResolver p")
62 public class PIPResolver implements Serializable {
63         private static final long serialVersionUID = 1L;
64
65         @Id
66         @GeneratedValue(strategy=GenerationType.AUTO)
67         @Column(name="id")
68         private int id;
69
70         @Column(name="DESCRIPTION", nullable=true, length=2048)
71         private String description;
72
73         @Column(name="NAME", nullable=false, length=255)
74         private String name;
75
76         @Column(name="ISSUER", nullable=true, length=1024)
77         private String issuer;
78
79         @Column(name="CLASSNAME", nullable=false, length=2048)
80         private String classname;
81
82         @Column(name="READ_ONLY", nullable=false)
83         private char readOnly = '0';
84
85         @Column(name="CREATED_BY", nullable=false, length=255)
86         private String createdBy = "guest";
87
88         @Temporal(TemporalType.TIMESTAMP)
89         @Column(name="CREATED_DATE", nullable=false, updatable=false)
90         private Date createdDate;
91
92         @Column(name="MODIFIED_BY", nullable=false, length=255)
93         private String modifiedBy = "guest";
94
95         @Temporal(TemporalType.TIMESTAMP)
96         @Column(name="MODIFIED_DATE", nullable=false)
97         private Date modifiedDate;
98
99         //bi-directional many-to-one association to PIPConfiguration
100         @ManyToOne
101         @JoinColumn(name="PIP_ID")
102         private PIPConfiguration pipconfiguration;
103
104         //bi-directional many-to-one association to PIPResolverParam
105         @OneToMany(mappedBy="pipresolver", orphanRemoval=true, cascade=CascadeType.REMOVE)
106         private Set<PIPResolverParam> pipresolverParams = new HashSet<>();
107
108         public PIPResolver() {
109         }
110         
111         public PIPResolver(String prefix, Properties properties, String user) throws PIPException {
112                 this.createdBy = user;
113                 this.modifiedBy = user;
114                 this.readOnly = '0';
115                 this.readProperties(prefix, properties);
116         }
117         
118         public PIPResolver(PIPResolver resolver) {
119                 this.name = resolver.name;
120                 this.description = resolver.description;
121                 this.issuer = resolver.issuer;
122                 this.classname = resolver.classname;
123                 this.readOnly = resolver.readOnly;
124                 for (PIPResolverParam param : this.pipresolverParams) {
125                         this.addPipresolverParam(new PIPResolverParam(param));
126                 }
127         }
128
129         @PrePersist
130         public void     prePersist() {
131                 Date date = new Date();
132                 this.createdDate = date;
133                 this.modifiedDate = date;
134         }
135         
136         @PreUpdate
137         public void preUpdate() {
138                 this.modifiedDate = new Date();
139         }
140
141         public int getId() {
142                 return this.id;
143         }
144
145         public void setId(int id) {
146                 this.id = id;
147         }
148
149         public String getDescription() {
150                 return this.description;
151         }
152
153         public void setDescription(String description) {
154                 this.description = description;
155         }
156
157         public String getName() {
158                 return this.name;
159         }
160
161         public void setName(String name) {
162                 this.name = name;
163         }
164
165         public String getIssuer() {
166                 return issuer;
167         }
168
169         public void setIssuer(String issuer) {
170                 this.issuer = issuer;
171         }
172
173         public String getClassname() {
174                 return classname;
175         }
176
177         public void setClassname(String classname) {
178                 this.classname = classname;
179         }
180
181         public char getReadOnly() {
182                 return readOnly;
183         }
184
185         public void setReadOnly(char readOnly) {
186                 this.readOnly = readOnly;
187         }
188
189         public String getCreatedBy() {
190                 return createdBy;
191         }
192
193         public void setCreatedBy(String createdBy) {
194                 this.createdBy = createdBy;
195         }
196
197         public Date getCreatedDate() {
198                 return createdDate;
199         }
200
201         public void setCreatedDate(Date createdDate) {
202                 this.createdDate = createdDate;
203         }
204
205         public String getModifiedBy() {
206                 return modifiedBy;
207         }
208
209         public void setModifiedBy(String modifiedBy) {
210                 this.modifiedBy = modifiedBy;
211         }
212
213         public Date getModifiedDate() {
214                 return modifiedDate;
215         }
216
217         public void setModifiedDate(Date modifiedDate) {
218                 this.modifiedDate = modifiedDate;
219         }
220
221         public PIPConfiguration getPipconfiguration() {
222                 return this.pipconfiguration;
223         }
224
225         public void setPipconfiguration(PIPConfiguration pipconfiguration) {
226                 this.pipconfiguration = pipconfiguration;
227         }
228
229         public Set<PIPResolverParam> getPipresolverParams() {
230                 return this.pipresolverParams;
231         }
232
233         public void setPipresolverParams(Set<PIPResolverParam> pipresolverParams) {
234                 this.pipresolverParams = pipresolverParams;
235         }
236
237         public PIPResolverParam addPipresolverParam(PIPResolverParam pipresolverParam) {
238                 getPipresolverParams().add(pipresolverParam);
239                 pipresolverParam.setPipresolver(this);
240
241                 return pipresolverParam;
242         }
243
244         public PIPResolverParam removePipresolverParam(PIPResolverParam pipresolverParam) {
245                 if (pipresolverParam == null) {
246                         return pipresolverParam;
247                 }
248                 getPipresolverParams().remove(pipresolverParam);
249                 pipresolverParam.setPipresolver(null);
250
251                 return pipresolverParam;
252         }
253         
254         @Transient
255         public void clearParams() {
256                 while (this.pipresolverParams.isEmpty() == false) {
257                         this.removePipresolverParam(this.pipresolverParams.iterator().next());
258                 }
259         }
260
261         @Transient
262         public boolean isReadOnly() {
263                 return (this.readOnly == '1');
264         }
265         
266         @Transient
267         public void setReadOnly(boolean readOnly) {
268                 if (readOnly) {
269                         this.readOnly = '1';
270                 } else {
271                         this.readOnly = '0';
272                 }
273         }
274         
275         @Transient
276         public static Collection<PIPResolver>   importResolvers(String prefix, String list, Properties properties, String user) throws PIPException {
277                 Collection<PIPResolver> resolvers = new ArrayList<>();
278                 for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(list)) {
279                         resolvers.add(new PIPResolver(prefix + "." + id, properties, user));
280                 }               
281                 return resolvers;
282         }
283
284         @Transient
285         protected void readProperties(String prefix, Properties properties) throws PIPException {
286                 //
287                 // Get its classname, this MUST exist.
288                 //
289                 this.classname = properties.getProperty(prefix + ".classname");
290                 if (this.classname == null) {
291                         throw new PIPException("PIP Engine defined without a classname");
292                 }
293                 //
294                 // Go through each property
295                 //
296                 for (Object name : properties.keySet()) {
297                         if (name.toString().startsWith(prefix) == false) {
298                                 continue;
299                         }
300                         if (name.equals(prefix + ".classname")) {
301                                 //
302                                 // We already saved this
303                                 //
304                         } else if (name.equals(prefix + "." + StdConfigurableEngine.PROP_NAME)) {
305                                 this.name = properties.getProperty(name.toString());
306                         } else if (name.equals(prefix + "." + StdConfigurableEngine.PROP_DESCRIPTION)) {
307                                 this.description = properties.getProperty(name.toString());
308                         } else if (name.equals(prefix + "." + StdConfigurableEngine.PROP_ISSUER)) {
309                                 this.issuer = properties.getProperty(name.toString());
310                         } else {
311                                 this.addPipresolverParam(new PIPResolverParam(name.toString().substring(prefix.length() + 1),
312                                                                                                                         properties.getProperty(name.toString())));
313                         }
314                 }
315         }
316
317         @Transient
318         public Map<String, String> getConfiguration(String prefix) {
319                 Map<String, String> map = new HashMap<>();
320                 if (prefix.endsWith(".") == false) {
321                         prefix = prefix + ".";
322                 }
323                 map.put(prefix + "classname", this.classname);
324                 map.put(prefix + "name", this.name);
325                 if (this.description != null) {
326                         map.put(prefix + "description", this.description);
327                 }
328                 if (this.issuer != null && this.issuer.isEmpty() != false) {
329                         map.put(prefix + "issuer", this.issuer);
330                 }
331                 for (PIPResolverParam param : this.pipresolverParams) {
332                         map.put(prefix + param.getParamName(), param.getParamValue());
333                 }
334                 return map;
335         }
336
337         @Transient
338         public void     generateProperties(Properties props, String prefix) {
339                 if (prefix.endsWith(".") == false) {
340                         prefix = prefix + ".";
341                 }
342                 props.setProperty(prefix + "classname", this.classname);
343                 props.setProperty(prefix + "name", this.name);
344                 if (this.description != null) {
345                         props.setProperty(prefix + "description", this.description);
346                 }
347                 if (this.issuer != null && this.issuer.isEmpty() != false) {
348                         props.setProperty(prefix + "issuer", this.issuer);
349                 }
350                 for (PIPResolverParam param : this.pipresolverParams) {
351                         props.setProperty(prefix + param.getParamName(), param.getParamValue());
352                 }
353         }
354
355         @Transient
356         @Override
357         public String toString() {
358                 return "PIPResolver [id=" + id + ", classname=" + classname + ", name="
359                                 + name + ", description=" + description + ", issuer=" + issuer
360                                 + ", readOnly=" + readOnly + ", createdBy=" + createdBy
361                                 + ", createdDate=" + createdDate + ", modifiedBy=" + modifiedBy
362                                 + ", modifiedDate=" + modifiedDate + ", pipresolverParams="
363                                 + pipresolverParams + "]";
364         }
365 }