* ============LICENSE_START=======================================================
* ONAP Policy API
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.api.main.startstop;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
import org.onap.policy.api.main.exception.PolicyApiException;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.yaml.snakeyaml.Yaml;
/**
* This class creates initial policy types in the database.
private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
- private StandardCoder standardCoder;
+ private static final StandardYamlCoder coder = new StandardYamlCoder();
private PolicyModelsProviderFactory factory;
- // @formatter:off
- private static final String[] PRELOAD_POLICYTYPES = {
- "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
- "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
- "policytypes/onap.policies.Optimization.yaml",
- "policytypes/onap.policies.optimization.AffinityPolicy.yaml",
- "policytypes/onap.policies.optimization.DistancePolicy.yaml",
- "policytypes/onap.policies.optimization.HpaPolicy.yaml",
- "policytypes/onap.policies.optimization.OptimizationPolicy.yaml",
- "policytypes/onap.policies.optimization.PciPolicy.yaml",
- "policytypes/onap.policies.optimization.QueryPolicy.yaml",
- "policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
- "policytypes/onap.policies.optimization.Vim_fit.yaml",
- "policytypes/onap.policies.optimization.VnfPolicy.yaml",
- "policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
- "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
- "policytypes/onap.policies.controlloop.guard.MinMax.yaml",
- "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
- "policytypes/onap.policies.controlloop.Operational.yaml",
- "policytypes/onap.policies.Naming.yaml"
- };
- // @formatter:on
-
/**
* Constructs the object.
*/
public ApiDatabaseInitializer() {
factory = new PolicyModelsProviderFactory();
- standardCoder = new StandardCoder();
}
/**
- * Initializes database by preloading policy types.
+ * Initializes database by preloading policy types and policies.
*
- * @param policyModelsProviderParameters the database parameters
+ * @param apiParameterGroup the apiParameterGroup parameters
* @throws PolicyApiException in case of errors.
*/
- public void initializeApiDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
- throws PolicyApiException {
+ public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup) throws PolicyApiException {
try (PolicyModelsProvider databaseProvider =
- factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
+ factory.createPolicyModelsProvider(apiParameterGroup.getDatabaseProviderParameters())) {
+
+ if (alreadyExists(databaseProvider)) {
+ LOGGER.warn("DB already contains policy data - skipping preload");
+ return;
+ }
+
ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
+ serviceTemplate.setDataTypes(new LinkedHashMap<String, ToscaDataType>());
serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
- serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
- for (String pt : PRELOAD_POLICYTYPES) {
- String policyTypeAsStringYaml = ResourceUtils.getResourceAsString(pt);
- if (policyTypeAsStringYaml == null) {
- throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
- }
-
- Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
- String policyTypeAsString = new StandardCoder().encode(yamlObject);
-
- ToscaServiceTemplate singlePolicyType =
- standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
- if (singlePolicyType == null) {
- throw new PolicyApiException("Error deserializing policy type from file: " + pt);
- }
- // Consolidate policy types
- serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
+ serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
+
+ ToscaServiceTemplate createdPolicyTypes = preloadServiceTemplate(serviceTemplate,
+ apiParameterGroup.getPreloadPolicyTypes(), databaseProvider::createPolicyTypes);
+ preloadServiceTemplate(createdPolicyTypes,
+ apiParameterGroup.getPreloadPolicies(), databaseProvider::createPolicies);
+ } catch (final PolicyApiException | PfModelException | CoderException exp) {
+ throw new PolicyApiException(exp);
+ }
+ }
+
+ private boolean alreadyExists(PolicyModelsProvider databaseProvider) throws PfModelException {
+ try {
+ ToscaServiceTemplate serviceTemplate =
+ databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build());
+ if (!serviceTemplate.getPolicyTypes().isEmpty()) {
+ return true;
}
- ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
- if (createdPolicyTypes == null) {
- throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
- } else {
- LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
+
+ } catch (PfModelRuntimeException e) {
+ LOGGER.trace("DB does not yet contain policy types", e);
+ }
+
+ return false;
+ }
+
+ private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate,
+ List<String> entities, FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter)
+ throws PolicyApiException, CoderException, PfModelException {
+
+ for (String entity : entities) {
+ String entityAsStringYaml = ResourceUtils.getResourceAsString(entity);
+ if (entityAsStringYaml == null) {
+ throw new PolicyApiException("Preloading entity cannot be found: " + entity);
+ }
+
+ ToscaServiceTemplate singleEntity =
+ coder.decode(entityAsStringYaml, ToscaServiceTemplate.class);
+ if (singleEntity == null) {
+ throw new PolicyApiException("Error deserializaing entity from file: " + entity);
+ }
+
+ // Consolidate data types and policy types
+ if (singleEntity.getDataTypes() != null) {
+ serviceTemplate.getDataTypes().putAll(singleEntity.getDataTypes());
+ }
+ if (singleEntity.getPolicyTypes() != null) {
+ serviceTemplate.getPolicyTypes().putAll(singleEntity.getPolicyTypes());
+ }
+
+ // Consolidate policies
+ ToscaTopologyTemplate topologyTemplate = singleEntity.getToscaTopologyTemplate();
+ if (topologyTemplate != null && topologyTemplate.getPolicies() != null) {
+ serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
+ serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>());
+ serviceTemplate.getToscaTopologyTemplate().getPolicies()
+ .addAll(singleEntity.getToscaTopologyTemplate().getPolicies());
}
- } catch (final PfModelException | CoderException exp) {
- throw new PolicyApiException(exp);
}
+ // Preload the specified entities
+ ToscaServiceTemplate createdServiceTemplate = getter.apply(serviceTemplate);
+ LOGGER.debug("Created initial tosca service template in DB - {}", createdServiceTemplate);
+ return createdServiceTemplate;
+ }
+
+ @FunctionalInterface
+ protected interface FunctionWithEx<T, R> {
+ public R apply(T value) throws PfModelException;
}
-}
+}
\ No newline at end of file