Update API for changes in TOSCA provider
[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-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
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
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;
42
43 /**
44  * This class creates initial policy types in the database.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  */
48 public class ApiDatabaseInitializer {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
51
52     private StandardCoder standardCoder;
53     private PolicyModelsProviderFactory factory;
54
55     /**
56      * Constructs the object.
57      */
58     public ApiDatabaseInitializer() {
59         factory = new PolicyModelsProviderFactory();
60         standardCoder = new StandardCoder();
61     }
62
63     /**
64      * Initializes database by preloading policy types.
65      *
66      * @param apiParameterGroup the apiParameterGroup parameters
67      * @throws PolicyApiException in case of errors.
68      */
69     public void initializeApiDatabase(final ApiParameterGroup apiParameterGroup) throws PolicyApiException {
70
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);
81                 }
82
83                 Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
84                 String policyTypeAsString = new StandardCoder().encode(yamlObject);
85
86                 ToscaServiceTemplate singlePolicyType =
87                         standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
88                 if (singlePolicyType == null) {
89                     throw new PolicyApiException("Error deserializing policy type from file: " + pt);
90                 }
91                 // Consolidate data types and policy types
92                 if (singlePolicyType.getDataTypes() != null) {
93                     serviceTemplate.getDataTypes().putAll(singlePolicyType.getDataTypes());
94                 }
95                 serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
96             }
97             ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
98             if (createdPolicyTypes == null) {
99                 throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
100             } else {
101                 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
102             }
103         } catch (final PfModelException | CoderException exp) {
104             throw new PolicyApiException(exp);
105         }
106     }
107 }