Adding code for db config & initial group creation
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / PapDatabaseInitializer.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.startstop;
22
23 import java.util.List;
24
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;
37
38 /**
39  * This class creates initial PdpGroup/SubGroup in the database.
40  *
41  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42  */
43 public class PapDatabaseInitializer {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
46
47     private StandardCoder standardCoder;
48     private PolicyModelsProviderFactory factory;
49
50     /**
51      * Constructs the object.
52      */
53     public PapDatabaseInitializer() {
54         factory = new PolicyModelsProviderFactory();
55         standardCoder = new StandardCoder();
56     }
57
58     /**
59      * Initializes database.
60      *
61      * @param policyModelsProviderParameters the database parameters
62      * @throws PolicyPapException in case of errors.
63      */
64     public void initializePapDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
65             throws PolicyPapException {
66
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);
76             } else {
77                 LOGGER.debug("Initial pdpGroup already exists in DB, skipping create - {}", pdpGroupsFromDb);
78             }
79         } catch (final PfModelException | CoderException exp) {
80             throw new PolicyPapException(exp);
81         }
82     }
83 }