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.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.common.utils.resources.ResourceUtils;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.provider.PolicyModelsProvider;
33 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
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 private static final String[] PRELOAD_POLICYTYPES = {
55 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
56 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
57 "policytypes/onap.policies.Optimization.yaml",
58 "policytypes/onap.policies.optimization.AffinityPolicy.yaml",
59 "policytypes/onap.policies.optimization.DistancePolicy.yaml",
60 "policytypes/onap.policies.optimization.HpaPolicy.yaml",
61 "policytypes/onap.policies.optimization.OptimizationPolicy.yaml",
62 "policytypes/onap.policies.optimization.PciPolicy.yaml",
63 "policytypes/onap.policies.optimization.QueryPolicy.yaml",
64 "policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
65 "policytypes/onap.policies.optimization.Vim_fit.yaml",
66 "policytypes/onap.policies.optimization.VnfPolicy.yaml",
67 "policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
68 "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
69 "policytypes/onap.policies.controlloop.guard.MinMax.yaml",
70 "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
71 "policytypes/onap.policies.controlloop.Operational.yaml"
76 * Constructs the object.
78 public ApiDatabaseInitializer() {
79 factory = new PolicyModelsProviderFactory();
80 standardCoder = new StandardCoder();
84 * Initializes database by preloading policy types.
86 * @param policyModelsProviderParameters the database parameters
87 * @throws PolicyApiException in case of errors.
89 public void initializeApiDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
90 throws PolicyApiException {
92 try (PolicyModelsProvider databaseProvider =
93 factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
94 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
95 serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
96 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
97 for (String pt : PRELOAD_POLICYTYPES) {
98 String policyTypeAsStringYaml = ResourceUtils.getResourceAsString(pt);
99 if (policyTypeAsStringYaml == null) {
100 throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
103 Object yamlObject = new Yaml().load(policyTypeAsStringYaml);
104 String policyTypeAsString = new StandardCoder().encode(yamlObject);
106 ToscaServiceTemplate singlePolicyType =
107 standardCoder.decode(policyTypeAsString, ToscaServiceTemplate.class);
108 if (singlePolicyType == null) {
109 throw new PolicyApiException("Error deserializing policy type from file: " + pt);
111 // Consolidate policy types
112 serviceTemplate.getPolicyTypes().putAll(singlePolicyType.getPolicyTypes());
114 ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(serviceTemplate);
115 if (createdPolicyTypes == null) {
116 throw new PolicyApiException("Error preloading policy types: " + serviceTemplate);
118 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
120 } catch (final PfModelException | CoderException exp) {
121 throw new PolicyApiException(exp);