/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
+import lombok.Getter;
+import lombok.Setter;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.slf4j.Logger;
public class XacmlPdpApplicationManager {
private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
- private static boolean needsInit = true;
- private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
- private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
- private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
- private static List<ToscaPolicyIdentifier> toscaPolicyIdents = new ArrayList<>();
- private static List<ToscaPolicy> toscaPolicies = new ArrayList<>();
- private static Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
+ @Getter
+ @Setter
+ private static XacmlPdpApplicationManager current;
+ private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
+ private Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
+ private List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
+ private Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
- private XacmlPdpApplicationManager() {
- super();
- }
/**
* One time to initialize the applications upon startup.
*/
- public static void initializeApplications(Path applicationPath) {
- //
- // If we have already done this
- //
- if (! needsInit) {
- LOGGER.error("Already initialized the applications");
- return;
+ public XacmlPdpApplicationManager(Path applicationPath, BusTopicParams policyApiParameters) {
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Initialization applications {} {}", applicationPath.toAbsolutePath(), policyApiParameters);
}
//
// Load service
// Iterate through the applications for actions and supported policy types
//
for (XacmlApplicationServiceProvider application : applicationLoader) {
- LOGGER.info("Application {} supports {}", application.applicationName(),
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Application {} supports {}", application.applicationName(),
application.supportedPolicyTypes());
+ }
//
// We are not going to make this available unless the application can
// install correctly.
//
- boolean applicationInitialized = false;
+ var applicationInitialized = false;
//
// Have it initialize at a path
//
try {
- initializeApplicationPath(applicationPath, application);
+ initializeApplicationPath(applicationPath, application, policyApiParameters);
//
// We are initialized
//
//
// we have initialized
//
- needsInit = false;
+ LOGGER.info("Finished applications initialization {}", providerActionMap);
+
}
- public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
+ public XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
return providerActionMap.get(request.getAction());
}
+ public XacmlApplicationServiceProvider findNativeApplication() {
+ return providerActionMap.get("native");
+ }
+
/**
* getToscaPolicies.
*
* @return the map containing ToscaPolicies
*/
- public static Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
+ public Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
return mapLoadedPolicies;
}
*
* @return list of ToscaPolicyIdentifier
*/
- public static List<ToscaPolicyIdentifier> getToscaPolicyIdentifiers() {
+ public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers() {
//
// converting map to return List of ToscaPolicyIdentiers
//
return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
}
- public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
+ public List<ToscaConceptIdentifier> getToscaPolicyTypeIdents() {
return toscaPolicyTypeIdents;
}
*
* @param policy Incoming policy
*/
- public static void removeUndeployedPolicy(ToscaPolicy policy) {
+ public void removeUndeployedPolicy(ToscaPolicy policy) {
for (XacmlApplicationServiceProvider application : applicationLoader) {
try {
if (application.unloadPolicy(policy)) {
- LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
application.applicationName());
+ }
if (mapLoadedPolicies.remove(policy) == null) {
LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
mapLoadedPolicies.size());
}
/**
- * Finds the appropriate application and loads the policy.
+ * Finds the appropriate application and loads the policy, throws an exception if it fails.
*
* @param policy Incoming policy
+ * @throws XacmlApplicationException if loadPolicy fails
*/
- public static void loadDeployedPolicy(ToscaPolicy policy) {
-
+ public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
for (XacmlApplicationServiceProvider application : applicationLoader) {
- try {
+ //
+ // There should be only one application per policytype. We can
+ // put more logic surrounding enforcement of that later. For now,
+ // just use the first one found.
+ //
+ if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
//
- // There should be only one application per policytype. We can
- // put more logic surrounding enforcement of that later. For now,
- // just use the first one found.
+ // Try to load the policy
//
- if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
- if (application.loadPolicy(policy)) {
- LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
- application.applicationName());
- mapLoadedPolicies.put(policy, application);
- }
- return;
+ application.loadPolicy(policy);
+ mapLoadedPolicies.put(policy, application);
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
+ application.applicationName());
}
- } catch (XacmlApplicationException e) {
- LOGGER.error("Failed to load the Tosca Policy", e);
+ return;
}
}
+ //
+ // Ideally we shouldn't ever get here if we
+ // are ensuring we are reporting a set of Policy Types and the
+ // pap honors that. The loadPolicy for each application should be
+ // the one throwing exceptions if there are any errors in the policy type.
+ //
+ throw new XacmlApplicationException("Application not found for policy type" + policy.getTypeIdentifier());
}
/**
*
* @return Total count added from all applications
*/
- public static long getPolicyTypeCount() {
+ public long getPolicyTypeCount() {
long types = 0;
for (XacmlApplicationServiceProvider application : applicationLoader) {
types += application.supportedPolicyTypes().size();
return types;
}
- private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
- throws XacmlApplicationException {
+ /**
+ * Gets the number of policies currently deployed.
+ *
+ * @return the number of policies currently deployed
+ */
+ public int getPolicyCount() {
+ return mapLoadedPolicies.size();
+ }
+
+ private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application,
+ BusTopicParams policyApiParameters) throws XacmlApplicationException {
//
// Making an assumption that all application names are unique, and
// they can result in a valid directory being created.
//
- Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
- LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+ var path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+ }
//
// Create that the directory if it does not exist. Ideally
// this is only for testing, but could be used for production
//
Files.createDirectory(path);
} catch (IOException e) {
- LOGGER.error("Failed to create application directory", e);
+ throw new XacmlApplicationException("Failed to create application directory " + path.toAbsolutePath(),
+ e);
}
}
//
// Have the application initialize
//
- application.initialize(path);
+ application.initialize(path, policyApiParameters);
}
-
}