a8b02580d83bab21041e83260bb32c49df81040f
[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-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
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         //An empty constructor
110     }
111
112     public PIPResolver(String prefix, Properties properties, String user) throws PIPException {
113         this.createdBy = user;
114         this.modifiedBy = user;
115         this.readOnly = '0';
116         this.readProperties(prefix, properties);
117     }
118
119     public PIPResolver(PIPResolver resolver) {
120         this.name = resolver.name;
121         this.description = resolver.description;
122         this.issuer = resolver.issuer;
123         this.classname = resolver.classname;
124         this.readOnly = resolver.readOnly;
125         for (PIPResolverParam param : this.pipresolverParams) {
126             this.addPipresolverParam(new PIPResolverParam(param));
127         }
128     }
129
130     @PrePersist
131     public void prePersist() {
132         Date date = new Date();
133         this.createdDate = date;
134         this.modifiedDate = date;
135     }
136
137     @PreUpdate
138     public void preUpdate() {
139         this.modifiedDate = new Date();
140     }
141
142     public int getId() {
143         return this.id;
144     }
145
146     public void setId(int id) {
147         this.id = id;
148     }
149
150     public String getDescription() {
151         return this.description;
152     }
153
154     public void setDescription(String description) {
155         this.description = description;
156     }
157
158     public String getName() {
159         return this.name;
160     }
161
162     public void setName(String name) {
163         this.name = name;
164     }
165
166     public String getIssuer() {
167         return issuer;
168     }
169
170     public void setIssuer(String issuer) {
171         this.issuer = issuer;
172     }
173
174     public String getClassname() {
175         return classname;
176     }
177
178     public void setClassname(String classname) {
179         this.classname = classname;
180     }
181
182     public char getReadOnly() {
183         return readOnly;
184     }
185
186     public void setReadOnly(char readOnly) {
187         this.readOnly = readOnly;
188     }
189
190     public String getCreatedBy() {
191         return createdBy;
192     }
193
194     public void setCreatedBy(String createdBy) {
195         this.createdBy = createdBy;
196     }
197
198     public Date getCreatedDate() {
199         return createdDate;
200     }
201
202     public void setCreatedDate(Date createdDate) {
203         this.createdDate = createdDate;
204     }
205
206     public String getModifiedBy() {
207         return modifiedBy;
208     }
209
210     public void setModifiedBy(String modifiedBy) {
211         this.modifiedBy = modifiedBy;
212     }
213
214     public Date getModifiedDate() {
215         return modifiedDate;
216     }
217
218     public void setModifiedDate(Date modifiedDate) {
219         this.modifiedDate = modifiedDate;
220     }
221
222     public PIPConfiguration getPipconfiguration() {
223         return this.pipconfiguration;
224     }
225
226     public void setPipconfiguration(PIPConfiguration pipconfiguration) {
227         this.pipconfiguration = pipconfiguration;
228     }
229
230     public Set<PIPResolverParam> getPipresolverParams() {
231         return this.pipresolverParams;
232     }
233
234     public void setPipresolverParams(Set<PIPResolverParam> pipresolverParams) {
235         this.pipresolverParams = pipresolverParams;
236     }
237
238     public PIPResolverParam addPipresolverParam(PIPResolverParam pipresolverParam) {
239         getPipresolverParams().add(pipresolverParam);
240         pipresolverParam.setPipresolver(this);
241
242         return pipresolverParam;
243     }
244
245     public PIPResolverParam removePipresolverParam(PIPResolverParam pipresolverParam) {
246         if (pipresolverParam == null) {
247             return pipresolverParam;
248         }
249         getPipresolverParams().remove(pipresolverParam);
250         pipresolverParam.setPipresolver(null);
251
252         return pipresolverParam;
253     }
254
255     @Transient
256     public void clearParams() {
257         while (!this.pipresolverParams.isEmpty()) {
258             this.removePipresolverParam(this.pipresolverParams.iterator().next());
259         }
260     }
261
262     @Transient
263     public boolean isReadOnly() {
264         return this.readOnly == '1';
265     }
266
267     @Transient
268     public void setReadOnly(boolean readOnly) {
269         if (readOnly) {
270             this.readOnly = '1';
271         } else {
272             this.readOnly = '0';
273         }
274     }
275
276     @Transient
277     public static Collection<PIPResolver>       importResolvers(String prefix, String list, Properties properties, String user) throws PIPException {
278         Collection<PIPResolver> resolvers = new ArrayList<>();
279         for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(list)) {
280             resolvers.add(new PIPResolver(prefix + "." + id, properties, user));
281         }
282         return resolvers;
283     }
284
285     @Transient
286     protected void readProperties(String prefix, Properties properties) throws PIPException {
287         //
288         // Get its classname, this MUST exist.
289         //
290         this.classname = properties.getProperty(prefix + ".classname");
291         if (this.classname == null) {
292             throw new PIPException("PIP Engine defined without a classname");
293         }
294         //
295         // Go through each property
296         //
297         for (Object nme : properties.keySet()) {
298             if (!nme.toString().startsWith(prefix)) {
299                 continue;
300             }
301             if (nme.equals(prefix + ".classname")) {
302                 //
303                 // We already saved this
304                 //
305             } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_NAME)) {
306                 this.name = properties.getProperty(nme.toString());
307             } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_DESCRIPTION)) {
308                 this.description = properties.getProperty(nme.toString());
309             } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_ISSUER)) {
310                 this.issuer = properties.getProperty(nme.toString());
311             } else {
312                 this.addPipresolverParam(new PIPResolverParam(nme.toString().substring(prefix.length() + 1),
313                                                             properties.getProperty(nme.toString())));
314             }
315         }
316     }
317
318     @Transient
319     public Map<String, String> getConfiguration(String prefix) {
320         String pref = prefix;
321         Map<String, String> map = new HashMap<>();
322         if (!prefix.endsWith(".")) {
323             pref = prefix + ".";
324         }
325         map.put(pref + "classname", this.classname);
326         map.put(pref + "name", this.name);
327         if (this.description != null) {
328             map.put(pref + "description", this.description);
329         }
330         if (this.issuer != null && this.issuer.isEmpty()) {
331             map.put(pref + "issuer", this.issuer);
332         }
333         for (PIPResolverParam param : this.pipresolverParams) {
334             map.put(pref + param.getParamName(), param.getParamValue());
335         }
336         return map;
337     }
338
339     @Transient
340     public void generateProperties(Properties props, String prefix) {
341         String pref = prefix;
342         if (!prefix.endsWith(".")) {
343             pref = prefix + ".";
344         }
345         props.setProperty(pref + "classname", this.classname);
346         props.setProperty(pref + "name", this.name);
347         if (this.description != null) {
348             props.setProperty(pref + "description", this.description);
349         }
350         if (this.issuer != null && this.issuer.isEmpty()) {
351             props.setProperty(pref + "issuer", this.issuer);
352         }
353         for (PIPResolverParam param : this.pipresolverParams) {
354             props.setProperty(pref + param.getParamName(), param.getParamValue());
355         }
356     }
357
358     @Transient
359     @Override
360     public String toString() {
361         return "PIPResolver [id=" + id + ", classname=" + classname + ", name="
362                 + name + ", description=" + description + ", issuer=" + issuer
363                 + ", readOnly=" + readOnly + ", createdBy=" + createdBy
364                 + ", createdDate=" + createdDate + ", modifiedBy=" + modifiedBy
365                 + ", modifiedDate=" + modifiedDate + ", pipresolverParams="
366                 + pipresolverParams + "]";
367     }
368 }