2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.xacml.std.pap;
22 import org.onap.policy.common.logging.eelf.PolicyLogger;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Properties;
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;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.onap.policy.xacml.util.XACMLPolicyScanner;
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;
47 public class StdPDPPolicy implements PDPPolicy, Serializable {
48 private static final long serialVersionUID = 1L;
49 private static Log logger = LogFactory.getLog(StdPDPPolicy.class);
51 private String id = null;
53 private String name = null;
55 private String policyId = null;
57 private String description = null;
59 private int[] version = null;
61 private boolean isRoot = false;
63 private boolean isValid = false;
65 private URI location = null;
67 public StdPDPPolicy() {
69 // Methods needed for JSON Deserialization
73 public StdPDPPolicy(String id, boolean isRoot) {
78 public StdPDPPolicy(String id, boolean isRoot, String name) {
84 public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
87 this.location = location;
90 // Read the policy data
92 String theID = this.readPolicyData();
94 if (this.id == null) {
95 logger.debug("id is null so we are calling readPolicyData() to get the policyID");
99 logger.debug("The final outcome of the constructor returned the following: id = " + id +
100 ", location = " + location + ", name = " + name);
104 public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isValid, String policyId,
105 String description, String version) throws IOException {
108 this.location = location;
109 this.policyId = policyId;
110 this.description = description;
111 this.version = versionStringToArray(version);
112 this.isValid = isValid;
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);
120 public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException {
123 this.location = location;
124 this.isValid = isFromAPI;
126 logger.debug("The final outcome of the constructor returned the following: id = " + id +
127 ", location = " + location + ", name = " + name);
131 public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException {
133 this.location = location;
135 // Read the policy data
137 this.readPolicyData();
139 // See if there's a name
141 for (Object key : properties.keySet()) {
142 if (key.toString().equals(id + ".name")) {
143 this.name = properties.getProperty(key.toString());
150 private String readPolicyData() throws IOException {
152 // Extract XACML policy information
154 URL url = this.location.toURL();
155 Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream());
156 if (rootElement == null ||
158 ! (rootElement instanceof PolicySetType) &&
159 ! (rootElement instanceof PolicyType)
161 logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement);
162 this.isValid = false;
164 this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement));
165 if (rootElement instanceof PolicySetType) {
166 this.policyId = ((PolicySetType)rootElement).getPolicySetId();
167 this.description = ((PolicySetType)rootElement).getDescription();
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());
176 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
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);
189 public String getId() {
193 public void setId(String id) {
198 public String getName() {
202 public void setName(String name) {
207 public String getPolicyId() {
208 return this.policyId;
212 public String getDescription() {
213 return this.description;
217 public String getVersion() {
218 return versionArrayToString(this.version);
223 public int[] getVersionInts() {
228 public boolean isRoot() {
233 public boolean isValid()
240 public InputStream getStream() throws PAPException, IOException {
242 if (this.location != null) {
243 URL url = this.location.toURL();
244 return url.openStream();
247 } catch (FileNotFoundException e) {
248 throw new PAPException(e);
253 public URI getLocation() throws PAPException {
254 return this.location;
258 public int hashCode() {
259 final int prime = 31;
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];
274 public boolean equals(Object obj) {
279 if (getClass() != obj.getClass())
281 StdPDPPolicy other = (StdPDPPolicy) obj;
283 if (other.id != null)
285 } else if (!id.equals(other.id))
287 if (policyId == null) {
288 if (other.policyId != null)
290 } else if (!policyId.equals(other.policyId))
292 if (version != other.version)
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 + "]";
307 * Given a version string consisting of integers with dots between them, convert it into an array of ints.
311 * @throws NumberFormatException
313 public static int[] versionStringToArray(String version) throws NumberFormatException {
314 if (version == null || version.length() == 0) {
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]);
326 * Given an array representing a version, create the corresponding dot-separated string.
331 public static String versionArrayToString(int[] array) {
332 if (array == null || array.length == 0) {
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]);
342 return versionString;
345 public void setPolicyId(String policyId) {
346 this.policyId = policyId;
348 public void setDescription(String description) {
349 this.description = description;
351 public void setVersion(String version) {
352 this.version = versionStringToArray(version);
354 public void setRoot(boolean isRoot) {
355 this.isRoot = isRoot;
357 public void setValid(boolean isValid) {
358 this.isValid = isValid;
360 public void setLocation(URI location) {
361 this.location = location;