aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-PDP-REST/src/main/java/org/openecomp/policy/pdp/rest/impl/XACMLPdpPolicyFinderFactory.java
blob: 9335bef6cc366d45af712b8825386ed1c6375d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-PDP-REST
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.policy.pdp.rest.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.openecomp.policy.xacml.api.XACMLErrorConstants;
import com.att.research.xacml.std.StdStatusCode;
import com.att.research.xacml.std.dom.DOMStructureException;
import com.att.research.xacml.util.FactoryException;
import com.att.research.xacml.util.XACMLProperties;
import com.att.research.xacmlatt.pdp.policy.Policy;
import com.att.research.xacmlatt.pdp.policy.PolicyDef;
import com.att.research.xacmlatt.pdp.policy.PolicyFinder;
import com.att.research.xacmlatt.pdp.policy.PolicyFinderFactory;
import com.att.research.xacmlatt.pdp.policy.dom.DOMPolicyDef;
import com.att.research.xacmlatt.pdp.std.StdPolicyFinder;
import com.google.common.base.Splitter;

public class XACMLPdpPolicyFinderFactory extends PolicyFinderFactory {
	public static final String	PROP_FILE		= ".file";
	public static final String	PROP_URL		= ".url";
	
	private static Log logger							= LogFactory.getLog(XACMLPdpPolicyFinderFactory.class);
	private List<PolicyDef> rootPolicies;
	private List<PolicyDef> referencedPolicies;
	private boolean needsInit					= true;
	
	private Properties properties = null;
	
	public XACMLPdpPolicyFinderFactory() {
		//
		// Here we differ from the StdPolicyFinderFactory in that we initialize right away.
		// We do not wait for a policy request to happen to look for and load policies.
		//
		this.init();
	}

	public XACMLPdpPolicyFinderFactory(Properties properties) {
		//
		// Save our properties
		//
		this.properties = properties;
		//
		// Here we differ from the StdPolicyFinderFactory in that we initialize right away.
		// We do not wait for a policy request to happen to look for and load policies.
		//
		this.init();
	}

	/**
	 * Loads the <code>PolicyDef</code> for the given <code>String</code> identifier by looking first
	 * for a ".file" property associated with the ID and using that to load from a <code>File</code> and
	 * looking for a ".url" property associated with the ID and using that to load from a <code>URL</code>.
	 * 
	 * @param policyId the <code>String</code> identifier for the policy
	 * @return a <code>PolicyDef</code> loaded from the given identifier
	 */
	protected PolicyDef loadPolicyDef(String policyId) {
		String propLocation = null;
		if (this.properties == null) {
			propLocation	= XACMLProperties.getProperty(policyId + PROP_FILE);
		} else {
			propLocation	= this.properties.getProperty(policyId + PROP_FILE);
		}
		if (propLocation != null) {
			File fileLocation	= new File(propLocation);
			if (!fileLocation.exists()) {
				XACMLPdpPolicyFinderFactory.logger.error("Policy file " + fileLocation.getAbsolutePath() + " does not exist.");
			} else if (!fileLocation.canRead()) {
				XACMLPdpPolicyFinderFactory.logger.error("Policy file " + fileLocation.getAbsolutePath() + " cannot be read.");
			} else {
				try {
					XACMLPdpPolicyFinderFactory.logger.info("Loading policy file " + fileLocation);
					PolicyDef policyDef	= DOMPolicyDef.load(fileLocation);
					if (policyDef != null) {
						return policyDef;
					}
				} catch (DOMStructureException ex) {
					XACMLPdpPolicyFinderFactory.logger.error( XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error loading policy file " + fileLocation.getAbsolutePath() + ": " + ex.getMessage(), ex);
					return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage());
				}
			}
		}
		if (this.properties == null) {
			propLocation = XACMLProperties.getProperty(policyId + PROP_URL);
		} else {
			propLocation = this.properties.getProperty(policyId + PROP_URL);
		}
		if (propLocation != null) {
			 InputStream is = null;
			try {
				URL url						= new URL(propLocation);
				URLConnection urlConnection	= url.openConnection();
				XACMLPdpPolicyFinderFactory.logger.info("Loading policy file " + url.toString());
				is = urlConnection.getInputStream();
				PolicyDef policyDef			= DOMPolicyDef.load(is);
				if (policyDef != null) {
					return policyDef;
				}
			} catch (MalformedURLException ex) {
				XACMLPdpPolicyFinderFactory.logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Invalid URL " + propLocation + ": " + ex.getMessage(), ex);
			} catch (IOException ex) {
				XACMLPdpPolicyFinderFactory.logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "IOException opening URL " + propLocation + ": " + ex.getMessage(), ex);
			} catch (DOMStructureException ex) {
				XACMLPdpPolicyFinderFactory.logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Invalid Policy " + propLocation + ": " + ex.getMessage(), ex);
				return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage());
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						XACMLPdpPolicyFinderFactory.logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR  + "Exception closing InputStream for GET of url " + propLocation + " : " + e.getMessage() + "  (May be memory leak)", e);
					}
				}
			}
		}
		
		XACMLPdpPolicyFinderFactory.logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"No known location for Policy " + policyId);
		return null;
	}
	
	/**
	 * Finds the identifiers for all of the policies referenced by the given property name in the
	 * <code>XACMLProperties</code> and loads them using the requested loading method.
	 * 
	 * @param propertyName the <code>String</code> name of the property containing the list of policy identifiers
	 * @return a <code>List</code> of <code>PolicyDef</code>s loaded from the given property name
	 */
	protected List<PolicyDef> getPolicyDefs(String propertyName) {
		String policyIds	= XACMLProperties.getProperty(propertyName);
		if (policyIds == null || policyIds.length() == 0) {
			return null;
		}
		
		Iterable<String> policyIdArray	= Splitter.on(',').trimResults().omitEmptyStrings().split(policyIds);
		if (policyIdArray == null) {
			return null;
		}
		
		List<PolicyDef> listPolicyDefs	= new ArrayList<PolicyDef>();
		for (String policyId : policyIdArray) {
			PolicyDef policyDef	= this.loadPolicyDef(policyId);	
			if (policyDef != null) {
				listPolicyDefs.add(policyDef);
			}
		}
		return listPolicyDefs;
	}
	
	protected synchronized void init() {
		if (this.needsInit) {
			if (XACMLPdpPolicyFinderFactory.logger.isDebugEnabled()) {
				XACMLPdpPolicyFinderFactory.logger.debug("Initializing");
			}
			this.rootPolicies		= this.getPolicyDefs(XACMLProperties.PROP_ROOTPOLICIES);
			this.referencedPolicies	= this.getPolicyDefs(XACMLProperties.PROP_REFERENCEDPOLICIES);
			if (XACMLPdpPolicyFinderFactory.logger.isDebugEnabled()) {
				XACMLPdpPolicyFinderFactory.logger.debug("Root Policies: " + this.rootPolicies);
				XACMLPdpPolicyFinderFactory.logger.debug("Referenced Policies: " + this.referencedPolicies);
			}
			this.needsInit	= false;
		}
	}
	
	@Override
	public PolicyFinder getPolicyFinder() throws FactoryException {
		//
		// Force using any properties that were passed upon construction
		//
		return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, this.properties);
	}

	@Override
	public PolicyFinder getPolicyFinder(Properties properties) throws FactoryException {
		return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, properties);
	}

}