2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.jpa;
24 import com.att.research.xacml.api.pip.PIPException;
25 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
26 import com.google.common.base.Splitter;
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.HashSet;
35 import java.util.Properties;
38 import javax.persistence.CascadeType;
39 import javax.persistence.Column;
40 import javax.persistence.Entity;
41 import javax.persistence.GeneratedValue;
42 import javax.persistence.GenerationType;
43 import javax.persistence.Id;
44 import javax.persistence.JoinColumn;
45 import javax.persistence.ManyToOne;
46 import javax.persistence.NamedQuery;
47 import javax.persistence.OneToMany;
48 import javax.persistence.PrePersist;
49 import javax.persistence.PreUpdate;
50 import javax.persistence.Table;
51 import javax.persistence.Temporal;
52 import javax.persistence.TemporalType;
53 import javax.persistence.Transient;
56 import lombok.NoArgsConstructor;
60 * The persistent class for the PipResolver database table.
64 @Table(name = "PipResolver")
65 @NamedQuery(name = "PipResolver.findAll", query = "SELECT p FROM PipResolver p")
69 public class PipResolver implements Serializable {
70 private static final long serialVersionUID = 1L;
73 @GeneratedValue(strategy = GenerationType.AUTO)
77 @Column(name = "DESCRIPTION", nullable = true, length = 2048)
78 private String description;
80 @Column(name = "NAME", nullable = false, length = 255)
83 @Column(name = "ISSUER", nullable = true, length = 1024)
84 private String issuer;
86 @Column(name = "CLASSNAME", nullable = false, length = 2048)
87 private String classname;
89 @Column(name = "READ_ONLY", nullable = false)
90 private char readOnly = '0';
92 @Column(name = "CREATED_BY", nullable = false, length = 255)
93 private String createdBy = "guest";
95 @Temporal(TemporalType.TIMESTAMP)
96 @Column(name = "CREATED_DATE", nullable = false, updatable = false)
97 private Date createdDate;
99 @Column(name = "MODIFIED_BY", nullable = false, length = 255)
100 private String modifiedBy = "guest";
102 @Temporal(TemporalType.TIMESTAMP)
103 @Column(name = "MODIFIED_DATE", nullable = false)
104 private Date modifiedDate;
106 // bi-directional many-to-one association to PipConfiguration
108 @JoinColumn(name = "PIP_ID")
109 private PipConfiguration pipconfiguration;
111 // bi-directional many-to-one association to PipResolverParam
112 @OneToMany(mappedBy = "pipresolver", orphanRemoval = true, cascade = CascadeType.REMOVE)
113 private Set<PipResolverParam> pipresolverParams = new HashSet<>();
116 * Instantiates a new PIP resolver.
118 * @param prefix the prefix
119 * @param properties the properties
120 * @param user the user
121 * @throws PIPException the PIP exception
123 public PipResolver(String prefix, Properties properties, String user) throws PIPException {
124 this.createdBy = user;
125 this.modifiedBy = user;
127 this.readProperties(prefix, properties);
131 * Instantiates a new PIP resolver.
133 * @param resolver the resolver
135 public PipResolver(PipResolver resolver) {
136 this.name = resolver.name;
137 this.description = resolver.description;
138 this.issuer = resolver.issuer;
139 this.classname = resolver.classname;
140 this.readOnly = resolver.readOnly;
141 for (PipResolverParam param : resolver.pipresolverParams) {
142 this.addPipresolverParam(new PipResolverParam(param));
150 public void prePersist() {
151 Date date = new Date();
152 this.createdDate = date;
153 this.modifiedDate = date;
160 public void preUpdate() {
161 this.modifiedDate = new Date();
165 * Adds the pipresolver param.
167 * @param pipresolverParam the pipresolver param
168 * @return the PIP resolver param
170 public PipResolverParam addPipresolverParam(PipResolverParam pipresolverParam) {
171 getPipresolverParams().add(pipresolverParam);
172 pipresolverParam.setPipresolver(this);
174 return pipresolverParam;
178 * Removes the pipresolver param.
180 * @param pipresolverParam the pipresolver param
181 * @return the PIP resolver param
183 public PipResolverParam removePipresolverParam(PipResolverParam pipresolverParam) {
184 if (pipresolverParam == null) {
185 return pipresolverParam;
187 getPipresolverParams().remove(pipresolverParam);
188 pipresolverParam.setPipresolver(null);
190 return pipresolverParam;
197 public void clearParams() {
198 while (!this.pipresolverParams.isEmpty()) {
199 this.removePipresolverParam(this.pipresolverParams.iterator().next());
204 * Checks if is read only.
206 * @return true, if is read only
209 public boolean isReadOnly() {
210 return this.readOnly == '1';
214 * Sets the read only.
216 * @param readOnly the new read only
219 public void setReadOnly(boolean readOnly) {
230 * @param prefix the prefix
231 * @param list the list
232 * @param properties the properties
233 * @param user the user
234 * @return the collection
235 * @throws PIPException the PIP exception
238 public static Collection<PipResolver> importResolvers(String prefix, String list, Properties properties,
239 String user) throws PIPException {
240 Collection<PipResolver> resolvers = new ArrayList<>();
241 for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(list)) {
242 resolvers.add(new PipResolver(prefix + "." + id, properties, user));
250 * @param prefix the prefix
251 * @param properties the properties
252 * @throws PIPException the PIP exception
255 protected void readProperties(String prefix, Properties properties) throws PIPException {
257 // Get its classname, this MUST exist.
259 this.classname = properties.getProperty(prefix + ".classname");
260 if (this.classname == null) {
261 throw new PIPException("PIP Engine defined without a classname");
264 // Go through each property
266 for (Object nme : properties.keySet()) {
267 if (!nme.toString().startsWith(prefix)) {
270 if (nme.equals(prefix + ".classname")) {
272 // We already saved this
274 } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_NAME)) {
275 this.name = properties.getProperty(nme.toString());
276 } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_DESCRIPTION)) {
277 this.description = properties.getProperty(nme.toString());
278 } else if (nme.equals(prefix + "." + StdConfigurableEngine.PROP_ISSUER)) {
279 this.issuer = properties.getProperty(nme.toString());
281 this.addPipresolverParam(new PipResolverParam(nme.toString().substring(prefix.length() + 1),
282 properties.getProperty(nme.toString())));
288 * Gets the configuration.
290 * @param prefix the prefix
291 * @return the configuration
294 public Map<String, String> getConfiguration(String prefix) {
295 String pref = prefix;
296 Map<String, String> map = new HashMap<>();
297 if (!prefix.endsWith(".")) {
300 map.put(pref + "classname", this.classname);
301 map.put(pref + "name", this.name);
302 if (this.description != null) {
303 map.put(pref + "description", this.description);
305 if (this.issuer != null && this.issuer.isEmpty()) {
306 map.put(pref + "issuer", this.issuer);
308 for (PipResolverParam param : this.pipresolverParams) {
309 map.put(pref + param.getParamName(), param.getParamValue());
315 * Generate properties.
317 * @param props the props
318 * @param prefix the prefix
321 public void generateProperties(Properties props, String prefix) {
322 String pref = prefix;
323 if (!prefix.endsWith(".")) {
326 props.setProperty(pref + "classname", this.classname);
327 props.setProperty(pref + "name", this.name);
328 if (this.description != null) {
329 props.setProperty(pref + "description", this.description);
331 if (this.issuer != null && this.issuer.isEmpty()) {
332 props.setProperty(pref + "issuer", this.issuer);
334 for (PipResolverParam param : this.pipresolverParams) {
335 props.setProperty(pref + param.getParamName(), param.getParamValue());
346 public String toString() {
347 return "PipResolver [id=" + id + ", classname=" + classname + ", name=" + name + ", description=" + description
348 + ", issuer=" + issuer + ", readOnly=" + readOnly + ", createdBy=" + createdBy
349 + ", createdDate=" + createdDate + ", modifiedBy=" + modifiedBy + ", modifiedDate="
350 + modifiedDate + ", pipresolverParams=" + pipresolverParams + "]";