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=========================================================
21 package org.onap.policy.pdp.rest.impl;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.MalformedURLException;
28 import java.net.URLConnection;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
36 import org.onap.policy.xacml.api.XACMLErrorConstants;
37 import com.att.research.xacml.std.StdStatusCode;
38 import com.att.research.xacml.std.dom.DOMStructureException;
39 import com.att.research.xacml.util.FactoryException;
40 import com.att.research.xacml.util.XACMLProperties;
41 import com.att.research.xacmlatt.pdp.policy.Policy;
42 import com.att.research.xacmlatt.pdp.policy.PolicyDef;
43 import com.att.research.xacmlatt.pdp.policy.PolicyFinder;
44 import com.att.research.xacmlatt.pdp.policy.PolicyFinderFactory;
45 import com.att.research.xacmlatt.pdp.policy.dom.DOMPolicyDef;
46 import com.att.research.xacmlatt.pdp.std.StdPolicyFinder;
47 import com.google.common.base.Splitter;
49 public class XACMLPdpPolicyFinderFactory extends PolicyFinderFactory {
50 public static final String PROP_FILE = ".file";
51 public static final String PROP_URL = ".url";
53 private static Log LOGGER = LogFactory.getLog(XACMLPdpPolicyFinderFactory.class);
54 private List<PolicyDef> rootPolicies;
55 private List<PolicyDef> referencedPolicies;
56 private boolean needsInit = true;
58 private Properties properties = null;
60 public XACMLPdpPolicyFinderFactory() {
62 // Here we differ from the StdPolicyFinderFactory in that we initialize right away.
63 // We do not wait for a policy request to happen to look for and load policies.
68 public XACMLPdpPolicyFinderFactory(Properties properties) {
70 // Save our properties
72 this.properties = properties;
74 // Here we differ from the StdPolicyFinderFactory in that we initialize right away.
75 // We do not wait for a policy request to happen to look for and load policies.
81 * Loads the <code>PolicyDef</code> for the given <code>String</code> identifier by looking first
82 * for a ".file" property associated with the ID and using that to load from a <code>File</code> and
83 * looking for a ".url" property associated with the ID and using that to load from a <code>URL</code>.
85 * @param policyId the <code>String</code> identifier for the policy
86 * @return a <code>PolicyDef</code> loaded from the given identifier
88 protected PolicyDef loadPolicyDef(String policyId) {
89 String propLocation = null;
90 if (this.properties == null) {
91 propLocation = XACMLProperties.getProperty(policyId + PROP_FILE);
93 propLocation = this.properties.getProperty(policyId + PROP_FILE);
95 if (propLocation != null) {
96 File fileLocation = new File(propLocation);
97 if (!fileLocation.exists()) {
98 XACMLPdpPolicyFinderFactory.LOGGER.error("Policy file " + fileLocation.getAbsolutePath() + " does not exist.");
99 } else if (!fileLocation.canRead()) {
100 XACMLPdpPolicyFinderFactory.LOGGER.error("Policy file " + fileLocation.getAbsolutePath() + " cannot be read.");
103 XACMLPdpPolicyFinderFactory.LOGGER.info("Loading policy file " + fileLocation);
104 PolicyDef policyDef = DOMPolicyDef.load(fileLocation);
105 if (policyDef != null) {
108 } catch (DOMStructureException ex) {
109 XACMLPdpPolicyFinderFactory.LOGGER.error( XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error loading policy file " + fileLocation.getAbsolutePath() + ": " + ex.getMessage(), ex);
110 return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage());
114 if (this.properties == null) {
115 propLocation = XACMLProperties.getProperty(policyId + PROP_URL);
117 propLocation = this.properties.getProperty(policyId + PROP_URL);
119 if (propLocation != null) {
120 InputStream is = null;
122 URL url = new URL(propLocation);
123 URLConnection urlConnection = url.openConnection();
124 XACMLPdpPolicyFinderFactory.LOGGER.info("Loading policy file " + url.toString());
125 is = urlConnection.getInputStream();
126 PolicyDef policyDef = DOMPolicyDef.load(is);
127 if (policyDef != null) {
130 } catch (MalformedURLException ex) {
131 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Invalid URL " + propLocation + ": " + ex.getMessage(), ex);
132 } catch (IOException ex) {
133 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "IOException opening URL " + propLocation + ": " + ex.getMessage(), ex);
134 } catch (DOMStructureException ex) {
135 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Invalid Policy " + propLocation + ": " + ex.getMessage(), ex);
136 return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage());
141 } catch (IOException e) {
142 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception closing InputStream for GET of url " + propLocation + " : " + e.getMessage() + " (May be memory leak)", e);
148 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"No known location for Policy " + policyId);
153 * Finds the identifiers for all of the policies referenced by the given property name in the
154 * <code>XACMLProperties</code> and loads them using the requested loading method.
156 * @param propertyName the <code>String</code> name of the property containing the list of policy identifiers
157 * @return a <code>List</code> of <code>PolicyDef</code>s loaded from the given property name
159 protected List<PolicyDef> getPolicyDefs(String propertyName) {
160 String policyIds = XACMLProperties.getProperty(propertyName);
161 if (policyIds == null || policyIds.length() == 0) {
165 Iterable<String> policyIdArray = Splitter.on(',').trimResults().omitEmptyStrings().split(policyIds);
166 if (policyIdArray == null) {
170 List<PolicyDef> listPolicyDefs = new ArrayList<>();
171 for (String policyId : policyIdArray) {
172 PolicyDef policyDef = this.loadPolicyDef(policyId);
173 if (policyDef != null) {
174 listPolicyDefs.add(policyDef);
177 return listPolicyDefs;
180 protected synchronized void init() {
181 if (this.needsInit) {
182 if (XACMLPdpPolicyFinderFactory.LOGGER.isDebugEnabled()) {
183 XACMLPdpPolicyFinderFactory.LOGGER.debug("Initializing");
185 this.rootPolicies = this.getPolicyDefs(XACMLProperties.PROP_ROOTPOLICIES);
186 this.referencedPolicies = this.getPolicyDefs(XACMLProperties.PROP_REFERENCEDPOLICIES);
187 if (XACMLPdpPolicyFinderFactory.LOGGER.isDebugEnabled()) {
188 XACMLPdpPolicyFinderFactory.LOGGER.debug("Root Policies: " + this.rootPolicies);
189 XACMLPdpPolicyFinderFactory.LOGGER.debug("Referenced Policies: " + this.referencedPolicies);
191 this.needsInit = false;
196 public PolicyFinder getPolicyFinder() throws FactoryException {
198 // Force using any properties that were passed upon construction
200 return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, this.properties);
204 public PolicyFinder getPolicyFinder(Properties properties) throws FactoryException {
205 return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, properties);