Added changes to make the list of preloaded policy types configurable
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / ApiDatabaseInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.startstop;
25
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;
40
41 /**
42  * This class creates initial policy types in the database.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class ApiDatabaseInitializer {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
49
50     private StandardCoder standardCoder;
51     private PolicyModelsProviderFactory factory;
52
53     /**
54      * Constructs the object.
55      */
56     public ApiDatabaseInitializer() {
57         factory = new PolicyModelsProviderFactory();
58         standardCoder = new StandardCoder();
59     }
60
61     /**
62      * Initializes database by preloading policy types.
63      *
64      * @param apiParameterGroup the apiParameterGroup parameters
65      * @throws PolicyApiException in case of errors.
66      */
67     public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup)
68             throws PolicyApiException {
69
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);
79                 }
80
81                 Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
82                 String policyTypeAsString = new StandardCoder().encode(yamlObject);
83
84                 ToscaServiceTemplate singlePolicyType =
85                         standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
86                 if (singlePolicyType == null) {
87                     throw new PolicyApiException("Error deserializing policy type from file: " + pt);
88                 }
89                 // Consolidate policy types
90                 serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
91             }
92             ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
93             if (createdPolicyTypes == null) {
94                 throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
95             } else {
96                 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
97             }
98         } catch (final PfModelException | CoderException exp) {
99             throw new PolicyApiException(exp);
100         }
101     }
102 }