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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
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;
31 import java.util.ServiceLoader;
32 import java.util.stream.Collectors;
33 import org.onap.policy.models.decisions.concepts.DecisionRequest;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
37 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
38 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class XacmlPdpApplicationManager {
43 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
45 private static boolean needsInit = true;
46 private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
47 private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
48 private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
49 private static Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
52 private XacmlPdpApplicationManager() {
57 * One time to initialize the applications upon startup.
59 public static void initializeApplications(Path applicationPath) {
60 if (LOGGER.isInfoEnabled()) {
61 LOGGER.info("Initialization applications {}", applicationPath.toAbsolutePath());
64 // If we have already done this
67 LOGGER.warn("Already initialized the applications {}", providerActionMap);
69 // I had to remove this because the JUnits kept failing - although I probably can
70 // add it back. The main() is hanging around during JUnits and initialization will
78 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
80 // Iterate through the applications for actions and supported policy types
82 for (XacmlApplicationServiceProvider application : applicationLoader) {
83 if (LOGGER.isInfoEnabled()) {
84 LOGGER.info("Application {} supports {}", application.applicationName(),
85 application.supportedPolicyTypes());
88 // We are not going to make this available unless the application can
91 boolean applicationInitialized = false;
93 // Have it initialize at a path
96 initializeApplicationPath(applicationPath, 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
124 LOGGER.info("Finished applications initialization {}", providerActionMap);
128 public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
129 return providerActionMap.get(request.getAction());
135 * @return the map containing ToscaPolicies
137 public static Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
138 return mapLoadedPolicies;
142 * getToscaPolicyIdentifiers.
144 * @return list of ToscaPolicyIdentifier
146 public static List<ToscaPolicyIdentifier> getToscaPolicyIdentifiers() {
148 // converting map to return List of ToscaPolicyIdentiers
150 return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
153 public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
154 return toscaPolicyTypeIdents;
158 * Finds the appropriate application and removes the policy.
160 * @param policy Incoming policy
162 public static void removeUndeployedPolicy(ToscaPolicy policy) {
164 for (XacmlApplicationServiceProvider application : applicationLoader) {
166 if (application.unloadPolicy(policy)) {
167 if (LOGGER.isInfoEnabled()) {
168 LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
169 application.applicationName());
171 if (mapLoadedPolicies.remove(policy) == null) {
172 LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
173 mapLoadedPolicies.size());
176 } catch (XacmlApplicationException e) {
177 LOGGER.error("Failed to undeploy the Tosca Policy", e);
183 * Finds the appropriate application and loads the policy.
185 * @param policy Incoming policy
187 public static void loadDeployedPolicy(ToscaPolicy policy) {
189 for (XacmlApplicationServiceProvider application : applicationLoader) {
192 // There should be only one application per policytype. We can
193 // put more logic surrounding enforcement of that later. For now,
194 // just use the first one found.
196 if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
197 if (application.loadPolicy(policy)) {
198 if (LOGGER.isInfoEnabled()) {
199 LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
200 application.applicationName());
202 mapLoadedPolicies.put(policy, application);
206 } catch (XacmlApplicationException e) {
207 LOGGER.error("Failed to load the Tosca Policy", e);
213 * Returns the current count of policy types supported. This could be misleading a bit
214 * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
215 * well as individual types/versions. Nevertheless useful for debugging and testing.
217 * @return Total count added from all applications
219 public static long getPolicyTypeCount() {
221 for (XacmlApplicationServiceProvider application : applicationLoader) {
222 types += application.supportedPolicyTypes().size();
227 private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
228 throws XacmlApplicationException {
230 // Making an assumption that all application names are unique, and
231 // they can result in a valid directory being created.
233 Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
234 if (LOGGER.isInfoEnabled()) {
235 LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
238 // Create that the directory if it does not exist. Ideally
239 // this is only for testing, but could be used for production
240 // Probably better to have the docker container and/or helm
241 // scripts setup the local directory.
243 if (! path.toFile().exists()) {
246 // Try to create the directory
248 Files.createDirectory(path);
249 } catch (IOException e) {
250 LOGGER.error("Failed to create application directory {}", path.toAbsolutePath().toString(), e);
254 // Have the application initialize
256 application.initialize(path);