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.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.HashMap;
26 import java.util.Iterator;
28 import java.util.ServiceLoader;
30 import org.onap.policy.models.decisions.concepts.DecisionRequest;
31 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class XacmlPdpApplicationManager {
36 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
38 private static boolean needsInit = true;
39 private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
40 private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
42 private XacmlPdpApplicationManager() {
47 * One time to initialize the applications upon startup.
49 public static void initializeApplications(Path applicationPath) {
51 // If we have already done this
54 LOGGER.error("Already initialized the applications");
60 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
62 // Iterate through them
64 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
65 while (iterator.hasNext()) {
67 // Get the application
69 XacmlApplicationServiceProvider application = iterator.next();
70 LOGGER.info("Application {} supports {}", application.applicationName(),
71 application.supportedPolicyTypes());
73 // Iterate each application
76 for (String action : application.actionDecisionsSupported()) {
78 // Save the actions that it supports
80 providerActionMap.put(action, application);
82 // Create a unique path for the application to store its data
83 // May need to scan this name to remove unsafe characters etc.
84 // But for debugging purposes, its good to use the application name
87 Path path = Paths.get(applicationPath.toAbsolutePath().toString(),
88 application.applicationName(), Integer.toString(pathCount++));
90 // Have the application initialize
92 application.initialize(path);
96 // we have initialized
101 public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
102 return providerActionMap.get(request.getAction());
106 * Returns the current count of policy types supported. This could be misleading a bit
107 * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
108 * well as individual types/versions. Nevertheless useful for debugging and testing.
110 * @return Total count added from all applications
112 public static long getPolicyTypeCount() {
113 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
115 while (iterator.hasNext()) {
116 XacmlApplicationServiceProvider application = iterator.next();
117 types += application.supportedPolicyTypes().size();