2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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=========================================================
22 package org.onap.policy.xacml.std.pap;
24 import com.att.research.xacml.api.pap.PAPException;
25 import com.att.research.xacml.api.pap.PDPPolicy;
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27 import com.google.common.base.Splitter;
28 import com.google.common.collect.Lists;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.Serializable;
36 import java.util.ArrayList;
37 import java.util.Properties;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.onap.policy.common.logging.eelf.PolicyLogger;
45 import org.onap.policy.xacml.util.XACMLPolicyScanner;
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) {
87 * @param isRoot boolean
90 * @throws IOException IOException
92 public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
95 this.location = location;
98 // Read the policy data
100 String theID = this.readPolicyData();
102 if (this.id == null) {
103 logger.debug("id is null so we are calling readPolicyData() to get the policyID");
107 logger.debug("The final outcome of the constructor returned the following: id = " + id + ", location = "
108 + location + ", name = " + name);
113 * StdPDPPolicy constructor.
115 * @param stdPDPPolicyParams StdPDPPolicyParams
116 * @throws IOException IOException
118 public StdPDPPolicy(StdPDPPolicyParams stdPDPPolicyParams) throws IOException {
119 this(stdPDPPolicyParams.getId(), stdPDPPolicyParams.isRoot());
120 this.name = stdPDPPolicyParams.getName();
121 this.location = stdPDPPolicyParams.getLocation();
122 this.policyId = stdPDPPolicyParams.getPolicyId();
123 this.description = stdPDPPolicyParams.getDescription();
124 this.version = versionStringToArray(stdPDPPolicyParams.getVersion());
125 this.isValid = stdPDPPolicyParams.isValid();
127 logger.debug("The final outcome of the constructor returned the following: id = " + stdPDPPolicyParams.getId()
128 + ", location = " + stdPDPPolicyParams.getLocation() + ", name = " + stdPDPPolicyParams.getName()
129 + ", policyId = " + stdPDPPolicyParams.getPolicyId() + ", description = "
130 + stdPDPPolicyParams.getDescription() + ", Version = " + stdPDPPolicyParams.getVersion());
138 * @param isRoot boolean
140 * @param location URI
141 * @param isFromAPI boolean
142 * @throws IOException IOException
144 public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException {
147 this.location = location;
148 this.isValid = isFromAPI;
150 logger.debug("The final outcome of the constructor returned the following: id = " + id + ", location = "
151 + location + ", name = " + name);
159 * @param isRoot boolean
160 * @param location URI
161 * @param properties boolean
162 * @throws IOException IOException
164 public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException {
166 this.location = location;
168 // Read the policy data
170 this.readPolicyData();
172 // See if there's a name
174 for (Object key : properties.keySet()) {
175 if (key.toString().equals(id + ".name")) {
176 this.name = properties.getProperty(key.toString());
182 private String readPolicyData() throws IOException {
184 // Extract XACML policy information
186 URL url = this.location.toURL();
187 Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream());
188 if (rootElement == null || (!(rootElement instanceof PolicySetType) && !(rootElement instanceof PolicyType))) {
189 logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement);
190 this.isValid = false;
192 this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement));
193 if (rootElement instanceof PolicySetType) {
194 this.policyId = ((PolicySetType) rootElement).getPolicySetId();
195 this.description = ((PolicySetType) rootElement).getDescription();
197 this.version = versionStringToArray(((PolicySetType) rootElement).getVersion());
198 } else if (rootElement instanceof PolicyType) {
199 this.policyId = ((PolicyType) rootElement).getPolicyId();
200 this.description = ((PolicyType) rootElement).getDescription();
201 this.version = versionStringToArray(((PolicyType) rootElement).getVersion());
204 PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
207 if (this.policyId != null) {
208 ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId));
209 if (!foo.isEmpty()) {
210 return foo.get(foo.size() - 1);
217 public String getId() {
221 public void setId(String id) {
226 public String getName() {
230 public void setName(String name) {
235 public String getPolicyId() {
236 return this.policyId;
240 public String getDescription() {
241 return this.description;
245 public String getVersion() {
246 return versionArrayToString(this.version);
251 public int[] getVersionInts() {
256 public boolean isRoot() {
261 public boolean isValid() {
267 public InputStream getStream() throws PAPException, IOException {
269 if (this.location != null) {
270 URL url = this.location.toURL();
271 return url.openStream();
274 } catch (FileNotFoundException e) {
275 throw new PAPException(e);
280 public URI getLocation() throws PAPException {
281 return this.location;
285 public int hashCode() {
286 final int prime = 31;
288 result = prime * result + ((id == null) ? 0 : id.hashCode());
289 result = prime * result + ((policyId == null) ? 0 : policyId.hashCode());
290 result = prime * result;
291 if (version != null) {
292 for (int i = 0; i < version.length; i++) {
293 result += version[i];
300 public boolean equals(Object obj) {
307 if (getClass() != obj.getClass()) {
310 StdPDPPolicy other = (StdPDPPolicy) obj;
312 if (other.id != null) {
315 } else if (!id.equals(other.id)) {
318 if (policyId == null) {
319 if (other.policyId != null) {
322 } else if (!policyId.equals(other.policyId)) {
325 if (version != other.version) {
332 public String toString() {
333 return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId=" + policyId + ", description=" + description
334 + ", version=" + this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid + ", location="
339 * Given a version string consisting of integers with dots between them, convert it into an array of ints.
341 * @param version String
344 public static int[] versionStringToArray(String version) {
345 if (version == null || version.length() == 0) {
348 String[] stringArray = version.split("\\.");
349 int[] resultArray = new int[stringArray.length];
350 for (int i = 0; i < stringArray.length; i++) {
351 resultArray[i] = Integer.parseInt(stringArray[i]);
357 * Given an array representing a version, create the corresponding dot-separated string.
359 * @param array int array
362 public static String versionArrayToString(int[] array) {
363 if (array == null || array.length == 0) {
366 String versionString = "";
367 if (array.length > 0) {
368 versionString = "" + Integer.toString(array[0]);
369 for (int i = 1; i < array.length; i++) {
370 versionString += "." + Integer.toString(array[i]);
373 return versionString;
376 public void setPolicyId(String policyId) {
377 this.policyId = policyId;
380 public void setDescription(String description) {
381 this.description = description;
384 public void setVersion(String version) {
385 this.version = versionStringToArray(version);
388 public void setRoot(boolean isRoot) {
389 this.isRoot = isRoot;
392 public void setValid(boolean isValid) {
393 this.isValid = isValid;
396 public void setLocation(URI location) {
397 this.location = location;