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 org.onap.policy.models.decisions.concepts.DecisionRequest;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
36 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
37 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class XacmlPdpApplicationManager {
42 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
44 private static boolean needsInit = true;
45 private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
46 private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
47 private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
48 private static List<ToscaPolicyIdentifier> toscaPolicies = new ArrayList<>();
50 private XacmlPdpApplicationManager() {
55 * One time to initialize the applications upon startup.
57 public static void initializeApplications(Path applicationPath) {
59 // If we have already done this
62 LOGGER.error("Already initialized the applications");
68 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
70 // Iterate through the applications for actions and supported policy types
72 for (XacmlApplicationServiceProvider application : applicationLoader) {
73 LOGGER.info("Application {} supports {}", application.applicationName(),
74 application.supportedPolicyTypes());
76 // We are not going to make this available unless the application can
79 boolean applicationInitialized = false;
81 // Have it initialize at a path
84 initializeApplicationPath(applicationPath, application);
88 applicationInitialized = true;
89 } catch (XacmlApplicationException e) {
90 LOGGER.error("Failed to initialize path for {}", application.applicationName(), e);
92 if (applicationInitialized) {
94 // Iterate through the actions and save in the providerActionMap
96 for (String action : application.actionDecisionsSupported()) {
98 // Save the actions that it supports
100 providerActionMap.put(action, application);
103 // Add all the supported policy types
105 toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes());
109 // we have initialized
114 public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
115 return providerActionMap.get(request.getAction());
118 public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
119 return toscaPolicyTypeIdents;
123 * Finds the appropriate application and loads the policy.
125 * @param policy Incoming policy
127 public static void loadDeployedPolicy(ToscaPolicy policy) {
129 for (XacmlApplicationServiceProvider application : applicationLoader) {
132 // There should be only one application per policytype. We can
133 // put more logic surrounding enforcement of that later. For now,
134 // just use the first one found.
136 if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
137 application.loadPolicy(policy);
140 } catch (XacmlApplicationException e) {
141 LOGGER.error("Failed to load the Tosca Policy", e);
146 public static List<ToscaPolicyIdentifier> getToscaPolicies() {
147 return toscaPolicies;
151 * Returns the current count of policy types supported. This could be misleading a bit
152 * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
153 * well as individual types/versions. Nevertheless useful for debugging and testing.
155 * @return Total count added from all applications
157 public static long getPolicyTypeCount() {
159 for (XacmlApplicationServiceProvider application : applicationLoader) {
160 types += application.supportedPolicyTypes().size();
165 private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
166 throws XacmlApplicationException {
168 // Making an assumption that all application names are unique, and
169 // they can result in a valid directory being created.
171 Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
172 LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
174 // Create that the directory if it does not exist. Ideally
175 // this is only for testing, but could be used for production
176 // Probably better to have the docker container and/or helm
177 // scripts setup the local directory.
179 if (! path.toFile().exists()) {
182 // Try to create the directory
184 Files.createDirectory(path);
185 } catch (IOException e) {
186 LOGGER.error("Failed to create application directory", e);
190 // Have the application initialize
192 application.initialize(path);