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.openecomp.policy.xacml.std.pap;
22 import org.openecomp.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.openecomp.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 //TODO:EELF Cleanup - Remove logger
172 //logger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
173 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
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);
186 public String getId() {
190 public void setId(String id) {
195 public String getName() {
199 public void setName(String name) {
204 public String getPolicyId() {
205 return this.policyId;
209 public String getDescription() {
210 return this.description;
214 public String getVersion() {
215 return versionArrayToString(this.version);
220 public int[] getVersionInts() {
225 public boolean isRoot() {
230 public boolean isValid()
237 public InputStream getStream() throws PAPException, IOException {
239 if (this.location != null) {
240 URL url = this.location.toURL();
241 return url.openStream();
244 } catch (FileNotFoundException e) {
245 throw new PAPException(e);
250 public URI getLocation() throws PAPException {
251 return this.location;
255 public int hashCode() {
256 final int prime = 31;
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];
271 public boolean equals(Object obj) {
276 if (getClass() != obj.getClass())
278 StdPDPPolicy other = (StdPDPPolicy) obj;
280 if (other.id != null)
282 } else if (!id.equals(other.id))
284 if (policyId == null) {
285 if (other.policyId != null)
287 } else if (!policyId.equals(other.policyId))
289 if (version != other.version)
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 + "]";
304 * Given a version string consisting of integers with dots between them, convert it into an array of ints.
308 * @throws NumberFormatException
310 public static int[] versionStringToArray(String version) throws NumberFormatException {
311 if (version == null || version.length() == 0) {
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]);
323 * Given an array representing a version, create the corresponding dot-separated string.
328 public static String versionArrayToString(int[] array) {
329 if (array == null || array.length == 0) {
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];
339 return versionString;
345 // Methods needed for JSON Deserialization
347 public StdPDPPolicy() {}
349 public void setPolicyId(String policyId) {
350 this.policyId = policyId;
352 public void setDescription(String description) {
353 this.description = description;
355 public void setVersion(String version) {
356 this.version = versionStringToArray(version);
358 public void setRoot(boolean isRoot) {
359 this.isRoot = isRoot;
361 public void setValid(boolean isValid) {
362 this.isValid = isValid;
364 public void setLocation(URI location) {
365 this.location = location;