2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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;
28 import org.onap.policy.api.main.exception.PolicyApiException;
29 import org.onap.policy.api.main.parameters.ApiParameterGroup;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.yaml.snakeyaml.Yaml;
44 * This class creates initial policy types in the database.
46 * @author Chenfei Gao (cgao@research.att.com)
48 public class ApiDatabaseInitializer {
50 private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
52 private StandardCoder standardCoder;
53 private PolicyModelsProviderFactory factory;
56 * Constructs the object.
58 public ApiDatabaseInitializer() {
59 factory = new PolicyModelsProviderFactory();
60 standardCoder = new StandardCoder();
64 * Initializes database by preloading policy types.
66 * @param apiParameterGroup the apiParameterGroup parameters
67 * @throws PolicyApiException in case of errors.
69 public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup) throws PolicyApiException {
71 try (PolicyModelsProvider databaseProvider =
72 factory.createPolicyModelsProvider(apiParameterGroup.getDatabaseProviderParameters())) {
73 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
74 serviceTemplate.setDataTypes(new LinkedHashMap<String, ToscaDataType>());
75 serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
76 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
77 for (String pt : apiParameterGroup.getPreloadPolicyTypes()) {
78 String policyTypeAsStringYaml = ResourceUtils.getResourceAsString(pt);
79 if (policyTypeAsStringYaml == null) {
80 throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
83 Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
84 String policyTypeAsString = new StandardCoder().encode(yamlObject);
86 ToscaServiceTemplate singlePolicyType =
87 standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
88 if (singlePolicyType == null) {
89 throw new PolicyApiException("Error deserializing policy type from file: " + pt);
91 // Consolidate data types and policy types
92 if (singlePolicyType.getDataTypes() != null) {
93 serviceTemplate.getDataTypes().putAll(singlePolicyType.getDataTypes());
95 serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
97 ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
98 if (createdPolicyTypes == null) {
99 throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
101 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
103 } catch (final PfModelException | CoderException exp) {
104 throw new PolicyApiException(exp);