Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / std / pap / StdPDPPIPConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.xacml.std.pap;
22
23 import com.att.research.xacml.api.pap.PDPPIPConfig;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25
26 import java.io.Serializable;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import lombok.EqualsAndHashCode;
32 import lombok.ToString;
33
34 @EqualsAndHashCode(callSuper = false)
35 @ToString
36 public class StdPDPPIPConfig implements PDPPIPConfig, Serializable {
37     private static final long serialVersionUID = 1L;
38
39     private String id;
40
41     private String name;
42
43     private String description;
44
45     private String classname;
46
47     private Map<String, String> config = new HashMap<>();
48
49     public StdPDPPIPConfig() {
50         //
51         // Default constructor
52         //
53     }
54
55     public StdPDPPIPConfig(String id) {
56         this.id = id;
57     }
58
59     /**
60      * Constructor.
61      *
62      * @param id String
63      * @param name String
64      * @param description String
65      */
66     public StdPDPPIPConfig(String id, String name, String description) {
67         this(id);
68         this.name = name;
69         this.description = description;
70     }
71
72     /**
73      * Constructor.
74      *
75      * @param id String
76      * @param properties Properties
77      */
78     public StdPDPPIPConfig(String id, Properties properties) {
79         this(id);
80         if (!this.initialize(properties)) {
81             throw new IllegalArgumentException("PIP Engine '" + id + "' has no classname property in config");
82         }
83     }
84
85     /**
86      * initialize.
87      *
88      * @param properties Properties
89      * @return boolean
90      */
91     public boolean initialize(Properties properties) {
92         boolean classnameSeen = false;
93         for (Object key : properties.keySet()) {
94             if (key.toString().startsWith(this.id + ".")) {
95                 if (key.toString().equals(this.id + ".name")) {
96                     this.name = properties.getProperty(key.toString());
97                 } else if (key.toString().equals(this.id + ".description")) {
98                     this.description = properties.getProperty(key.toString());
99                 } else if (key.toString().equals(this.id + ".classname")) {
100                     this.classname = properties.getProperty(key.toString());
101                     classnameSeen = true;
102                 }
103                 // all properties, including the special ones located above, are included in the properties list
104                 this.config.put(key.toString(), properties.getProperty(key.toString()));
105             }
106         }
107         return classnameSeen;
108     }
109
110     @Override
111     public String getId() {
112         return this.id;
113     }
114
115     public void setId(String id) {
116         this.id = id;
117     }
118
119     @Override
120     public String getName() {
121         return name;
122     }
123
124     public void setName(String name) {
125         this.name = name;
126     }
127
128     @Override
129     public String getDescription() {
130         return this.description;
131     }
132
133     public void setDescription(String description) {
134         this.description = description;
135     }
136
137     @Override
138     public String getClassname() {
139         return classname;
140     }
141
142     public void setClassname(String classname) {
143         this.classname = classname;
144     }
145
146     @Override
147     @JsonIgnore
148     public Map<String, String> getConfiguration() {
149         return Collections.unmodifiableMap(this.config);
150     }
151
152     public void setValues(Map<String, String> config) {
153         this.config = config;
154     }
155
156     @Override
157     @JsonIgnore
158     public boolean isConfigured() {
159         //
160         // Also include this in the JSON I/O if it is a data field rather than calculated
161         //
162         return true;
163     }
164
165     //
166     // Methods needed for JSON serialization/deserialization
167     //
168
169     public Map<String, String> getConfig() {
170         return config;
171     }
172
173     public void setConfig(Map<String, String> config) {
174         this.config = config;
175     }
176
177 }