2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest.jpa;
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;
30 import java.util.Properties;
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;
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;
56 * The persistent class for the PIPResolver database table.
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;
66 @GeneratedValue(strategy=GenerationType.AUTO)
70 @Column(name="DESCRIPTION", nullable=true, length=2048)
71 private String description;
73 @Column(name="NAME", nullable=false, length=255)
76 @Column(name="ISSUER", nullable=true, length=1024)
77 private String issuer;
79 @Column(name="CLASSNAME", nullable=false, length=2048)
80 private String classname;
82 @Column(name="READ_ONLY", nullable=false)
83 private char readOnly = '0';
85 @Column(name="CREATED_BY", nullable=false, length=255)
86 private String createdBy = "guest";
88 @Temporal(TemporalType.TIMESTAMP)
89 @Column(name="CREATED_DATE", nullable=false, updatable=false)
90 private Date createdDate;
92 @Column(name="MODIFIED_BY", nullable=false, length=255)
93 private String modifiedBy = "guest";
95 @Temporal(TemporalType.TIMESTAMP)
96 @Column(name="MODIFIED_DATE", nullable=false)
97 private Date modifiedDate;
99 //bi-directional many-to-one association to PIPConfiguration
101 @JoinColumn(name="PIP_ID")
102 private PIPConfiguration pipconfiguration;
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<>();
108 public PIPResolver() {
109 //An empty constructor
112 public PIPResolver(String prefix, Properties properties, String user) throws PIPException {
113 this.createdBy = user;
114 this.modifiedBy = user;
116 this.readProperties(prefix, properties);
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));
131 public void prePersist() {
132 Date date = new Date();
133 this.createdDate = date;
134 this.modifiedDate = date;
138 public void preUpdate() {
139 this.modifiedDate = new Date();
146 public void setId(int id) {
150 public String getDescription() {
151 return this.description;
154 public void setDescription(String description) {
155 this.description = description;
158 public String getName() {
162 public void setName(String name) {
166 public String getIssuer() {
170 public void setIssuer(String issuer) {
171 this.issuer = issuer;
174 public String getClassname() {
178 public void setClassname(String classname) {
179 this.classname = classname;
182 public char getReadOnly() {
186 public void setReadOnly(char readOnly) {
187 this.readOnly = readOnly;
190 public String getCreatedBy() {
194 public void setCreatedBy(String createdBy) {
195 this.createdBy = createdBy;
198 public Date getCreatedDate() {
202 public void setCreatedDate(Date createdDate) {
203 this.createdDate = createdDate;
206 public String getModifiedBy() {
210 public void setModifiedBy(String modifiedBy) {
211 this.modifiedBy = modifiedBy;
214 public Date getModifiedDate() {
218 public void setModifiedDate(Date modifiedDate) {
219 this.modifiedDate = modifiedDate;
222 public PIPConfiguration getPipconfiguration() {
223 return this.pipconfiguration;
226 public void setPipconfiguration(PIPConfiguration pipconfiguration) {
227 this.pipconfiguration = pipconfiguration;
230 public Set<PIPResolverParam> getPipresolverParams() {
231 return this.pipresolverParams;
234 public void setPipresolverParams(Set<PIPResolverParam> pipresolverParams) {
235 this.pipresolverParams = pipresolverParams;
238 public PIPResolverParam addPipresolverParam(PIPResolverParam pipresolverParam) {
239 getPipresolverParams().add(pipresolverParam);
240 pipresolverParam.setPipresolver(this);
242 return pipresolverParam;
245 public PIPResolverParam removePipresolverParam(PIPResolverParam pipresolverParam) {
246 if (pipresolverParam == null) {
247 return pipresolverParam;
249 getPipresolverParams().remove(pipresolverParam);
250 pipresolverParam.setPipresolver(null);
252 return pipresolverParam;
256 public void clearParams() {
257 while (!this.pipresolverParams.isEmpty()) {
258 this.removePipresolverParam(this.pipresolverParams.iterator().next());
263 public boolean isReadOnly() {
264 return this.readOnly == '1';
268 public void setReadOnly(boolean readOnly) {
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));
286 protected void readProperties(String prefix, Properties properties) throws PIPException {
288 // Get its classname, this MUST exist.
290 this.classname = properties.getProperty(prefix + ".classname");
291 if (this.classname == null) {
292 throw new PIPException("PIP Engine defined without a classname");
295 // Go through each property
297 for (Object nme : properties.keySet()) {
298 if (!nme.toString().startsWith(prefix)) {
301 if (nme.equals(prefix + ".classname")) {
303 // We already saved this
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());
312 this.addPipresolverParam(new PIPResolverParam(nme.toString().substring(prefix.length() + 1),
313 properties.getProperty(nme.toString())));
319 public Map<String, String> getConfiguration(String prefix) {
320 String pref = prefix;
321 Map<String, String> map = new HashMap<>();
322 if (!prefix.endsWith(".")) {
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);
330 if (this.issuer != null && this.issuer.isEmpty()) {
331 map.put(pref + "issuer", this.issuer);
333 for (PIPResolverParam param : this.pipresolverParams) {
334 map.put(pref + param.getParamName(), param.getParamValue());
340 public void generateProperties(Properties props, String prefix) {
341 String pref = prefix;
342 if (!prefix.endsWith(".")) {
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);
350 if (this.issuer != null && this.issuer.isEmpty()) {
351 props.setProperty(pref + "issuer", this.issuer);
353 for (PIPResolverParam param : this.pipresolverParams) {
354 props.setProperty(pref + param.getParamName(), param.getParamValue());
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 + "]";