2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.xacml.std.pap;
23 import org.onap.policy.common.logging.eelf.PolicyLogger;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.Serializable;
31 import java.util.ArrayList;
32 import java.util.Properties;
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;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.onap.policy.xacml.util.XACMLPolicyScanner;
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;
48 public class StdPDPPolicy implements PDPPolicy, Serializable {
49 private static final long serialVersionUID = 1L;
50 private static Log logger = LogFactory.getLog(StdPDPPolicy.class);
52 private String id = null;
54 private String name = null;
56 private String policyId = null;
58 private String description = null;
60 private int[] version = null;
62 private boolean isRoot = false;
64 private boolean isValid = false;
66 private URI location = null;
68 public StdPDPPolicy() {
70 // Methods needed for JSON Deserialization
74 public StdPDPPolicy(String id, boolean isRoot) {
79 public StdPDPPolicy(String id, boolean isRoot, String name) {
85 public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
88 this.location = location;
91 // Read the policy data
93 String theID = this.readPolicyData();
95 if (this.id == null) {
96 logger.debug("id is null so we are calling readPolicyData() to get the policyID");
100 logger.debug("The final outcome of the constructor returned the following: id = " + id +
101 ", location = " + location + ", name = " + name);
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();
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());
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;