diff options
Diffstat (limited to 'ONAP-XACML/src')
3 files changed, 550 insertions, 318 deletions
diff --git a/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicy.java b/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicy.java index 03170bdb7..2b198f5e7 100644 --- a/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicy.java +++ b/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicy.java @@ -3,6 +3,7 @@ * ONAP-XACML * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,320 +46,319 @@ import com.google.common.collect.Lists; public class StdPDPPolicy implements PDPPolicy, Serializable { - private static final long serialVersionUID = 1L; - private static Log logger = LogFactory.getLog(StdPDPPolicy.class); - - private String id = null; - - private String name = null; - - private String policyId = null; - - private String description = null; - - private int[] version = null; - - private boolean isRoot = false; - - private boolean isValid = false; - - private URI location = null; - - public StdPDPPolicy() { - // - // Methods needed for JSON Deserialization - // - } - - public StdPDPPolicy(String id, boolean isRoot) { - this.id = id; - this.isRoot = isRoot; - } - - public StdPDPPolicy(String id, boolean isRoot, String name) { - this(id, isRoot); - this.name = name; - } - - - public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException { - this(id, isRoot); - this.name = name; - this.location = location; - - // - // Read the policy data - // - String theID = this.readPolicyData(); - - if (this.id == null) { - logger.debug("id is null so we are calling readPolicyData() to get the policyID"); - this.id = theID; - } - - logger.debug("The final outcome of the constructor returned the following: id = " + id + - ", location = " + location + ", name = " + name); - - } - - public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isValid, String policyId, - String description, String version) throws IOException { - this(id, isRoot); - this.name = name; - this.location = location; - this.policyId = policyId; - this.description = description; - this.version = versionStringToArray(version); - this.isValid = isValid; - - logger.debug("The final outcome of the constructor returned the following: id = " + id + - ", location = " + location + ", name = " + name + ", policyId = " + policyId + - ", description = " + description + ", Version = " + version); - - } - - public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException { - this(id, isRoot); - this.name = name; - this.location = location; - this.isValid = isFromAPI; - - logger.debug("The final outcome of the constructor returned the following: id = " + id + - ", location = " + location + ", name = " + name); - - } - - public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException { - this(id, isRoot); - this.location = location; - // - // Read the policy data - // - this.readPolicyData(); - // - // See if there's a name - // - for (Object key : properties.keySet()) { - if (key.toString().equals(id + ".name")) { - this.name = properties.getProperty(key.toString()); - break; - } - } - } - - - private String readPolicyData() throws IOException { - // - // Extract XACML policy information - // - URL url = this.location.toURL(); - Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream()); - if (rootElement == null || - ( - ! (rootElement instanceof PolicySetType) && - ! (rootElement instanceof PolicyType) - ) ) { - logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement); - this.isValid = false; - } else { - this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement)); - if (rootElement instanceof PolicySetType) { - this.policyId = ((PolicySetType)rootElement).getPolicySetId(); - this.description = ((PolicySetType)rootElement).getDescription(); - this.isValid = true; - this.version = versionStringToArray(((PolicySetType)rootElement).getVersion()); - } else if (rootElement instanceof PolicyType) { - this.policyId = ((PolicyType)rootElement).getPolicyId(); - this.description = ((PolicyType)rootElement).getDescription(); - this.version = versionStringToArray(((PolicyType)rootElement).getVersion()); - this.isValid = true; - } else { - PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName()); - } - } - if (this.policyId != null) { - ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId)); - if (!foo.isEmpty()) { - return foo.get(foo.size() - 1); - } - } - return null; - } - - @Override - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - @Override - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String getPolicyId() { - return this.policyId; - } - - @Override - public String getDescription() { - return this.description; - } - - @Override - public String getVersion() { - return versionArrayToString(this.version); - } - - @Override - @JsonIgnore - public int[] getVersionInts() { - return version; - } - - @Override - public boolean isRoot() { - return this.isRoot; - } - - @Override - public boolean isValid() - { - return this.isValid; - } - - @Override - @JsonIgnore - public InputStream getStream() throws PAPException, IOException { - try { - if (this.location != null) { - URL url = this.location.toURL(); - return url.openStream(); - } - return null; - } catch (FileNotFoundException e) { - throw new PAPException(e); - } - } - - @Override - public URI getLocation() throws PAPException { - return this.location; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - result = prime * result - + ((policyId == null) ? 0 : policyId.hashCode()); - result = prime * result; - if (version != null) { - for (int i = 0; i < version.length; i++) { - result += version[i]; - } - } - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - StdPDPPolicy other = (StdPDPPolicy) obj; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - if (policyId == null) { - if (other.policyId != null) - return false; - } else if (!policyId.equals(other.policyId)) - return false; - if (version != other.version) - return false; - return true; - } - - @Override - public String toString() { - return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId=" - + policyId + ", description=" + description + ", version=" - + this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid - + ", location=" + location + "]"; - } - - - /** - * Given a version string consisting of integers with dots between them, convert it into an array of ints. - * - * @param version - * @return - * @throws NumberFormatException - */ - public static int[] versionStringToArray(String version) throws NumberFormatException { - if (version == null || version.length() == 0) { - return new int[0]; - } - String[] stringArray = version.split("\\."); - int[] resultArray = new int[stringArray.length]; - for (int i = 0; i < stringArray.length; i++) { - resultArray[i] = Integer.parseInt(stringArray[i]); - } - return resultArray; - } - - /** - * Given an array representing a version, create the corresponding dot-separated string. - * - * @param array - * @return - */ - public static String versionArrayToString(int[] array) { - if (array == null || array.length == 0) { - return ""; - } - String versionString = ""; - if (array.length > 0) { - versionString = "" + Integer.toString(array[0]); - for (int i = 1; i < array.length; i++) { - versionString += "." + Integer.toString(array[i]); - } - } - return versionString; - } - - public void setPolicyId(String policyId) { - this.policyId = policyId; - } - public void setDescription(String description) { - this.description = description; - } - public void setVersion(String version) { - this.version = versionStringToArray(version); - } - public void setRoot(boolean isRoot) { - this.isRoot = isRoot; - } - public void setValid(boolean isValid) { - this.isValid = isValid; - } - public void setLocation(URI location) { - this.location = location; - } - + private static final long serialVersionUID = 1L; + private static Log logger = LogFactory.getLog(StdPDPPolicy.class); + + private String id = null; + + private String name = null; + + private String policyId = null; + + private String description = null; + + private int[] version = null; + + private boolean isRoot = false; + + private boolean isValid = false; + + private URI location = null; + + public StdPDPPolicy() { + // + // Methods needed for JSON Deserialization + // + } + + public StdPDPPolicy(String id, boolean isRoot) { + this.id = id; + this.isRoot = isRoot; + } + + public StdPDPPolicy(String id, boolean isRoot, String name) { + this(id, isRoot); + this.name = name; + } + + + public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException { + this(id, isRoot); + this.name = name; + this.location = location; + + // + // Read the policy data + // + String theID = this.readPolicyData(); + + if (this.id == null) { + logger.debug("id is null so we are calling readPolicyData() to get the policyID"); + this.id = theID; + } + + logger.debug("The final outcome of the constructor returned the following: id = " + id + + ", location = " + location + ", name = " + name); + + } + + public StdPDPPolicy(StdPDPPolicyParams stdPDPPolicyParams) throws IOException { + this(stdPDPPolicyParams.getId(), stdPDPPolicyParams.isRoot()); + this.name = stdPDPPolicyParams.getName(); + this.location = stdPDPPolicyParams.getLocation(); + this.policyId = stdPDPPolicyParams.getPolicyId(); + this.description = stdPDPPolicyParams.getDescription(); + this.version = versionStringToArray(stdPDPPolicyParams.getVersion()); + this.isValid = stdPDPPolicyParams.isValid(); + + logger.debug("The final outcome of the constructor returned the following: id = " + stdPDPPolicyParams.getId() + + ", location = " + stdPDPPolicyParams.getLocation() + ", name = " + stdPDPPolicyParams.getName() + ", policyId = " + stdPDPPolicyParams.getPolicyId() + + ", description = " + stdPDPPolicyParams.getDescription() + ", Version = " + stdPDPPolicyParams.getVersion()); + + } + + public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException { + this(id, isRoot); + this.name = name; + this.location = location; + this.isValid = isFromAPI; + + logger.debug("The final outcome of the constructor returned the following: id = " + id + + ", location = " + location + ", name = " + name); + + } + + public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException { + this(id, isRoot); + this.location = location; + // + // Read the policy data + // + this.readPolicyData(); + // + // See if there's a name + // + for (Object key : properties.keySet()) { + if (key.toString().equals(id + ".name")) { + this.name = properties.getProperty(key.toString()); + break; + } + } + } + + + private String readPolicyData() throws IOException { + // + // Extract XACML policy information + // + URL url = this.location.toURL(); + Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream()); + if (rootElement == null || + ( + ! (rootElement instanceof PolicySetType) && + ! (rootElement instanceof PolicyType) + ) ) { + logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement); + this.isValid = false; + } else { + this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement)); + if (rootElement instanceof PolicySetType) { + this.policyId = ((PolicySetType)rootElement).getPolicySetId(); + this.description = ((PolicySetType)rootElement).getDescription(); + this.isValid = true; + this.version = versionStringToArray(((PolicySetType)rootElement).getVersion()); + } else if (rootElement instanceof PolicyType) { + this.policyId = ((PolicyType)rootElement).getPolicyId(); + this.description = ((PolicyType)rootElement).getDescription(); + this.version = versionStringToArray(((PolicyType)rootElement).getVersion()); + this.isValid = true; + } else { + PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName()); + } + } + if (this.policyId != null) { + ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId)); + if (!foo.isEmpty()) { + return foo.get(foo.size() - 1); + } + } + return null; + } + + @Override + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String getPolicyId() { + return this.policyId; + } + + @Override + public String getDescription() { + return this.description; + } + + @Override + public String getVersion() { + return versionArrayToString(this.version); + } + + @Override + @JsonIgnore + public int[] getVersionInts() { + return version; + } + + @Override + public boolean isRoot() { + return this.isRoot; + } + + @Override + public boolean isValid() + { + return this.isValid; + } + + @Override + @JsonIgnore + public InputStream getStream() throws PAPException, IOException { + try { + if (this.location != null) { + URL url = this.location.toURL(); + return url.openStream(); + } + return null; + } catch (FileNotFoundException e) { + throw new PAPException(e); + } + } + + @Override + public URI getLocation() throws PAPException { + return this.location; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + + ((policyId == null) ? 0 : policyId.hashCode()); + result = prime * result; + if (version != null) { + for (int i = 0; i < version.length; i++) { + result += version[i]; + } + } + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StdPDPPolicy other = (StdPDPPolicy) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (policyId == null) { + if (other.policyId != null) + return false; + } else if (!policyId.equals(other.policyId)) + return false; + if (version != other.version) + return false; + return true; + } + + @Override + public String toString() { + return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId=" + + policyId + ", description=" + description + ", version=" + + this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid + + ", location=" + location + "]"; + } + + + /** + * Given a version string consisting of integers with dots between them, convert it into an array of ints. + * + * @param version + * @return + * @throws NumberFormatException + */ + public static int[] versionStringToArray(String version) throws NumberFormatException { + if (version == null || version.length() == 0) { + return new int[0]; + } + String[] stringArray = version.split("\\."); + int[] resultArray = new int[stringArray.length]; + for (int i = 0; i < stringArray.length; i++) { + resultArray[i] = Integer.parseInt(stringArray[i]); + } + return resultArray; + } + + /** + * Given an array representing a version, create the corresponding dot-separated string. + * + * @param array + * @return + */ + public static String versionArrayToString(int[] array) { + if (array == null || array.length == 0) { + return ""; + } + String versionString = ""; + if (array.length > 0) { + versionString = "" + Integer.toString(array[0]); + for (int i = 1; i < array.length; i++) { + versionString += "." + Integer.toString(array[i]); + } + } + return versionString; + } + + public void setPolicyId(String policyId) { + this.policyId = policyId; + } + public void setDescription(String description) { + this.description = description; + } + public void setVersion(String version) { + this.version = versionStringToArray(version); + } + public void setRoot(boolean isRoot) { + this.isRoot = isRoot; + } + public void setValid(boolean isValid) { + this.isValid = isValid; + } + public void setLocation(URI location) { + this.location = location; + } + } diff --git a/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicyParams.java b/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicyParams.java new file mode 100644 index 000000000..dddfe2576 --- /dev/null +++ b/ONAP-XACML/src/main/java/org/onap/policy/xacml/std/pap/StdPDPPolicyParams.java @@ -0,0 +1,211 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-XACML + * ================================================================================ + * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.xacml.std.pap; + +import java.net.URI; + +/** + * Parameters class for StdPDPPolicy + */ +public class StdPDPPolicyParams { + private String id; + private boolean isRoot; + private String name; + private URI location; + private boolean isValid; + private String policyId; + private String description; + private String version; + + /** + * Private constructor + */ + private StdPDPPolicyParams(){ + super(); + } + + /** + * Get an instance of builder class + * @return StdPDPPolicyParamsBuilder + */ + public static StdPDPPolicyParamsBuilder builder() { + return new StdPDPPolicyParamsBuilder(); + } + + /** + * Return id + * @return id + */ + public String getId() { + return id; + } + + /** + * Boolean to indicate root + * @return isRoot + */ + public boolean isRoot() { + return isRoot; + } + + /** + * Get name of policy + * @return name + */ + public String getName() { + return name; + } + + /** + * Retrieve the uri + * @return location + */ + public URI getLocation() { + return location; + } + + /** + * Check policy valid + * @return isValid + */ + public boolean isValid() { + return isValid; + } + + /** + * Retrieve policy id + * @return policy id + */ + public String getPolicyId() { + return policyId; + } + + /** + * Description of policy + * @return description + */ + public String getDescription() { + return description; + } + + /** + * Retrieve version of policy + * @return version + */ + public String getVersion() { + return version; + } + + /** + * Builder class for std pdp policy params class + */ + public static class StdPDPPolicyParamsBuilder { + StdPDPPolicyParams m = new StdPDPPolicyParams(); + + /** + * Build the policy params + * @return stdPdpPolicyParams object + */ + public StdPDPPolicyParams build() { + return m; + } + + /** + * Set id + * @param id - provide id + * @return builder + */ + public StdPDPPolicyParamsBuilder id(String id) { + m.id = id; + return this; + } + + /** + * Set whether isRoot + * @param isRoot - true/false + * @return builder + */ + public StdPDPPolicyParamsBuilder isRoot(boolean isRoot) { + m.isRoot = isRoot; + return this; + } + + /** + * Set name + * @param name - name of policy + * @return builder + */ + public StdPDPPolicyParamsBuilder name(String name) { + m.name = name; + return this; + } + + /** + * Set location uri + * @param uri - for location + * @return builder + */ + public StdPDPPolicyParamsBuilder location(URI uri) { + m.location = uri; + return this; + } + + /** + * Set valid flag + * @param isValid - whether the policy is valid + * @return builder + */ + public StdPDPPolicyParamsBuilder isValid(boolean isValid) { + m.isValid = isValid; + return this; + } + + /** + * Set policy id + * @param policyId - policy id + * @return builder + */ + public StdPDPPolicyParamsBuilder policyId(String policyId) { + m.policyId = policyId; + return this; + } + + /** + * Set description of policy + * @param description - of policy + * @return builder + */ + public StdPDPPolicyParamsBuilder description(String description) { + m.description = description; + return this; + } + + /** + * Set version of policy + * @param version - of policy + * @return builder + */ + public StdPDPPolicyParamsBuilder version(String version) { + m.version = version; + return this; + } + } +} diff --git a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java index cf293beef..26af008b6 100644 --- a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java +++ b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java @@ -3,6 +3,7 @@ * ONAP-XACML * ================================================================================ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +34,7 @@ import java.util.Properties; import org.junit.Test; import org.onap.policy.xacml.std.pap.StdPDPPolicy; import com.att.research.xacml.api.pap.PAPException; +import org.onap.policy.xacml.std.pap.StdPDPPolicyParams; public class StdPDPPolicyTest { @@ -50,7 +52,17 @@ public class StdPDPPolicyTest { assertNotNull(policy2); StdPDPPolicy policy4 = new StdPDPPolicy(); assertNotNull(policy4); - StdPDPPolicy policy5 = new StdPDPPolicy(value, true, value, uri, false, value, value, "1"); + + StdPDPPolicy policy5 = new StdPDPPolicy(StdPDPPolicyParams.builder() + .id(value) + .isRoot(true) + .name(value) + .location(uri) + .isValid(false) + .policyId(value) + .description(value) + .version("1") + .build()); assertNotNull(policy5); StdPDPPolicy policy6 = new StdPDPPolicy(value, true, value, uri, false); assertNotNull(policy6); @@ -112,7 +124,16 @@ public class StdPDPPolicyTest { // Set up test data String value = "testVal"; URI uri = new URI("http://localhost:54287"); - StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri, false, value, value, "1"); + StdPDPPolicy policy = new StdPDPPolicy(StdPDPPolicyParams.builder() + .id(value) + .isRoot(true) + .name(value) + .location(uri) + .isValid(false) + .policyId(value) + .description(value) + .version("1") + .build()); // Negative test stream policy.getStream(); |