Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / PapDatabaseInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021-2022 Bell Canada. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.startstop;
24
25 import jakarta.annotation.PostConstruct;
26 import java.util.List;
27 import org.onap.policy.common.parameters.ValidationResult;
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.pdp.concepts.PdpGroup;
32 import org.onap.policy.models.pdp.concepts.PdpGroups;
33 import org.onap.policy.pap.main.PolicyPapException;
34 import org.onap.policy.pap.main.service.PdpGroupService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class creates initial PdpGroup/SubGroup in the database.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
46  */
47 @Component
48 @ConditionalOnProperty(value = "db.initialize", havingValue = "true", matchIfMissing = true)
49 public class PapDatabaseInitializer {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
52
53     private final StandardCoder standardCoder;
54
55     @Autowired
56     private PdpGroupService pdpGroupService;
57
58     @Value("${group-config-file:PapDb.json}")
59     private String groupConfigFile;
60
61     /**
62      * Constructs the object.
63      */
64     public PapDatabaseInitializer() {
65         standardCoder = new StandardCoder();
66     }
67
68     /**
69      * Initializes database with group information.
70      *
71      * @param groupsJson the group file path
72      * @throws PolicyPapException in case of errors.
73      */
74     private void initializePapDatabase(String groupsJson) throws PolicyPapException {
75
76         try {
77             final var originalJson = ResourceUtils.getResourceAsString(groupsJson);
78             final var pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class);
79             final List<PdpGroup> pdpGroupsFromDb =
80                 pdpGroupService.getPdpGroups(pdpGroupsToCreate.getGroups().get(0).getName());
81             if (pdpGroupsFromDb.isEmpty()) {
82                 ValidationResult result = pdpGroupsToCreate.validatePapRest();
83                 if (!result.isValid()) {
84                     throw new PolicyPapException(result.getResult());
85                 }
86                 pdpGroupService.createPdpGroups(pdpGroupsToCreate.getGroups());
87                 LOGGER.info("Created initial pdpGroup in DB - {} from {}", pdpGroupsToCreate, groupsJson);
88             } else {
89                 LOGGER.info("Initial pdpGroup already exists in DB, skipping create - {} from {}", pdpGroupsFromDb,
90                     groupsJson);
91             }
92         } catch (final CoderException | RuntimeException exp) {
93             throw new PolicyPapException(exp);
94         }
95     }
96
97     /**
98      * Initializes database with group information.
99      */
100     @PostConstruct
101     public void loadData() throws PolicyPapException {
102         initializePapDatabase(groupConfigFile);
103     }
104 }