2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.startstop;
25 import java.util.ArrayList;
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;
41 * This class creates initial policy types in the database.
43 * @author Chenfei Gao (cgao@research.att.com)
45 public class ApiDatabaseInitializer {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
49 private StandardCoder standardCoder;
50 private PolicyModelsProviderFactory factory;
52 private static final String[] PRELOAD_POLICYTYPES = {
53 "preloadedPolicyTypes/onap.policies.monitoring.cdap.tca.hi.lo.app.json",
54 "preloadedPolicyTypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.json",
55 "preloadedPolicyTypes/onap.policies.optimization.AffinityPolicy.json",
56 "preloadedPolicyTypes/onap.policies.optimization.DistancePolicy.json",
57 "preloadedPolicyTypes/onap.policies.optimization.HpaPolicy.json",
58 "preloadedPolicyTypes/onap.policies.optimization.OptimizationPolicy.json",
59 "preloadedPolicyTypes/onap.policies.optimization.PciPolicy.json",
60 "preloadedPolicyTypes/onap.policies.optimization.QueryPolicy.json",
61 "preloadedPolicyTypes/onap.policies.optimization.SubscriberPolicy.json",
62 "preloadedPolicyTypes/onap.policies.optimization.Vim_fit.json",
63 "preloadedPolicyTypes/onap.policies.optimization.VnfPolicy.json",
64 "preloadedPolicyTypes/onap.policies.controlloop.guard.Blacklist.json",
65 "preloadedPolicyTypes/onap.policies.controlloop.guard.FrequencyLimiter.json",
66 "preloadedPolicyTypes/onap.policies.controlloop.guard.MinMax.json",
67 "preloadedPolicyTypes/onap.policies.controlloop.Operational.json"
71 * Constructs the object.
73 public ApiDatabaseInitializer() {
74 factory = new PolicyModelsProviderFactory();
75 standardCoder = new StandardCoder();
79 * Initializes database by preloading policy types.
81 * @param policyModelsProviderParameters the database parameters
82 * @throws PolicyApiException in case of errors.
84 public void initializeApiDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
85 throws PolicyApiException {
87 try (PolicyModelsProvider databaseProvider =
88 factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
89 ToscaServiceTemplate policyTypes = new ToscaServiceTemplate();
90 policyTypes.setPolicyTypes(new ArrayList<Map<String,ToscaPolicyType>>());
91 policyTypes.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
92 for (String pt : PRELOAD_POLICYTYPES) {
93 String policyTypeAsString = ResourceUtils.getResourceAsString(pt);
94 if (policyTypeAsString == null) {
95 throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
97 ToscaServiceTemplate singlePolicyType = standardCoder.decode(policyTypeAsString,
98 ToscaServiceTemplate.class);
99 if (singlePolicyType == null) {
100 throw new PolicyApiException("Error deserializing policy type from file: " + pt);
102 // Consolidate policy types
103 for (Map<String, ToscaPolicyType> eachPolicyType : singlePolicyType.getPolicyTypes()) {
104 policyTypes.getPolicyTypes().add(eachPolicyType);
107 ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(policyTypes);
108 if (createdPolicyTypes == null) {
109 throw new PolicyApiException("Error preloading policy types: " + policyTypes);
111 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
113 } catch (final PfModelException | CoderException exp) {
114 throw new PolicyApiException(exp);