2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.startstop;
23 import java.util.List;
25 import org.onap.policy.common.utils.coder.CoderException;
26 import org.onap.policy.common.utils.coder.StandardCoder;
27 import org.onap.policy.common.utils.resources.ResourceUtils;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.pdp.concepts.PdpGroup;
30 import org.onap.policy.models.pdp.concepts.PdpGroups;
31 import org.onap.policy.models.provider.PolicyModelsProvider;
32 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
34 import org.onap.policy.pap.main.PolicyPapException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This class creates initial PdpGroup/SubGroup in the database.
41 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43 public class PapDatabaseInitializer {
45 private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
47 private StandardCoder standardCoder;
48 private PolicyModelsProviderFactory factory;
51 * Constructs the object.
53 public PapDatabaseInitializer() {
54 factory = new PolicyModelsProviderFactory();
55 standardCoder = new StandardCoder();
59 * Initializes database.
61 * @param policyModelsProviderParameters the database parameters
62 * @throws PolicyPapException in case of errors.
64 public void initializePapDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
65 throws PolicyPapException {
67 try (PolicyModelsProvider databaseProvider =
68 factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
69 final String originalJson = ResourceUtils.getResourceAsString("PapDb.json");
70 final PdpGroups pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class);
71 final List<PdpGroup> pdpGroupsFromDb = databaseProvider.getPdpGroups(
72 pdpGroupsToCreate.getGroups().get(0).getName(), pdpGroupsToCreate.getGroups().get(0).getVersion());
73 if (pdpGroupsFromDb.isEmpty()) {
74 databaseProvider.createPdpGroups(pdpGroupsToCreate.getGroups());
75 LOGGER.debug("Created initial pdpGroup in DB - {}", pdpGroupsToCreate);
77 LOGGER.debug("Initial pdpGroup already exists in DB, skipping create - {}", pdpGroupsFromDb);
79 } catch (final PfModelException | CoderException exp) {
80 throw new PolicyPapException(exp);