2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2022 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
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;
32 import java.util.ServiceLoader;
33 import java.util.stream.Collectors;
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;
46 public class XacmlPdpApplicationManager {
47 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
51 private static XacmlPdpApplicationManager current;
53 private final ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
54 private final Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
55 private final List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
56 private final Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
60 * One time to initialize the applications upon startup.
62 public XacmlPdpApplicationManager(XacmlApplicationParameters applicationParameters,
63 HttpClient policyApiClient) {
64 if (LOGGER.isInfoEnabled()) {
65 LOGGER.info("Initialization applications {} {}", applicationParameters, policyApiClient);
70 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
72 // Iterate through the applications for actions and supported policy types
74 for (XacmlApplicationServiceProvider application : applicationLoader) {
75 if (LOGGER.isInfoEnabled()) {
76 LOGGER.info("Application {} supports {}", application.applicationName(),
77 application.supportedPolicyTypes());
80 // Check for exclusions
82 if (applicationParameters.isExcluded(application.getClass().getName())) {
83 LOGGER.info("excluded {}", application.getClass().getName());
87 // We are not going to make this available unless the application can
90 var applicationInitialized = false;
92 // Have it initialize at a path
95 initializeApplicationPath(Paths.get(applicationParameters.getApplicationPath()), application,
100 applicationInitialized = true;
101 } catch (XacmlApplicationException e) {
102 LOGGER.error("Failed to initialize path for {}", application.applicationName(), e);
104 if (applicationInitialized) {
106 // Iterate through the actions and save in the providerActionMap
108 for (String action : application.actionDecisionsSupported()) {
110 // Save the actions that it supports
112 providerActionMap.put(action, application);
115 // Add all the supported policy types
117 toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes());
121 // we have initialized
123 LOGGER.info("Finished applications initialization {}", providerActionMap);
127 public XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
128 return providerActionMap.get(request.getAction());
131 public XacmlApplicationServiceProvider findNativeApplication() {
132 return providerActionMap.get("native");
138 * @return the map containing ToscaPolicies
140 public Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
141 return mapLoadedPolicies;
145 * getToscaPolicyIdentifiers.
147 * @return list of ToscaPolicyIdentifier
149 public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers() {
151 // converting map to return List of ToscaPolicyIdentiers
153 return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
156 public List<ToscaConceptIdentifier> getToscaPolicyTypeIdents() {
157 return toscaPolicyTypeIdents;
161 * Finds the appropriate application and removes the policy.
163 * @param policy Incoming policy
165 public void removeUndeployedPolicy(ToscaPolicy policy) {
167 for (XacmlApplicationServiceProvider application : applicationLoader) {
169 if (application.unloadPolicy(policy)) {
170 if (LOGGER.isInfoEnabled()) {
171 LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
172 application.applicationName());
174 if (mapLoadedPolicies.remove(policy) == null) {
175 LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
176 mapLoadedPolicies.size());
179 } catch (XacmlApplicationException e) {
180 LOGGER.error("Failed to undeploy the Tosca Policy", e);
186 * Finds the appropriate application and loads the policy, throws an exception if it fails.
188 * @param policy Incoming policy
189 * @throws XacmlApplicationException if loadPolicy fails
191 public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
192 for (XacmlApplicationServiceProvider application : applicationLoader) {
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.
198 if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
200 // Try to load the policy
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());
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.
217 throw new XacmlApplicationException("Application not found for policy type" + policy.getTypeIdentifier());
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.
225 * @return Total count added from all applications
227 public long getPolicyTypeCount() {
228 return toscaPolicyTypeIdents.size();
232 * Gets the number of policies currently deployed.
234 * @return the number of policies currently deployed
236 public int getPolicyCount() {
237 return mapLoadedPolicies.size();
240 private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application,
241 HttpClient policyApiClient) throws XacmlApplicationException {
243 // Making an assumption that all application names are unique, and
244 // they can result in a valid directory being created.
246 var path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
247 if (LOGGER.isInfoEnabled()) {
248 LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
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.
256 if (! path.toFile().exists()) {
259 // Try to create the directory
261 Files.createDirectory(path);
262 } catch (IOException e) {
263 throw new XacmlApplicationException("Failed to create application directory " + path.toAbsolutePath(),
268 // Have the application initialize
270 application.initialize(path, policyApiClient);