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.ToscaPolicyTypeIdentifier;
34 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
35 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class XacmlPdpApplicationManager {
40 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
42 private static boolean needsInit = true;
43 private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
44 private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
45 private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
47 private XacmlPdpApplicationManager() {
52 * One time to initialize the applications upon startup.
54 public static void initializeApplications(Path applicationPath) {
56 // If we have already done this
59 LOGGER.error("Already initialized the applications");
65 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
67 // Iterate through the applications for actions and supported policy types
69 for (XacmlApplicationServiceProvider application : applicationLoader) {
70 LOGGER.info("Application {} supports {}", application.applicationName(),
71 application.supportedPolicyTypes());
73 // We are not going to make this available unless the application can
76 boolean applicationInitialized = false;
78 // Have it initialize at a path
81 initializeApplicationPath(applicationPath, application);
85 applicationInitialized = true;
86 } catch (XacmlApplicationException e) {
87 LOGGER.error("Failed to initialize path for {}", application.applicationName(), e);
89 if (applicationInitialized) {
91 // Iterate through the actions and save in the providerActionMap
93 for (String action : application.actionDecisionsSupported()) {
95 // Save the actions that it supports
97 providerActionMap.put(action, application);
100 // Add all the supported policy types
102 toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes());
106 // we have initialized
111 public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
112 return providerActionMap.get(request.getAction());
115 public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
116 return toscaPolicyTypeIdents;
120 * Returns the current count of policy types supported. This could be misleading a bit
121 * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
122 * well as individual types/versions. Nevertheless useful for debugging and testing.
124 * @return Total count added from all applications
126 public static long getPolicyTypeCount() {
128 for (XacmlApplicationServiceProvider application : applicationLoader) {
129 types += application.supportedPolicyTypes().size();
134 private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
135 throws XacmlApplicationException {
137 // Making an assumption that all application names are unique, and
138 // they can result in a valid directory being created.
140 Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
141 LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
143 // Create that the directory if it does not exist. Ideally
144 // this is only for testing, but could be used for production
145 // Probably better to have the docker container and/or helm
146 // scripts setup the local directory.
148 if (! path.toFile().exists()) {
151 // Try to create the directory
153 Files.createDirectory(path);
154 } catch (IOException e) {
155 LOGGER.error("Failed to create application directory", e);
159 // Have the application initialize
161 application.initialize(path);