0e06b3b8b0d91f510649630903d3b9041b17bce8
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / impl / XACMLPdpPolicyFinderFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pdp.rest.impl;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import org.openecomp.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;
48
49 public class XACMLPdpPolicyFinderFactory extends PolicyFinderFactory {
50         public static final String      PROP_FILE               = ".file";
51         public static final String      PROP_URL                = ".url";
52         
53         private static Log LOGGER                                                       = LogFactory.getLog(XACMLPdpPolicyFinderFactory.class);
54         private List<PolicyDef> rootPolicies;
55         private List<PolicyDef> referencedPolicies;
56         private boolean needsInit                                       = true;
57         
58         private Properties properties = null;
59         
60         public XACMLPdpPolicyFinderFactory() {
61                 //
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.
64                 //
65                 this.init();
66         }
67
68         public XACMLPdpPolicyFinderFactory(Properties properties) {
69                 //
70                 // Save our properties
71                 //
72                 this.properties = properties;
73                 //
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.
76                 //
77                 this.init();
78         }
79
80         /**
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>.
84          * 
85          * @param policyId the <code>String</code> identifier for the policy
86          * @return a <code>PolicyDef</code> loaded from the given identifier
87          */
88         protected PolicyDef loadPolicyDef(String policyId) {
89                 String propLocation = null;
90                 if (this.properties == null) {
91                         propLocation    = XACMLProperties.getProperty(policyId + PROP_FILE);
92                 } else {
93                         propLocation    = this.properties.getProperty(policyId + PROP_FILE);
94                 }
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.");
101                         } else {
102                                 try {
103                                         XACMLPdpPolicyFinderFactory.LOGGER.info("Loading policy file " + fileLocation);
104                                         PolicyDef policyDef     = DOMPolicyDef.load(fileLocation);
105                                         if (policyDef != null) {
106                                                 return policyDef;
107                                         }
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());
111                                 }
112                         }
113                 }
114                 if (this.properties == null) {
115                         propLocation = XACMLProperties.getProperty(policyId + PROP_URL);
116                 } else {
117                         propLocation = this.properties.getProperty(policyId + PROP_URL);
118                 }
119                 if (propLocation != null) {
120                          InputStream is = null;
121                         try {
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) {
128                                         return policyDef;
129                                 }
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());
137                         } finally {
138                                 if (is != null) {
139                                         try {
140                                                 is.close();
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);
143                                         }
144                                 }
145                         }
146                 }
147                 
148                 XACMLPdpPolicyFinderFactory.LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"No known location for Policy " + policyId);
149                 return null;
150         }
151         
152         /**
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.
155          * 
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
158          */
159         protected List<PolicyDef> getPolicyDefs(String propertyName) {
160                 String policyIds        = XACMLProperties.getProperty(propertyName);
161                 if (policyIds == null || policyIds.length() == 0) {
162                         return null;
163                 }
164                 
165                 Iterable<String> policyIdArray  = Splitter.on(',').trimResults().omitEmptyStrings().split(policyIds);
166                 if (policyIdArray == null) {
167                         return null;
168                 }
169                 
170                 List<PolicyDef> listPolicyDefs  = new ArrayList<PolicyDef>();
171                 for (String policyId : policyIdArray) {
172                         PolicyDef policyDef     = this.loadPolicyDef(policyId); 
173                         if (policyDef != null) {
174                                 listPolicyDefs.add(policyDef);
175                         }
176                 }
177                 return listPolicyDefs;
178         }
179         
180         protected synchronized void init() {
181                 if (this.needsInit) {
182                         if (XACMLPdpPolicyFinderFactory.LOGGER.isDebugEnabled()) {
183                                 XACMLPdpPolicyFinderFactory.LOGGER.debug("Initializing");
184                         }
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);
190                         }
191                         this.needsInit  = false;
192                 }
193         }
194         
195         @Override
196         public PolicyFinder getPolicyFinder() throws FactoryException {
197                 //
198                 // Force using any properties that were passed upon construction
199                 //
200                 return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, this.properties);
201         }
202
203         @Override
204         public PolicyFinder getPolicyFinder(Properties properties) throws FactoryException {
205                 return new StdPolicyFinder(this.rootPolicies, this.referencedPolicies, properties);
206         }
207
208 }