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.guard.coordination.FirstBlocksSecond.json",
68 "preloadedPolicyTypes/onap.policies.controlloop.Operational.json"
72 * Constructs the object.
74 public ApiDatabaseInitializer() {
75 factory = new PolicyModelsProviderFactory();
76 standardCoder = new StandardCoder();
80 * Initializes database by preloading policy types.
82 * @param policyModelsProviderParameters the database parameters
83 * @throws PolicyApiException in case of errors.
85 public void initializeApiDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
86 throws PolicyApiException {
88 try (PolicyModelsProvider databaseProvider =
89 factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
90 ToscaServiceTemplate policyTypes = new ToscaServiceTemplate();
91 policyTypes.setPolicyTypes(new ArrayList<Map<String,ToscaPolicyType>>());
92 policyTypes.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
93 for (String pt : PRELOAD_POLICYTYPES) {
94 String policyTypeAsString = ResourceUtils.getResourceAsString(pt);
95 if (policyTypeAsString == null) {
96 throw new PolicyApiException("Preloading policy type cannot be found: " + pt);
98 ToscaServiceTemplate singlePolicyType = standardCoder.decode(policyTypeAsString,
99 ToscaServiceTemplate.class);
100 if (singlePolicyType == null) {
101 throw new PolicyApiException("Error deserializing policy type from file: " + pt);
103 // Consolidate policy types
104 for (Map<String, ToscaPolicyType> eachPolicyType : singlePolicyType.getPolicyTypes()) {
105 policyTypes.getPolicyTypes().add(eachPolicyType);
108 ToscaServiceTemplate createdPolicyTypes = databaseProvider.createPolicyTypes(policyTypes);
109 if (createdPolicyTypes == null) {
110 throw new PolicyApiException("Error preloading policy types: " + policyTypes);
112 LOGGER.debug("Created initial policy types in DB - {}", createdPolicyTypes);
114 } catch (final PfModelException | CoderException exp) {
115 throw new PolicyApiException(exp);