Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-XACML / src / main / java / org / openecomp / policy / xacml / std / pap / StdPDPPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.policy.xacml.std.pap;
21
22 import org.openecomp.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.openecomp.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                                 //TODO:EELF Cleanup - Remove logger
172                                 //logger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
173                                 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
174                         }
175                 }
176                 if (this.policyId != null) {
177                         ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId));
178                         if (foo.isEmpty() == false) {
179                                 return foo.get(foo.size() - 1);
180                         }
181                 }
182                 return null;
183         }
184         
185         @Override
186         public String getId() {
187                 return id;
188         }
189
190         public void setId(String id) {
191                 this.id = id;
192         }
193
194         @Override
195         public String getName() {
196                 return this.name;
197         }
198         
199         public void setName(String name) {
200                 this.name = name;
201         }
202
203         @Override
204         public String getPolicyId() {
205                 return this.policyId;
206         }
207         
208         @Override
209         public String getDescription() {
210                 return this.description;
211         }
212         
213         @Override
214         public String getVersion() {
215                 return versionArrayToString(this.version);
216         }
217         
218         @Override
219         @JsonIgnore
220         public int[] getVersionInts() {
221                 return version;
222         }
223
224         @Override
225         public boolean isRoot() {
226                 return this.isRoot;
227         }
228
229         @Override
230         public boolean isValid()
231         {
232                 return this.isValid;
233         }
234
235         @Override
236         @JsonIgnore
237         public InputStream getStream() throws PAPException, IOException {
238                 try {
239                         if (this.location != null) {
240                                 URL url = this.location.toURL();
241                                 return url.openStream();
242                         }
243                         return null;
244                 } catch (FileNotFoundException e) {
245                         throw new PAPException(e);
246                 }
247         }
248
249         @Override
250         public URI getLocation() throws PAPException {
251                 return this.location;
252         }
253
254         @Override
255         public int hashCode() {
256                 final int prime = 31;
257                 int result = 1;
258                 result = prime * result + ((id == null) ? 0 : id.hashCode());
259                 result = prime * result
260                                 + ((policyId == null) ? 0 : policyId.hashCode());
261                 result = prime * result;
262                 if (version != null) {
263                         for (int i = 0; i < version.length; i++) {
264                                 result += version[i];
265                         }
266                 }
267                 return result;
268         }
269
270         @Override
271         public boolean equals(Object obj) {
272                 if (this == obj)
273                         return true;
274                 if (obj == null)
275                         return false;
276                 if (getClass() != obj.getClass())
277                         return false;
278                 StdPDPPolicy other = (StdPDPPolicy) obj;
279                 if (id == null) {
280                         if (other.id != null)
281                                 return false;
282                 } else if (!id.equals(other.id))
283                         return false;
284                 if (policyId == null) {
285                         if (other.policyId != null)
286                                 return false;
287                 } else if (!policyId.equals(other.policyId))
288                         return false;
289                 if (version != other.version)
290                         return false;
291                 return true;
292         }
293
294         @Override
295         public String toString() {
296                 return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId="
297                                 + policyId + ", description=" + description + ", version="
298                                 + this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid
299                                 + ", location=" + location + "]";
300         }
301         
302         
303         /**
304          * Given a version string consisting of integers with dots between them, convert it into an array of ints.
305          * 
306          * @param version
307          * @return
308          * @throws NumberFormatException
309          */
310         public static int[] versionStringToArray(String version) throws NumberFormatException {
311                 if (version == null || version.length() == 0) {
312                         return new int[0];
313                 }
314                 String[] stringArray = version.split("\\.");
315                 int[] resultArray = new int[stringArray.length];
316                 for (int i = 0; i < stringArray.length; i++) {
317                         resultArray[i] = Integer.parseInt(stringArray[i]);
318                 }
319                 return resultArray;
320         }
321         
322         /**
323          * Given an array representing a version, create the corresponding dot-separated string.
324          * 
325          * @param array
326          * @return
327          */
328         public static String versionArrayToString(int[] array) {
329                 if (array == null || array.length == 0) {
330                         return "";
331                 }
332                 String versionString = "";
333                 if (array.length > 0) {
334                         versionString = "" + array[0];
335                         for (int i = 1; i < array.length; i++) {
336                                 versionString += "." + array[i];
337                         }
338                 }
339                 return versionString;
340         }
341         
342
343         
344         //
345         // Methods needed for JSON Deserialization
346         //
347         public StdPDPPolicy() {}
348         
349         public void setPolicyId(String policyId) {
350                 this.policyId = policyId;
351         }
352         public void setDescription(String description) {
353                 this.description = description;
354         }
355         public void setVersion(String version) {
356                 this.version = versionStringToArray(version);
357         }
358         public void setRoot(boolean isRoot) {
359                 this.isRoot = isRoot;
360         }
361         public void setValid(boolean isValid) {
362                 this.isValid = isValid;
363         }
364         public void setLocation(URI location) {
365                 this.location = location;
366         }
367         
368 }