aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/PipConfiguration.java
blob: 99bd613e8576b326600b57bfd4fc57415a87b6de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*-
 * ============LICENSE_START=======================================================
 * ONAP-REST
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.rest.jpa;

import com.att.research.xacml.api.pip.PIPException;
import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
import com.att.research.xacml.util.XACMLProperties;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.onap.policy.common.logging.eelf.MessageCodes;
import org.onap.policy.common.logging.eelf.PolicyLogger;
import org.onap.policy.xacml.api.XACMLErrorConstants;

/**
 * The persistent class for the PipConfiguration database table.
 *
 */
@Entity
@Table(name = "PipConfiguration")
@NamedQuery(name = "PipConfiguration.findAll", query = "SELECT p FROM PipConfiguration p")
@Getter
@Setter
@NoArgsConstructor
@ToString
public class PipConfiguration implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final Log logger = LogFactory.getLog(PipConfiguration.class);

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "DESCRIPTION", nullable = true, length = 2048)
    private String description;

    @Column(name = "NAME", nullable = false, length = 255)
    private String name;

    @Column(name = "CLASSNAME", nullable = false, length = 2048)
    private String classname;

    @Column(name = "ISSUER", nullable = true, length = 1024)
    private String issuer;

    @Column(name = "READ_ONLY", nullable = false)
    private char readOnly = '0';

    @Column(name = "REQUIRES_RESOLVER", nullable = false)
    private char requiresResolvers;

    @Column(name = "CREATED_BY", nullable = false, length = 255)
    private String createdBy = "guest";

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_DATE", nullable = false, updatable = false)
    private Date createdDate;

    @Column(name = "MODIFIED_BY", nullable = false, length = 255)
    private String modifiedBy = "guest";

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFIED_DATE", nullable = false)
    private Date modifiedDate;

    // bi-directional many-to-one association to PipConfigParam
    @OneToMany(mappedBy = "pipconfiguration", orphanRemoval = true, cascade = CascadeType.REMOVE)
    private Set<PipConfigParam> pipconfigParams = new HashSet<>();

    // bi-directional many-to-one association to PipType
    @ManyToOne
    @JoinColumn(name = "TYPE")
    private PipType piptype;

    // bi-directional many-to-one association to PipResolver
    @OneToMany(mappedBy = "pipconfiguration", orphanRemoval = true, cascade = CascadeType.REMOVE)
    private Set<PipResolver> pipresolvers = new HashSet<>();

    /**
     * Instantiates a new PIP configuration.
     *
     * @param config the config
     * @param user the user
     */
    public PipConfiguration(PipConfiguration config, String user) {
        this.description = config.description;
        this.name = config.name;
        this.classname = config.classname;
        this.issuer = config.issuer;
        this.requiresResolvers = config.requiresResolvers;
        this.readOnly = config.readOnly;
        this.piptype = config.piptype;
        for (PipConfigParam param : config.pipconfigParams) {
            this.addPipconfigParam(new PipConfigParam(param));
        }
        for (PipResolver resolver : config.pipresolvers) {
            this.addPipresolver(new PipResolver(resolver));
        }
    }

    /**
     * Instantiates a new PIP configuration.
     *
     * @param id the id
     * @param properties the properties
     * @throws PIPException the PIP exception
     */
    public PipConfiguration(String id, Properties properties) throws PIPException {
        this.readProperties(id, properties);
    }

    /**
     * Instantiates a new PIP configuration.
     *
     * @param id the id
     * @param properties the properties
     * @param user the user
     * @throws PIPException the PIP exception
     */
    public PipConfiguration(String id, Properties properties, String user) throws PIPException {
        this.createdBy = user;
        this.modifiedBy = user;
        this.readProperties(id, properties);
    }

    /**
     * Pre persist.
     */
    @PrePersist
    public void prePersist() {
        Date date = new Date();
        this.createdDate = date;
        this.modifiedDate = date;
    }

    /**
     * Pre update.
     */
    @PreUpdate
    public void preUpdate() {
        this.modifiedDate = new Date();
    }

    /**
     * Adds the pipconfig param.
     *
     * @param pipconfigParam the pipconfig param
     * @return the PIP config param
     */
    public PipConfigParam addPipconfigParam(PipConfigParam pipconfigParam) {
        getPipconfigParams().add(pipconfigParam);
        pipconfigParam.setPipconfiguration(this);

        return pipconfigParam;
    }

    /**
     * Removes the pipconfig param.
     *
     * @param pipconfigParam the pipconfig param
     * @return the PIP config param
     */
    public PipConfigParam removePipconfigParam(PipConfigParam pipconfigParam) {
        if (pipconfigParam == null) {
            return pipconfigParam;
        }
        getPipconfigParams().remove(pipconfigParam);
        pipconfigParam.setPipconfiguration(null);

        return pipconfigParam;
    }

    /**
     * Clear config params.
     */
    @Transient
    public void clearConfigParams() {
        while (!this.pipconfigParams.isEmpty()) {
            this.removePipconfigParam(this.pipconfigParams.iterator().next());
        }
    }

    /**
     * Adds the pipresolver.
     *
     * @param pipresolver the pipresolver
     * @return the PIP resolver
     */
    public PipResolver addPipresolver(PipResolver pipresolver) {
        getPipresolvers().add(pipresolver);
        pipresolver.setPipconfiguration(this);

        return pipresolver;
    }

    /**
     * Removes the pipresolver.
     *
     * @param pipresolver the pipresolver
     * @return the PIP resolver
     */
    public PipResolver removePipresolver(PipResolver pipresolver) {
        getPipresolvers().remove(pipresolver);
        pipresolver.setPipconfiguration(null);

        return pipresolver;
    }

    /**
     * Checks if is read only.
     *
     * @return true, if is read only
     */
    @Transient
    public boolean isReadOnly() {
        return this.readOnly == '1';
    }

    /**
     * Sets the read only flag.
     *
     * @param readOnly the new read only flag
     */
    @Transient
    public void setReadOnlyFlag(boolean readOnly) {
        if (readOnly) {
            this.readOnly = '1';
        } else {
            this.readOnly = '0';
        }
    }

    /**
     * Requires resolvers.
     *
     * @return true, if successful
     */
    @Transient
    public boolean requiresResolvers() {
        return this.requiresResolvers == '1';
    }

    /**
     * Sets the requires resolvers flag.
     *
     * @param requires the new requires resolvers flag
     */
    @Transient
    public void setRequiresResolversFlag(boolean requires) {
        if (requires) {
            this.requiresResolvers = '1';
        } else {
            this.requiresResolvers = '0';
        }
    }

    /**
     * Import PIP configurations.
     *
     * @param properties the properties
     * @return the collection
     */
    @Transient
    public static Collection<PipConfiguration> importPipConfigurations(Properties properties) {
        Collection<PipConfiguration> configurations = new ArrayList<>();
        String engines = properties.getProperty(XACMLProperties.PROP_PIP_ENGINES);
        if (engines == null || engines.isEmpty()) {
            return configurations;
        }
        for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(engines)) {
            PipConfiguration configuration;
            try {
                String user = "super-admin";
                configuration = new PipConfiguration(id, properties, user);
                configuration.setCreatedBy(user);
                configuration.setModifiedBy(user);
                configurations.add(configuration);
            } catch (PIPException e) {
                logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Import failed: " + e.getLocalizedMessage());
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PipConfiguration", "Import failed");
            }
        }

        return configurations;
    }

    /**
     * Read properties.
     *
     * @param id the id
     * @param properties the properties
     * @throws PIPException the PIP exception
     */
    @Transient
    protected void readProperties(String id, Properties properties) throws PIPException {
        //
        // Save the id if we don't have one already
        //

        if (this.id == 0) {
            try {
                this.id = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Convert id to integer failed: " + id);
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PipConfiguration", "Convert id to integer failed");
            }
        }
        //
        // Get its classname, this MUST exist.
        //
        this.classname = properties.getProperty(id + ".classname");
        if (this.classname == null) {
            throw new PIPException("PIP Engine defined without a classname");
        }
        //
        // Go through each property
        //
        for (Object propertyKey : properties.keySet()) {
            readProperty(id, properties, propertyKey);
        }
        //
        // Make sure we have a name at least
        //
        if (this.name == null) {
            this.name = id;
        }
    }

    /**
     * Read a property into the PIP configuration.
     *
     * @param id the ID of the property
     * @param properties the properties object to read from
     * @param key the key of the property being checked
     * @throws PIPException on exceptions thrown on reading the property
     */
    private void readProperty(String id, Properties properties, Object key) throws PIPException {
        if (!key.toString().startsWith(id)) {
            return;
        }
        if (key.equals(id + ".classname")) {
            //
            // We already saved this
            //
        } else if (key.equals(id + "." + StdConfigurableEngine.PROP_NAME)) {
            this.name = properties.getProperty(key.toString());
        } else if (key.equals(id + "." + StdConfigurableEngine.PROP_DESCRIPTION)) {
            this.description = properties.getProperty(key.toString());
        } else if (key.equals(id + "." + StdConfigurableEngine.PROP_ISSUER)) {
            this.issuer = properties.getProperty(key.toString());
        } else if (key.equals(id + ".resolvers")) {
            //
            // It has resolvers, make sure this is set to true if
            // it has been already.
            //
            this.setRequiresResolversFlag(true);
            //
            // Parse the resolvers
            //
            Collection<PipResolver> resolvers = PipResolver.importResolvers(id + ".resolver",
                            properties.getProperty(key.toString()), properties, "super-admin");
            for (PipResolver resolver : resolvers) {
                this.addPipresolver(resolver);
            }
        } else if (key.toString().startsWith(id + ".resolver")) {
            //
            // Ignore, the PipResolver will parse these values
            //
        } else {
            //
            // Config Parameter
            //
            this.addPipconfigParam(new PipConfigParam(key.toString().substring(id.length() + 1),
                            properties.getProperty(key.toString())));
        }
    }

    /**
     * Gets the configuration.
     *
     * @param name the name
     * @return the configuration
     */
    @Transient
    public Map<String, String> getConfiguration(String name) {
        String prefix;
        if (name == null) {
            prefix = Integer.toString(this.id);
        } else {
            prefix = name;
        }
        if (!prefix.endsWith(".")) {
            prefix = prefix + ".";
        }
        Map<String, String> map = new HashMap<>();
        map.put(prefix + "classname", this.classname);
        map.put(prefix + "name", this.name);
        if (this.description != null) {
            map.put(prefix + "description", this.description);
        }
        if (this.issuer != null) {
            map.put(prefix + "issuer", this.issuer);
        }

        for (PipConfigParam param : this.pipconfigParams) {
            map.put(prefix + param.getParamName(), param.getParamValue());
        }

        List<String> ids = new ArrayList<>();
        Iterator<PipResolver> iter = this.pipresolvers.iterator();
        while (iter.hasNext()) {
            PipResolver resolver = iter.next();
            String idd = Integer.toString(resolver.getId());
            Map<String, String> resolverMap = resolver.getConfiguration(prefix + "resolver." + idd);
            map.putAll(resolverMap);
            ids.add(idd);
        }
        if (!ids.isEmpty()) {
            map.put(prefix + "resolvers", Joiner.on(',').join(ids));
        }
        return map;
    }

    /**
     * Generate properties.
     *
     * @param name the name
     * @return the properties
     */
    @Transient
    public Properties generateProperties(String name) {
        String prefix;
        if (name == null) {
            prefix = Integer.toString(this.id);
        } else {
            if (name.endsWith(".")) {
                prefix = name;
            } else {
                prefix = name + ".";
                /**
                 * Instantiates a new PIP configuration.
                 */

            }
        }
        Properties props = new Properties();
        props.setProperty("xacml.pip.engines", prefix);
        props.setProperty(prefix + "classname", this.classname);
        props.setProperty(prefix + "name", this.name);
        if (this.description != null) {
            props.setProperty(prefix + "description", this.description);
        }
        if (this.issuer != null && !this.issuer.isEmpty()) {
            props.setProperty(prefix + "issuer", this.issuer);
        }

        for (PipConfigParam param : this.pipconfigParams) {
            props.setProperty(prefix + param.getParamName(), param.getParamValue());
        }

        List<String> ids = new ArrayList<>();
        Iterator<PipResolver> iter = this.pipresolvers.iterator();
        while (iter.hasNext()) {
            PipResolver resolver = iter.next();
            String idd = Integer.toString(resolver.getId());
            resolver.generateProperties(props, prefix + "resolver." + idd);
            ids.add(idd);
        }
        if (!ids.isEmpty()) {
            props.setProperty(prefix + "resolvers", Joiner.on(',').join(ids));
        }
        return props;
    }
}