2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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 org.onap.policy.api.main.exception.PolicyApiException;
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.provider.PolicyModelsProvider;
34 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.yaml.snakeyaml.Yaml;
42 * This class creates initial policy types in the database.
44 * @author Chenfei Gao (cgao@research.att.com)
46 public class ApiDatabaseInitializer {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
50 private StandardCoder standardCoder;
51 private PolicyModelsProviderFactory factory;
54 * Constructs the object.
56 public ApiDatabaseInitializer() {
57 factory = new PolicyModelsProviderFactory();
58 standardCoder = new StandardCoder();
62 * Initializes database by preloading policy types.
64 * @param apiParameterGroup the apiParameterGroup parameters
65 * @throws PolicyApiException in case of errors.
67 public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup)
68 throws PolicyApiException {
70 try (PolicyModelsProvider databaseProvider =
71 factory.createPolicyModelsProvider(apiParameterGroup.getDatabaseProviderParameters())) {
72 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
73 serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
74 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
75 for (String pt : apiParameterGroup.getPreloadPolicyTypes()) {
76 String policyTypeAsStringYaml = ResourceUtils.getResourceAsString(pt);
77 if (policyTypeAsStringYaml == null) {
78 throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
81 Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
82 String policyTypeAsString = new StandardCoder().encode(yamlObject);
84 ToscaServiceTemplate singlePolicyType =
85 standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
86 if (singlePolicyType == null) {
87 throw new PolicyApiException("Error deserializing policy type from file: " + pt);
89 // Consolidate policy types
90 serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
92 ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
93 if (createdPolicyTypes == null) {
94 throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
96 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
98 } catch (final PfModelException | CoderException exp) {
99 throw new PolicyApiException(exp);