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