2b198f5e7080dc35f9a0c0fcc903b72dc0c445eb
[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  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.policy.xacml.std.pap;
22
23 import org.onap.policy.common.logging.eelf.PolicyLogger;
24
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.Serializable;
29 import java.net.URI;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.Properties;
33
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.onap.policy.xacml.util.XACMLPolicyScanner;
40
41 import com.att.research.xacml.api.pap.PAPException;
42 import com.att.research.xacml.api.pap.PDPPolicy;
43 import com.fasterxml.jackson.annotation.JsonIgnore;
44 import com.google.common.base.Splitter;
45 import com.google.common.collect.Lists;
46
47
48 public class StdPDPPolicy implements PDPPolicy, Serializable {
49     private static final long serialVersionUID = 1L;
50     private static Log logger = LogFactory.getLog(StdPDPPolicy.class);
51
52     private String id = null;
53
54     private String name = null;
55
56     private String policyId = null;
57
58     private String description = null;
59
60     private int[]       version = null;
61
62     private boolean isRoot = false;
63
64     private boolean isValid = false;
65
66     private URI location = null;
67
68     public StdPDPPolicy() {
69         //
70         // Methods needed for JSON Deserialization
71         //
72     }
73
74     public StdPDPPolicy(String id, boolean isRoot) {
75         this.id = id;
76         this.isRoot = isRoot;
77     }
78
79     public StdPDPPolicy(String id, boolean isRoot, String name) {
80         this(id, isRoot);
81         this.name = name;
82     }
83
84
85     public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
86         this(id, isRoot);
87         this.name = name;
88         this.location = location;
89
90         //
91         // Read the policy data
92         //
93         String theID = this.readPolicyData();
94
95         if (this.id == null) {
96             logger.debug("id is null so we are calling readPolicyData() to get the policyID");
97             this.id = theID;
98         }
99
100         logger.debug("The final outcome of the constructor returned the following:  id = " + id +
101                         ", location = " + location + ", name = " + name);
102
103     }
104
105     public StdPDPPolicy(StdPDPPolicyParams stdPDPPolicyParams) throws IOException {
106         this(stdPDPPolicyParams.getId(), stdPDPPolicyParams.isRoot());
107         this.name = stdPDPPolicyParams.getName();
108         this.location = stdPDPPolicyParams.getLocation();
109         this.policyId = stdPDPPolicyParams.getPolicyId();
110         this.description = stdPDPPolicyParams.getDescription();
111         this.version = versionStringToArray(stdPDPPolicyParams.getVersion());
112         this.isValid = stdPDPPolicyParams.isValid();
113
114         logger.debug("The final outcome of the constructor returned the following:  id = " + stdPDPPolicyParams.getId() +
115                         ", location = " + stdPDPPolicyParams.getLocation() + ", name = " + stdPDPPolicyParams.getName() + ", policyId = " + stdPDPPolicyParams.getPolicyId() +
116                             ", description = " + stdPDPPolicyParams.getDescription() + ", Version = " + stdPDPPolicyParams.getVersion());
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 }