Change RestServerParameters to BusTopicParams
[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.event.comm.bus.internal.BusTopicParams;
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.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class XacmlPdpApplicationManager {
46     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
47
48     @Getter
49     @Setter
50     private static XacmlPdpApplicationManager current;
51
52     private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
53     private Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
54     private List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
55     private Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
56
57
58     /**
59      * One time to initialize the applications upon startup.
60      */
61     public XacmlPdpApplicationManager(Path applicationPath, BusTopicParams policyApiParameters) {
62         if (LOGGER.isInfoEnabled()) {
63             LOGGER.info("Initialization applications {} {}", applicationPath.toAbsolutePath(), policyApiParameters);
64         }
65         //
66         // Load service
67         //
68         applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
69         //
70         // Iterate through the applications for actions and supported policy types
71         //
72         for (XacmlApplicationServiceProvider application : applicationLoader) {
73             if (LOGGER.isInfoEnabled()) {
74                 LOGGER.info("Application {} supports {}", application.applicationName(),
75                     application.supportedPolicyTypes());
76             }
77             //
78             // We are not going to make this available unless the application can
79             // install correctly.
80             //
81             boolean applicationInitialized = false;
82             //
83             // Have it initialize at a path
84             //
85             try {
86                 initializeApplicationPath(applicationPath, application, policyApiParameters);
87                 //
88                 // We are initialized
89                 //
90                 applicationInitialized = true;
91             } catch (XacmlApplicationException e) {
92                 LOGGER.error("Failed to initialize path for {}", application.applicationName(), e);
93             }
94             if (applicationInitialized) {
95                 //
96                 // Iterate through the actions and save in the providerActionMap
97                 //
98                 for (String action : application.actionDecisionsSupported()) {
99                     //
100                     // Save the actions that it supports
101                     //
102                     providerActionMap.put(action, application);
103                 }
104                 //
105                 // Add all the supported policy types
106                 //
107                 toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes());
108             }
109         }
110         //
111         // we have initialized
112         //
113         LOGGER.info("Finished applications initialization {}", providerActionMap);
114
115     }
116
117     public XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
118         return providerActionMap.get(request.getAction());
119     }
120
121     public XacmlApplicationServiceProvider findNativeApplication() {
122         return providerActionMap.get("native");
123     }
124
125     /**
126      * getToscaPolicies.
127      *
128      * @return the map containing ToscaPolicies
129      */
130     public Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
131         return mapLoadedPolicies;
132     }
133
134     /**
135      * getToscaPolicyIdentifiers.
136      *
137      * @return list of ToscaPolicyIdentifier
138      */
139     public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers() {
140         //
141         // converting map to return List of ToscaPolicyIdentiers
142         //
143         return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
144     }
145
146     public List<ToscaConceptIdentifier> getToscaPolicyTypeIdents() {
147         return toscaPolicyTypeIdents;
148     }
149
150     /**
151      * Finds the appropriate application and removes the policy.
152      *
153      * @param policy Incoming policy
154      */
155     public void removeUndeployedPolicy(ToscaPolicy policy) {
156
157         for (XacmlApplicationServiceProvider application : applicationLoader) {
158             try {
159                 if (application.unloadPolicy(policy)) {
160                     if (LOGGER.isInfoEnabled()) {
161                         LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
162                             application.applicationName());
163                     }
164                     if (mapLoadedPolicies.remove(policy) == null) {
165                         LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
166                                 mapLoadedPolicies.size());
167                     }
168                 }
169             } catch (XacmlApplicationException e) {
170                 LOGGER.error("Failed to undeploy the Tosca Policy", e);
171             }
172         }
173     }
174
175     /**
176      * Finds the appropriate application and loads the policy, throws an exception if it fails.
177      *
178      * @param policy Incoming policy
179      * @throws XacmlApplicationException if loadPolicy fails
180      */
181     public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
182         for (XacmlApplicationServiceProvider application : applicationLoader) {
183             //
184             // There should be only one application per policytype. We can
185             // put more logic surrounding enforcement of that later. For now,
186             // just use the first one found.
187             //
188             if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
189                 //
190                 // Try to load the policy
191                 //
192                 application.loadPolicy(policy);
193                 mapLoadedPolicies.put(policy, application);
194                 if (LOGGER.isInfoEnabled()) {
195                     LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
196                             application.applicationName());
197                 }
198                 return;
199             }
200         }
201         //
202         // Ideally we shouldn't ever get here if we
203         // are ensuring we are reporting a set of Policy Types and the
204         // pap honors that. The loadPolicy for each application should be
205         // the one throwing exceptions if there are any errors in the policy type.
206         //
207         throw new XacmlApplicationException("Application not found for policy type" + policy.getTypeIdentifier());
208     }
209
210     /**
211      * Returns the current count of policy types supported. This could be misleading a bit
212      * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
213      * well as individual types/versions. Nevertheless useful for debugging and testing.
214      *
215      * @return Total count added from all applications
216      */
217     public long getPolicyTypeCount() {
218         long types = 0;
219         for (XacmlApplicationServiceProvider application : applicationLoader) {
220             types += application.supportedPolicyTypes().size();
221         }
222         return types;
223     }
224
225     /**
226      * Gets the number of policies currently deployed.
227      *
228      * @return the number of policies currently deployed
229      */
230     public int getPolicyCount() {
231         return mapLoadedPolicies.size();
232     }
233
234     private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application,
235                     BusTopicParams policyApiParameters) throws XacmlApplicationException {
236         //
237         // Making an assumption that all application names are unique, and
238         // they can result in a valid directory being created.
239         //
240         Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
241         if (LOGGER.isInfoEnabled()) {
242             LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
243         }
244         //
245         // Create that the directory if it does not exist. Ideally
246         // this is only for testing, but could be used for production
247         // Probably better to have the docker container and/or helm
248         // scripts setup the local directory.
249         //
250         if (! path.toFile().exists()) {
251             try {
252                 //
253                 // Try to create the directory
254                 //
255                 Files.createDirectory(path);
256             } catch (IOException e) {
257                 throw new XacmlApplicationException("Failed to create application directory " + path.toAbsolutePath(),
258                         e);
259             }
260         }
261         //
262         // Have the application initialize
263         //
264         application.initialize(path, policyApiParameters);
265     }
266 }