2 * ============LICENSE_START=======================================================
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
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;
68 public StdPDPPolicy(String id, boolean isRoot) {
73 public StdPDPPolicy(String id, boolean isRoot, String name) {
79 public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
82 this.location = location;
85 // Read the policy data
87 String theID = this.readPolicyData();
89 if (this.id == null) {
90 logger.debug("id is null so we are calling readPolicyData() to get the policyID");
94 logger.debug("The final outcome of the constructor returned the following: id = " + id +
95 ", location = " + location + ", name = " + name);
99 public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isValid, String policyId,
100 String description, String version) throws IOException {
103 this.location = location;
104 this.policyId = policyId;
105 this.description = description;
106 this.version = versionStringToArray(version);
107 this.isValid = isValid;
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);
115 public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException {
118 this.location = location;
119 this.isValid = isFromAPI;
121 logger.debug("The final outcome of the constructor returned the following: id = " + id +
122 ", location = " + location + ", name = " + name);
126 public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException {
128 this.location = location;
130 // Read the policy data
132 this.readPolicyData();
134 // See if there's a name
136 for (Object key : properties.keySet()) {
137 if (key.toString().equals(id + ".name")) {
138 this.name = properties.getProperty(key.toString());
145 private String readPolicyData() throws IOException {
147 // Extract XACML policy information
149 URL url = this.location.toURL();
150 Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream());
151 if (rootElement == null ||
153 ! (rootElement instanceof PolicySetType) &&
154 ! (rootElement instanceof PolicyType)
156 logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement);
157 this.isValid = false;
159 this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement));
160 if (rootElement instanceof PolicySetType) {
161 this.policyId = ((PolicySetType)rootElement).getPolicySetId();
162 this.description = ((PolicySetType)rootElement).getDescription();
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());
171 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
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);
184 public String getId() {
188 public void setId(String id) {
193 public String getName() {
197 public void setName(String name) {
202 public String getPolicyId() {
203 return this.policyId;
207 public String getDescription() {
208 return this.description;
212 public String getVersion() {
213 return versionArrayToString(this.version);
218 public int[] getVersionInts() {
223 public boolean isRoot() {
228 public boolean isValid()
235 public InputStream getStream() throws PAPException, IOException {
237 if (this.location != null) {
238 URL url = this.location.toURL();
239 return url.openStream();
242 } catch (FileNotFoundException e) {
243 throw new PAPException(e);
248 public URI getLocation() throws PAPException {
249 return this.location;
253 public int hashCode() {
254 final int prime = 31;
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];
269 public boolean equals(Object obj) {
274 if (getClass() != obj.getClass())
276 StdPDPPolicy other = (StdPDPPolicy) obj;
278 if (other.id != null)
280 } else if (!id.equals(other.id))
282 if (policyId == null) {
283 if (other.policyId != null)
285 } else if (!policyId.equals(other.policyId))
287 if (version != other.version)
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 + "]";
302 * Given a version string consisting of integers with dots between them, convert it into an array of ints.
306 * @throws NumberFormatException
308 public static int[] versionStringToArray(String version) throws NumberFormatException {
309 if (version == null || version.length() == 0) {
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]);
321 * Given an array representing a version, create the corresponding dot-separated string.
326 public static String versionArrayToString(int[] array) {
327 if (array == null || array.length == 0) {
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];
337 return versionString;
343 // Methods needed for JSON Deserialization
345 public StdPDPPolicy() {}
347 public void setPolicyId(String policyId) {
348 this.policyId = policyId;
350 public void setDescription(String description) {
351 this.description = description;
353 public void setVersion(String version) {
354 this.version = versionStringToArray(version);
356 public void setRoot(boolean isRoot) {
357 this.isRoot = isRoot;
359 public void setValid(boolean isValid) {
360 this.isValid = isValid;
362 public void setLocation(URI location) {
363 this.location = location;