10f2046acde0a105c452ae47a69a384386372a68
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpApplicationManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest;
23
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.ServiceLoader;
33 import java.util.stream.Collectors;
34 import lombok.Getter;
35 import lombok.Setter;
36 import org.onap.policy.common.endpoints.http.client.HttpClient;
37 import org.onap.policy.models.decisions.concepts.DecisionRequest;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
40 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
41 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
42 import org.onap.policy.pdpx.main.parameters.XacmlApplicationParameters;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class XacmlPdpApplicationManager {
47     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
48
49     @Getter
50     @Setter
51     private static XacmlPdpApplicationManager current;
52
53     private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
54     private Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
55     private List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
56     private Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
57
58
59     /**
60      * One time to initialize the applications upon startup.
61      */
62     public XacmlPdpApplicationManager(XacmlApplicationParameters applicationParameters,
63                     HttpClient policyApiClient) {
64         if (LOGGER.isInfoEnabled()) {
65             LOGGER.info("Initialization applications {} {}", applicationParameters, policyApiClient);
66         }
67         //
68         // Load service
69         //
70         applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
71         //
72         // Iterate through the applications for actions and supported policy types
73         //
74         for (XacmlApplicationServiceProvider application : applicationLoader) {
75             if (LOGGER.isInfoEnabled()) {
76                 LOGGER.info("Application {} supports {}", application.applicationName(),
77                     application.supportedPolicyTypes());
78             }
79             //
80             // Check for exclusions
81             //
82             if (applicationParameters.isExcluded(application.getClass().getName())) {
83                 LOGGER.info("excluded {}", application.getClass().getName());
84                 continue;
85             }
86             //
87             // We are not going to make this available unless the application can
88             // install correctly.
89             //
90             var applicationInitialized = false;
91             //
92             // Have it initialize at a path
93             //
94             try {
95                 initializeApplicationPath(Paths.get(applicationParameters.getApplicationPath()), application,
96                                 policyApiClient);
97                 //
98                 // We are initialized
99                 //
100                 applicationInitialized = true;
101             } catch (XacmlApplicationException e) {
102                 LOGGER.error("Failed to initialize path for {}", application.applicationName(), e);
103             }
104             if (applicationInitialized) {
105                 //
106                 // Iterate through the actions and save in the providerActionMap
107                 //
108                 for (String action : application.actionDecisionsSupported()) {
109                     //
110                     // Save the actions that it supports
111                     //
112                     providerActionMap.put(action, application);
113                 }
114                 //
115                 // Add all the supported policy types
116                 //
117                 toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes());
118             }
119         }
120         //
121         // we have initialized
122         //
123         LOGGER.info("Finished applications initialization {}", providerActionMap);
124
125     }
126
127     public XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
128         return providerActionMap.get(request.getAction());
129     }
130
131     public XacmlApplicationServiceProvider findNativeApplication() {
132         return providerActionMap.get("native");
133     }
134
135     /**
136      * getToscaPolicies.
137      *
138      * @return the map containing ToscaPolicies
139      */
140     public Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
141         return mapLoadedPolicies;
142     }
143
144     /**
145      * getToscaPolicyIdentifiers.
146      *
147      * @return list of ToscaPolicyIdentifier
148      */
149     public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers() {
150         //
151         // converting map to return List of ToscaPolicyIdentiers
152         //
153         return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
154     }
155
156     public List<ToscaConceptIdentifier> getToscaPolicyTypeIdents() {
157         return toscaPolicyTypeIdents;
158     }
159
160     /**
161      * Finds the appropriate application and removes the policy.
162      *
163      * @param policy Incoming policy
164      */
165     public void removeUndeployedPolicy(ToscaPolicy policy) {
166
167         for (XacmlApplicationServiceProvider application : applicationLoader) {
168             try {
169                 if (application.unloadPolicy(policy)) {
170                     if (LOGGER.isInfoEnabled()) {
171                         LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
172                             application.applicationName());
173                     }
174                     if (mapLoadedPolicies.remove(policy) == null) {
175                         LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
176                                 mapLoadedPolicies.size());
177                     }
178                 }
179             } catch (XacmlApplicationException e) {
180                 LOGGER.error("Failed to undeploy the Tosca Policy", e);
181             }
182         }
183     }
184
185     /**
186      * Finds the appropriate application and loads the policy, throws an exception if it fails.
187      *
188      * @param policy Incoming policy
189      * @throws XacmlApplicationException if loadPolicy fails
190      */
191     public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
192         for (XacmlApplicationServiceProvider application : applicationLoader) {
193             //
194             // There should be only one application per policytype. We can
195             // put more logic surrounding enforcement of that later. For now,
196             // just use the first one found.
197             //
198             if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
199                 //
200                 // Try to load the policy
201                 //
202                 application.loadPolicy(policy);
203                 mapLoadedPolicies.put(policy, application);
204                 if (LOGGER.isInfoEnabled()) {
205                     LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
206                             application.applicationName());
207                 }
208                 return;
209             }
210         }
211         //
212         // Ideally we shouldn't ever get here if we
213         // are ensuring we are reporting a set of Policy Types and the
214         // pap honors that. The loadPolicy for each application should be
215         // the one throwing exceptions if there are any errors in the policy type.
216         //
217         throw new XacmlApplicationException("Application not found for policy type" + policy.getTypeIdentifier());
218     }
219
220     /**
221      * Returns the current count of policy types supported. This could be misleading a bit
222      * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
223      * well as individual types/versions. Nevertheless useful for debugging and testing.
224      *
225      * @return Total count added from all applications
226      */
227     public long getPolicyTypeCount() {
228         return toscaPolicyTypeIdents.size();
229     }
230
231     /**
232      * Gets the number of policies currently deployed.
233      *
234      * @return the number of policies currently deployed
235      */
236     public int getPolicyCount() {
237         return mapLoadedPolicies.size();
238     }
239
240     private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application,
241                     HttpClient policyApiClient) throws XacmlApplicationException {
242         //
243         // Making an assumption that all application names are unique, and
244         // they can result in a valid directory being created.
245         //
246         var path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
247         if (LOGGER.isInfoEnabled()) {
248             LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
249         }
250         //
251         // Create that the directory if it does not exist. Ideally
252         // this is only for testing, but could be used for production
253         // Probably better to have the docker container and/or helm
254         // scripts setup the local directory.
255         //
256         if (! path.toFile().exists()) {
257             try {
258                 //
259                 // Try to create the directory
260                 //
261                 Files.createDirectory(path);
262             } catch (IOException e) {
263                 throw new XacmlApplicationException("Failed to create application directory " + path.toAbsolutePath(),
264                         e);
265             }
266         }
267         //
268         // Have the application initialize
269         //
270         application.initialize(path, policyApiClient);
271     }
272 }