JUnit/SONAR/Checkstyle in ONAP-REST
[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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.rest.jpa;
23
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;
27
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;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.Set;
37
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;
54
55 import lombok.Getter;
56 import lombok.NoArgsConstructor;
57 import lombok.Setter;
58
59 /**
60  * The persistent class for the PipResolver database table.
61  *
62  */
63 @Entity
64 @Table(name = "PipResolver")
65 @NamedQuery(name = "PipResolver.findAll", query = "SELECT p FROM PipResolver p")
66 @Getter
67 @Setter
68 @NoArgsConstructor
69 public class PipResolver implements Serializable {
70     private static final long serialVersionUID = 1L;
71
72     @Id
73     @GeneratedValue(strategy = GenerationType.AUTO)
74     @Column(name = "id")
75     private int id;
76
77     @Column(name = "DESCRIPTION", nullable = true, length = 2048)
78     private String description;
79
80     @Column(name = "NAME", nullable = false, length = 255)
81     private String name;
82
83     @Column(name = "ISSUER", nullable = true, length = 1024)
84     private String issuer;
85
86     @Column(name = "CLASSNAME", nullable = false, length = 2048)
87     private String classname;
88
89     @Column(name = "READ_ONLY", nullable = false)
90     private char readOnly = '0';
91
92     @Column(name = "CREATED_BY", nullable = false, length = 255)
93     private String createdBy = "guest";
94
95     @Temporal(TemporalType.TIMESTAMP)
96     @Column(name = "CREATED_DATE", nullable = false, updatable = false)
97     private Date createdDate;
98
99     @Column(name = "MODIFIED_BY", nullable = false, length = 255)
100     private String modifiedBy = "guest";
101
102     @Temporal(TemporalType.TIMESTAMP)
103     @Column(name = "MODIFIED_DATE", nullable = false)
104     private Date modifiedDate;
105
106     // bi-directional many-to-one association to PipConfiguration
107     @ManyToOne
108     @JoinColumn(name = "PIP_ID")
109     private PipConfiguration pipconfiguration;
110
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<>();
114
115     /**
116      * Instantiates a new PIP resolver.
117      *
118      * @param prefix the prefix
119      * @param properties the properties
120      * @param user the user
121      * @throws PIPException the PIP exception
122      */
123     public PipResolver(String prefix, Properties properties, String user) throws PIPException {
124         this.createdBy = user;
125         this.modifiedBy = user;
126         this.readOnly = '0';
127         this.readProperties(prefix, properties);
128     }
129
130     /**
131      * Instantiates a new PIP resolver.
132      *
133      * @param resolver the resolver
134      */
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));
143         }
144     }
145
146     /**
147      * Pre persist.
148      */
149     @PrePersist
150     public void prePersist() {
151         Date date = new Date();
152         this.createdDate = date;
153         this.modifiedDate = date;
154     }
155
156     /**
157      * Pre update.
158      */
159     @PreUpdate
160     public void preUpdate() {
161         this.modifiedDate = new Date();
162     }
163
164     /**
165      * Adds the pipresolver param.
166      *
167      * @param pipresolverParam the pipresolver param
168      * @return the PIP resolver param
169      */
170     public PipResolverParam addPipresolverParam(PipResolverParam pipresolverParam) {
171         getPipresolverParams().add(pipresolverParam);
172         pipresolverParam.setPipresolver(this);
173
174         return pipresolverParam;
175     }
176
177     /**
178      * Removes the pipresolver param.
179      *
180      * @param pipresolverParam the pipresolver param
181      * @return the PIP resolver param
182      */
183     public PipResolverParam removePipresolverParam(PipResolverParam pipresolverParam) {
184         if (pipresolverParam == null) {
185             return pipresolverParam;
186         }
187         getPipresolverParams().remove(pipresolverParam);
188         pipresolverParam.setPipresolver(null);
189
190         return pipresolverParam;
191     }
192
193     /**
194      * Clear params.
195      */
196     @Transient
197     public void clearParams() {
198         while (!this.pipresolverParams.isEmpty()) {
199             this.removePipresolverParam(this.pipresolverParams.iterator().next());
200         }
201     }
202
203     /**
204      * Checks if is read only.
205      *
206      * @return true, if is read only
207      */
208     @Transient
209     public boolean isReadOnly() {
210         return this.readOnly == '1';
211     }
212
213     /**
214      * Sets the read only.
215      *
216      * @param readOnly the new read only
217      */
218     @Transient
219     public void setReadOnly(boolean readOnly) {
220         if (readOnly) {
221             this.readOnly = '1';
222         } else {
223             this.readOnly = '0';
224         }
225     }
226
227     /**
228      * Import resolvers.
229      *
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
236      */
237     @Transient
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));
243         }
244         return resolvers;
245     }
246
247     /**
248      * Read properties.
249      *
250      * @param prefix the prefix
251      * @param properties the properties
252      * @throws PIPException the PIP exception
253      */
254     @Transient
255     protected void readProperties(String prefix, Properties properties) throws PIPException {
256         //
257         // Get its classname, this MUST exist.
258         //
259         this.classname = properties.getProperty(prefix + ".classname");
260         if (this.classname == null) {
261             throw new PIPException("PIP Engine defined without a classname");
262         }
263         //
264         // Go through each property
265         //
266         for (Object nme : properties.keySet()) {
267             if (!nme.toString().startsWith(prefix)) {
268                 continue;
269             }
270             if (nme.equals(prefix + ".classname")) {
271                 //
272                 // We already saved this
273                 //
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());
280             } else {
281                 this.addPipresolverParam(new PipResolverParam(nme.toString().substring(prefix.length() + 1),
282                                 properties.getProperty(nme.toString())));
283             }
284         }
285     }
286
287     /**
288      * Gets the configuration.
289      *
290      * @param prefix the prefix
291      * @return the configuration
292      */
293     @Transient
294     public Map<String, String> getConfiguration(String prefix) {
295         String pref = prefix;
296         Map<String, String> map = new HashMap<>();
297         if (!prefix.endsWith(".")) {
298             pref = prefix + ".";
299         }
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);
304         }
305         if (this.issuer != null && this.issuer.isEmpty()) {
306             map.put(pref + "issuer", this.issuer);
307         }
308         for (PipResolverParam param : this.pipresolverParams) {
309             map.put(pref + param.getParamName(), param.getParamValue());
310         }
311         return map;
312     }
313
314     /**
315      * Generate properties.
316      *
317      * @param props the props
318      * @param prefix the prefix
319      */
320     @Transient
321     public void generateProperties(Properties props, String prefix) {
322         String pref = prefix;
323         if (!prefix.endsWith(".")) {
324             pref = prefix + ".";
325         }
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);
330         }
331         if (this.issuer != null && this.issuer.isEmpty()) {
332             props.setProperty(pref + "issuer", this.issuer);
333         }
334         for (PipResolverParam param : this.pipresolverParams) {
335             props.setProperty(pref + param.getParamName(), param.getParamValue());
336         }
337     }
338
339     /**
340      * To string.
341      *
342      * @return the string
343      */
344     @Transient
345     @Override
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 + "]";
351     }
352 }