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