2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2023-2024 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
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.pap.main.startstop;
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.Value;
38 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
39 import org.springframework.stereotype.Component;
42 * This class creates initial PdpGroup/SubGroup in the database.
44 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47 @ConditionalOnProperty(value = "db.initialize", havingValue = "true", matchIfMissing = true)
48 public class PapDatabaseInitializer {
50 private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
52 private final StandardCoder standardCoder;
54 private final PdpGroupService pdpGroupService;
56 @Value("${group-config-file:PapDb.json}")
57 private String groupConfigFile;
60 * Constructs the object.
62 public PapDatabaseInitializer(PdpGroupService pdpGroupService) {
63 this.pdpGroupService = pdpGroupService;
64 standardCoder = new StandardCoder();
68 * Initializes database with group information.
70 * @param groupsJson the group file path
71 * @throws PolicyPapException in case of errors.
73 private void initializePapDatabase(String groupsJson) throws PolicyPapException {
76 final var originalJson = ResourceUtils.getResourceAsString(groupsJson);
77 final var pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class);
78 final List<PdpGroup> pdpGroupsFromDb =
79 pdpGroupService.getPdpGroups(pdpGroupsToCreate.getGroups().get(0).getName());
80 if (pdpGroupsFromDb.isEmpty()) {
81 ValidationResult result = pdpGroupsToCreate.validatePapRest();
82 if (!result.isValid()) {
83 throw new PolicyPapException(result.getResult());
85 pdpGroupService.createPdpGroups(pdpGroupsToCreate.getGroups());
86 LOGGER.info("Created initial pdpGroup in DB - {} from {}", pdpGroupsToCreate, groupsJson);
88 LOGGER.info("Initial pdpGroup already exists in DB, skipping create - {} from {}", pdpGroupsFromDb,
91 } catch (final CoderException | RuntimeException exp) {
92 throw new PolicyPapException(exp);
97 * Initializes database with group information.
100 public void loadData() throws PolicyPapException {
101 initializePapDatabase(groupConfigFile);