2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.startstop;
26 import java.util.LinkedHashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import org.onap.policy.api.main.exception.PolicyApiException;
30 import org.onap.policy.api.main.parameters.ApiParameterGroup;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.common.utils.coder.StandardYamlCoder;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * This class creates initial policy types in the database.
49 * @author Chenfei Gao (cgao@research.att.com)
51 public class ApiDatabaseInitializer {
53 private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
55 private static final StandardYamlCoder coder = new StandardYamlCoder();
56 private PolicyModelsProviderFactory factory;
59 * Constructs the object.
61 public ApiDatabaseInitializer() {
62 factory = new PolicyModelsProviderFactory();
66 * Initializes database by preloading policy types and policies.
68 * @param apiParameterGroup the apiParameterGroup parameters
69 * @throws PolicyApiException in case of errors.
71 public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup) throws PolicyApiException {
73 try (PolicyModelsProvider databaseProvider =
74 factory.createPolicyModelsProvider(apiParameterGroup.getDatabaseProviderParameters())) {
76 if (alreadyExists(databaseProvider)) {
77 LOGGER.warn("DB already contains policy data - skipping preload");
81 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
82 serviceTemplate.setDataTypes(new LinkedHashMap<String, ToscaDataType>());
83 serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
84 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
86 ToscaServiceTemplate createdPolicyTypes = preloadServiceTemplate(serviceTemplate,
87 apiParameterGroup.getPreloadPolicyTypes(), databaseProvider::createPolicyTypes);
88 preloadServiceTemplate(createdPolicyTypes,
89 apiParameterGroup.getPreloadPolicies(), databaseProvider::createPolicies);
90 } catch (final PolicyApiException | PfModelException | CoderException exp) {
91 throw new PolicyApiException(exp);
95 private boolean alreadyExists(PolicyModelsProvider databaseProvider) throws PfModelException {
97 ToscaServiceTemplate serviceTemplate =
98 databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build());
99 if (!serviceTemplate.getPolicyTypes().isEmpty()) {
103 } catch (PfModelRuntimeException e) {
104 LOGGER.trace("DB does not yet contain policy types", e);
110 private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate,
111 List<String> entities, FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter)
112 throws PolicyApiException, CoderException, PfModelException {
114 for (String entity : entities) {
115 String entityAsStringYaml = ResourceUtils.getResourceAsString(entity);
116 if (entityAsStringYaml == null) {
117 throw new PolicyApiException("Preloading entity cannot be found: " + entity);
120 ToscaServiceTemplate singleEntity =
121 coder.decode(entityAsStringYaml, ToscaServiceTemplate.class);
122 if (singleEntity == null) {
123 throw new PolicyApiException("Error deserializaing entity from file: " + entity);
126 // Consolidate data types and policy types
127 if (singleEntity.getDataTypes() != null) {
128 serviceTemplate.getDataTypes().putAll(singleEntity.getDataTypes());
130 if (singleEntity.getPolicyTypes() != null) {
131 serviceTemplate.getPolicyTypes().putAll(singleEntity.getPolicyTypes());
134 // Consolidate policies
135 ToscaTopologyTemplate topologyTemplate = singleEntity.getToscaTopologyTemplate();
136 if (topologyTemplate != null && topologyTemplate.getPolicies() != null) {
137 serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
138 serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>());
139 serviceTemplate.getToscaTopologyTemplate().getPolicies()
140 .addAll(singleEntity.getToscaTopologyTemplate().getPolicies());
143 // Preload the specified entities
144 ToscaServiceTemplate createdServiceTemplate = getter.apply(serviceTemplate);
145 LOGGER.debug("Created initial tosca service template in DB - {}", createdServiceTemplate);
146 return createdServiceTemplate;
150 protected interface FunctionWithEx<T, R> {
151 public R apply(T value) throws PfModelException;