Xacml PDP Register/Unregister Changes
[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.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.ServiceLoader;
30 import org.onap.policy.models.decisions.concepts.DecisionRequest;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
32 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class XacmlPdpApplicationManager {
37     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
38
39     private static boolean needsInit = true;
40     private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
41     private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
42     private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
43
44     private XacmlPdpApplicationManager() {
45         super();
46     }
47
48     /**
49      * One time to initialize the applications upon startup.
50      */
51     public static void initializeApplications(Path applicationPath) {
52         //
53         // If we have already done this
54         //
55         if (! needsInit) {
56             LOGGER.error("Already initialized the applications");
57             return;
58         }
59         //
60         // Load service
61         //
62         applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
63
64         //
65         // Iterate through them the applications for actions and supported policy types
66         //
67         for (XacmlApplicationServiceProvider application : applicationLoader) {
68
69             LOGGER.info("Application {} supports {}", application.applicationName(),
70                     application.supportedPolicyTypes());
71
72             //
73             // Iterate through the actions and save in the providerActionMap
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                 Path path = Paths.get(applicationPath.toAbsolutePath().toString(),
87                         application.applicationName(), Integer.toString(pathCount++));
88                 //
89                 // Have the application initialize
90                 //
91                 application.initialize(path);
92             }
93
94             // Get string list of supportedPolicyTypes
95             List<String> supportedPolicyTypes = application.supportedPolicyTypes();
96
97             // Iterate through the supportedPolicyTypes to set the toscaPolicyTypeIdents
98             for (String name : supportedPolicyTypes) {
99                 ToscaPolicyTypeIdentifier ident = new ToscaPolicyTypeIdentifier(name, "1.0.0");
100                 toscaPolicyTypeIdents.add(ident);
101             }
102         }
103         //
104         // we have initialized
105         //
106         needsInit = false;
107     }
108
109     public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
110         return providerActionMap.get(request.getAction());
111     }
112
113     public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
114         return toscaPolicyTypeIdents;
115     }
116
117     /**
118      * Returns the current count of policy types supported. This could be misleading a bit
119      * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
120      * well as individual types/versions. Nevertheless useful for debugging and testing.
121      *
122      * @return Total count added from all applications
123      */
124     public static long getPolicyTypeCount() {
125         long types = 0;
126         for (XacmlApplicationServiceProvider application : applicationLoader) {
127             types += application.supportedPolicyTypes().size();
128         }
129         return types;
130     }
131
132 }