Merge "Implement Encryption on Passwords"
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / std / pap / StdPDPPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-2018 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 package org.onap.policy.xacml.std.pap;
21
22 import org.onap.policy.common.logging.eelf.PolicyLogger;
23
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.Serializable;
28 import java.net.URI;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.Properties;
32
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.onap.policy.xacml.util.XACMLPolicyScanner;
39
40 import com.att.research.xacml.api.pap.PAPException;
41 import com.att.research.xacml.api.pap.PDPPolicy;
42 import com.fasterxml.jackson.annotation.JsonIgnore;
43 import com.google.common.base.Splitter;
44 import com.google.common.collect.Lists;
45
46
47 public class StdPDPPolicy implements PDPPolicy, Serializable {
48         private static final long serialVersionUID = 1L;
49         private static Log      logger  = LogFactory.getLog(StdPDPPolicy.class);
50         
51         private String id = null;
52         
53         private String name = null;
54
55         private String policyId = null;
56         
57         private String description = null;
58         
59         private int[]   version = null;
60
61         private boolean isRoot = false;
62         
63         private boolean isValid = false;
64         
65         private URI location = null;
66         
67         public StdPDPPolicy() {
68                 //
69                 // Methods needed for JSON Deserialization
70                 //
71         }
72         
73         public StdPDPPolicy(String id, boolean isRoot) {
74                 this.id = id;
75                 this.isRoot = isRoot;
76         }
77
78         public StdPDPPolicy(String id, boolean isRoot, String name) {
79                 this(id, isRoot);
80                 this.name = name;
81         }
82
83         
84         public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
85                 this(id, isRoot);
86                 this.name = name;
87                 this.location = location;
88                 
89                 //
90                 // Read the policy data
91                 //              
92                 String theID = this.readPolicyData();
93
94                 if (this.id == null) {
95                         logger.debug("id is null so we are calling readPolicyData() to get the policyID");
96                         this.id = theID;
97                 }
98                 
99                 logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
100                                                 ", location = " + location + ", name = " + name);
101                 
102         }
103         
104         public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isValid, String policyId, 
105                                                         String description, String version) throws IOException {
106                 this(id, isRoot);
107                 this.name = name;
108                 this.location = location;
109                 this.policyId = policyId;
110                 this.description = description;
111                 this.version = versionStringToArray(version);
112                 this.isValid = isValid;
113                 
114                 logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
115                                                 ", location = " + location + ", name = " + name + ", policyId = " + policyId +
116                                                         ", description = " + description + ", Version = " + version);
117                 
118         }
119         
120         public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException {
121                 this(id, isRoot);
122                 this.name = name;
123                 this.location = location;
124                 this.isValid = isFromAPI;
125                 
126                 logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
127                                                 ", location = " + location + ", name = " + name);
128                 
129         }
130
131         public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException {
132                 this(id, isRoot);
133                 this.location = location;
134                 //
135                 // Read the policy data
136                 //
137                 this.readPolicyData();
138                 //
139                 // See if there's a name
140                 //
141                 for (Object key : properties.keySet()) {
142                         if (key.toString().equals(id + ".name")) {
143                                 this.name = properties.getProperty(key.toString());
144                                 break;
145                         }
146                 }
147         }
148         
149         
150         private String readPolicyData() throws IOException {
151                 //
152                 // Extract XACML policy information
153                 //
154                 URL url = this.location.toURL();
155                 Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream());
156                 if (rootElement == null ||
157                         (
158                                 ! (rootElement instanceof PolicySetType) &&
159                                 ! (rootElement instanceof PolicyType)
160                         )       ) {
161                         logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement);
162                         this.isValid = false;
163                 } else {
164                         this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement));
165                         if (rootElement instanceof PolicySetType) {
166                                 this.policyId = ((PolicySetType)rootElement).getPolicySetId();
167                                 this.description = ((PolicySetType)rootElement).getDescription();
168                                 this.isValid = true;
169                                 this.version = versionStringToArray(((PolicySetType)rootElement).getVersion());
170                         } else if (rootElement instanceof PolicyType) {
171                                 this.policyId = ((PolicyType)rootElement).getPolicyId();
172                                 this.description = ((PolicyType)rootElement).getDescription();
173                                 this.version = versionStringToArray(((PolicyType)rootElement).getVersion());
174                                 this.isValid = true;
175                         } else {
176                                 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
177                         }
178                 }
179                 if (this.policyId != null) {
180                         ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId));
181                         if (!foo.isEmpty()) {
182                                 return foo.get(foo.size() - 1);
183                         }
184                 }
185                 return null;
186         }
187         
188         @Override
189         public String getId() {
190                 return id;
191         }
192
193         public void setId(String id) {
194                 this.id = id;
195         }
196
197         @Override
198         public String getName() {
199                 return this.name;
200         }
201         
202         public void setName(String name) {
203                 this.name = name;
204         }
205
206         @Override
207         public String getPolicyId() {
208                 return this.policyId;
209         }
210         
211         @Override
212         public String getDescription() {
213                 return this.description;
214         }
215         
216         @Override
217         public String getVersion() {
218                 return versionArrayToString(this.version);
219         }
220         
221         @Override
222         @JsonIgnore
223         public int[] getVersionInts() {
224                 return version;
225         }
226
227         @Override
228         public boolean isRoot() {
229                 return this.isRoot;
230         }
231
232         @Override
233         public boolean isValid()
234         {
235                 return this.isValid;
236         }
237
238         @Override
239         @JsonIgnore
240         public InputStream getStream() throws PAPException, IOException {
241                 try {
242                         if (this.location != null) {
243                                 URL url = this.location.toURL();
244                                 return url.openStream();
245                         }
246                         return null;
247                 } catch (FileNotFoundException e) {
248                         throw new PAPException(e);
249                 }
250         }
251
252         @Override
253         public URI getLocation() throws PAPException {
254                 return this.location;
255         }
256
257         @Override
258         public int hashCode() {
259                 final int prime = 31;
260                 int result = 1;
261                 result = prime * result + ((id == null) ? 0 : id.hashCode());
262                 result = prime * result
263                                 + ((policyId == null) ? 0 : policyId.hashCode());
264                 result = prime * result;
265                 if (version != null) {
266                         for (int i = 0; i < version.length; i++) {
267                                 result += version[i];
268                         }
269                 }
270                 return result;
271         }
272
273         @Override
274         public boolean equals(Object obj) {
275                 if (this == obj)
276                         return true;
277                 if (obj == null)
278                         return false;
279                 if (getClass() != obj.getClass())
280                         return false;
281                 StdPDPPolicy other = (StdPDPPolicy) obj;
282                 if (id == null) {
283                         if (other.id != null)
284                                 return false;
285                 } else if (!id.equals(other.id))
286                         return false;
287                 if (policyId == null) {
288                         if (other.policyId != null)
289                                 return false;
290                 } else if (!policyId.equals(other.policyId))
291                         return false;
292                 if (version != other.version)
293                         return false;
294                 return true;
295         }
296
297         @Override
298         public String toString() {
299                 return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId="
300                                 + policyId + ", description=" + description + ", version="
301                                 + this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid
302                                 + ", location=" + location + "]";
303         }
304         
305         
306         /**
307          * Given a version string consisting of integers with dots between them, convert it into an array of ints.
308          * 
309          * @param version
310          * @return
311          * @throws NumberFormatException
312          */
313         public static int[] versionStringToArray(String version) throws NumberFormatException {
314                 if (version == null || version.length() == 0) {
315                         return new int[0];
316                 }
317                 String[] stringArray = version.split("\\.");
318                 int[] resultArray = new int[stringArray.length];
319                 for (int i = 0; i < stringArray.length; i++) {
320                         resultArray[i] = Integer.parseInt(stringArray[i]);
321                 }
322                 return resultArray;
323         }
324         
325         /**
326          * Given an array representing a version, create the corresponding dot-separated string.
327          * 
328          * @param array
329          * @return
330          */
331         public static String versionArrayToString(int[] array) {
332                 if (array == null || array.length == 0) {
333                         return "";
334                 }
335                 String versionString = "";
336                 if (array.length > 0) {
337                         versionString = "" + Integer.toString(array[0]);
338                         for (int i = 1; i < array.length; i++) {
339                                 versionString += "." + Integer.toString(array[i]);
340                         }
341                 }
342                 return versionString;
343         }
344         
345         public void setPolicyId(String policyId) {
346                 this.policyId = policyId;
347         }
348         public void setDescription(String description) {
349                 this.description = description;
350         }
351         public void setVersion(String version) {
352                 this.version = versionStringToArray(version);
353         }
354         public void setRoot(boolean isRoot) {
355                 this.isRoot = isRoot;
356         }
357         public void setValid(boolean isValid) {
358                 this.isValid = isValid;
359         }
360         public void setLocation(URI location) {
361                 this.location = location;
362         }
363         
364 }