2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 java.util.List;
26 import javax.annotation.PostConstruct;
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;
43 * This class creates initial PdpGroup/SubGroup in the database.
45 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48 @ConditionalOnProperty(value = "db.initialize", havingValue = "true", matchIfMissing = true)
49 public class PapDatabaseInitializer {
51 private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
53 private final StandardCoder standardCoder;
56 private PdpGroupService pdpGroupService;
58 @Value("${group-config-file:PapDb.json}")
59 private String groupConfigFile;
62 * Constructs the object.
64 public PapDatabaseInitializer() {
65 standardCoder = new StandardCoder();
69 * Initializes database with group information.
71 * @param groupsJson the group file path
72 * @throws PolicyPapException in case of errors.
74 private void initializePapDatabase(String groupsJson) throws PolicyPapException {
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());
86 pdpGroupService.createPdpGroups(pdpGroupsToCreate.getGroups());
87 LOGGER.info("Created initial pdpGroup in DB - {} from {}", pdpGroupsToCreate, groupsJson);
89 LOGGER.info("Initial pdpGroup already exists in DB, skipping create - {} from {}", pdpGroupsFromDb,
92 } catch (final CoderException | RuntimeException exp) {
93 throw new PolicyPapException(exp);
98 * Initializes database with group information.
101 public void loadData() throws PolicyPapException {
102 initializePapDatabase(groupConfigFile);