Tie XACML REST Decision
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpApplicationManager.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.ServiceLoader;
29
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;
34
35 public class XacmlPdpApplicationManager {
36     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
37
38     private static boolean needsInit = true;
39     private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
40     private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
41
42     private XacmlPdpApplicationManager() {
43         super();
44     }
45
46     /**
47      * One time to initialize the applications upon startup.
48      */
49     public static void initializeApplications(Path applicationPath) {
50         //
51         // If we have already done this
52         //
53         if (! needsInit) {
54             LOGGER.error("Already initialized the applications");
55             return;
56         }
57         //
58         // Load service
59         //
60         applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
61         //
62         // Iterate through them
63         //
64         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
65         while (iterator.hasNext()) {
66             //
67             // Get the application
68             //
69             XacmlApplicationServiceProvider application = iterator.next();
70             LOGGER.info("Application {} supports {}", application.applicationName(),
71                     application.supportedPolicyTypes());
72             //
73             // Iterate each application
74             //
75             int pathCount = 1;
76             for (String action : application.actionDecisionsSupported()) {
77                 //
78                 // Save the actions that it supports
79                 //
80                 providerActionMap.put(action, application);
81                 //
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
85                 //
86                 //
87                 Path path = Paths.get(applicationPath.toAbsolutePath().toString(),
88                         application.applicationName(), Integer.toString(pathCount++));
89                 //
90                 // Have the application initialize
91                 //
92                 application.initialize(path);
93             }
94         }
95         //
96         // we have initialized
97         //
98         needsInit = false;
99     }
100
101     public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
102         return providerActionMap.get(request.getAction());
103     }
104
105     /**
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.
109      *
110      * @return Total count added from all applications
111      */
112     public static long getPolicyTypeCount() {
113         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
114         long types = 0;
115         while (iterator.hasNext()) {
116             XacmlApplicationServiceProvider application = iterator.next();
117             types += application.supportedPolicyTypes().size();
118         }
119         return types;
120     }
121
122 }