2 * ============LICENSE_START=======================================================
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
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.openecomp.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<PIPResolverParam>();
108 public PIPResolver() {
111 public PIPResolver(String prefix, Properties properties, String user) throws PIPException {
112 this.createdBy = user;
113 this.modifiedBy = user;
115 this.readProperties(prefix, properties);
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));
130 public void prePersist() {
131 Date date = new Date();
132 this.createdDate = date;
133 this.modifiedDate = date;
137 public void preUpdate() {
138 this.modifiedDate = new Date();
145 public void setId(int id) {
149 public String getDescription() {
150 return this.description;
153 public void setDescription(String description) {
154 this.description = description;
157 public String getName() {
161 public void setName(String name) {
165 public String getIssuer() {
169 public void setIssuer(String issuer) {
170 this.issuer = issuer;
173 public String getClassname() {
177 public void setClassname(String classname) {
178 this.classname = classname;
181 public char getReadOnly() {
185 public void setReadOnly(char readOnly) {
186 this.readOnly = readOnly;
189 public String getCreatedBy() {
193 public void setCreatedBy(String createdBy) {
194 this.createdBy = createdBy;
197 public Date getCreatedDate() {
201 public void setCreatedDate(Date createdDate) {
202 this.createdDate = createdDate;
205 public String getModifiedBy() {
209 public void setModifiedBy(String modifiedBy) {
210 this.modifiedBy = modifiedBy;
213 public Date getModifiedDate() {
217 public void setModifiedDate(Date modifiedDate) {
218 this.modifiedDate = modifiedDate;
221 public PIPConfiguration getPipconfiguration() {
222 return this.pipconfiguration;
225 public void setPipconfiguration(PIPConfiguration pipconfiguration) {
226 this.pipconfiguration = pipconfiguration;
229 public Set<PIPResolverParam> getPipresolverParams() {
230 return this.pipresolverParams;
233 public void setPipresolverParams(Set<PIPResolverParam> pipresolverParams) {
234 this.pipresolverParams = pipresolverParams;
237 public PIPResolverParam addPipresolverParam(PIPResolverParam pipresolverParam) {
238 getPipresolverParams().add(pipresolverParam);
239 pipresolverParam.setPipresolver(this);
241 return pipresolverParam;
244 public PIPResolverParam removePipresolverParam(PIPResolverParam pipresolverParam) {
245 if (pipresolverParam == null) {
246 return pipresolverParam;
248 getPipresolverParams().remove(pipresolverParam);
249 pipresolverParam.setPipresolver(null);
251 return pipresolverParam;
255 public void clearParams() {
256 while (this.pipresolverParams.isEmpty() == false) {
257 this.removePipresolverParam(this.pipresolverParams.iterator().next());
262 public boolean isReadOnly() {
263 return (this.readOnly == '1');
267 public void setReadOnly(boolean readOnly) {
276 public static Collection<PIPResolver> importResolvers(String prefix, String list, Properties properties, String user) throws PIPException {
277 Collection<PIPResolver> resolvers = new ArrayList<PIPResolver>();
278 for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(list)) {
279 resolvers.add(new PIPResolver(prefix + "." + id, properties, user));
285 protected void readProperties(String prefix, Properties properties) throws PIPException {
287 // Get its classname, this MUST exist.
289 this.classname = properties.getProperty(prefix + ".classname");
290 if (this.classname == null) {
291 throw new PIPException("PIP Engine defined without a classname");
294 // Go through each property
296 for (Object name : properties.keySet()) {
297 if (name.toString().startsWith(prefix) == false) {
300 if (name.equals(prefix + ".classname")) {
302 // We already saved this
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());
311 this.addPipresolverParam(new PIPResolverParam(name.toString().substring(prefix.length() + 1),
312 properties.getProperty(name.toString())));
318 public Map<String, String> getConfiguration(String prefix) {
319 Map<String, String> map = new HashMap<String, String>();
320 if (prefix.endsWith(".") == false) {
321 prefix = prefix + ".";
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);
328 if (this.issuer != null && this.issuer.isEmpty() != false) {
329 map.put(prefix + "issuer", this.issuer);
331 for (PIPResolverParam param : this.pipresolverParams) {
332 map.put(prefix + param.getParamName(), param.getParamValue());
338 public void generateProperties(Properties props, String prefix) {
339 if (prefix.endsWith(".") == false) {
340 prefix = prefix + ".";
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);
347 if (this.issuer != null && this.issuer.isEmpty() != false) {
348 props.setProperty(prefix + "issuer", this.issuer);
350 for (PIPResolverParam param : this.pipresolverParams) {
351 props.setProperty(prefix + param.getParamName(), param.getParamValue());
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 + "]";