2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
24 import jakarta.ws.rs.core.Response;
25 import java.util.ArrayList;
26 import java.util.List;
27 import lombok.NonNull;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.common.parameters.BeanValidationResult;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfKey;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.base.PfReferenceKey;
34 import org.onap.policy.models.pdp.concepts.Pdp;
35 import org.onap.policy.models.pdp.concepts.PdpGroup;
36 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
37 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
38 import org.onap.policy.models.pdp.enums.PdpState;
39 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
42 import org.onap.policy.pap.main.repository.PdpGroupRepository;
43 import org.onap.policy.pap.main.repository.PdpRepository;
44 import org.onap.policy.pap.main.repository.PdpSubGroupRepository;
45 import org.springframework.stereotype.Service;
46 import org.springframework.transaction.annotation.Transactional;
50 @RequiredArgsConstructor
51 public class PdpGroupService {
53 private final PdpGroupRepository pdpGroupRepository;
54 private final PdpSubGroupRepository pdpSubGroupRepository;
55 private final PdpRepository pdpRepository;
60 * @return the PDP groups found
62 public List<PdpGroup> getPdpGroups() {
63 return asPdpGroups(pdpGroupRepository.findAll());
67 * Get PDP groups by name.
69 * @param pdpGroup the name of group
70 * @return the PDP groups found
72 public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup) {
73 return asPdpGroups(pdpGroupRepository.findByKeyName(pdpGroup));
77 * Get PDP groups by state.
79 * @param pdpState the state of pdpGroup
80 * @return the PDP groups found
82 public List<PdpGroup> getPdpGroups(@NonNull PdpState pdpState) {
83 return asPdpGroups(pdpGroupRepository.findByPdpGroupState(pdpState));
87 * Get PDP groups by name and state.
89 * @param pdpGroup the name of group
90 * @param state the state of pdpGroup
91 * @return the PDP groups found
93 public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup, @NonNull PdpState state) {
94 return asPdpGroups(pdpGroupRepository.findByKeyNameAndPdpGroupState(pdpGroup, state));
98 * Get filtered PDP groups.
100 * @param filter the filter for the PDP groups to get
101 * @return the PDP groups found
103 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PdpGroupFilter filter) {
104 return filter.filter(asPdpGroups(pdpGroupRepository.findAll()));
108 * Creates PDP groups.
110 * @param pdpGroups the PDP groups to create
111 * @return the PDP groups created
113 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
114 return savePdpGroups(pdpGroups);
118 * Updates PDP groups.
120 * @param pdpGroups the PDP groups to create
121 * @return the PDP groups created
123 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
124 return savePdpGroups(pdpGroups);
127 private List<PdpGroup> savePdpGroups(final List<PdpGroup> pdpGroups) {
128 List<PdpGroup> returnPdpGroupList = new ArrayList<>();
130 for (PdpGroup pdpGroup : pdpGroups) {
131 var jpaPdpGroup = new JpaPdpGroup();
133 jpaPdpGroup.fromAuthorative(pdpGroup);
135 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
136 if (!validationResult.isValid()) {
137 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
140 returnPdpGroupList.add(pdpGroupRepository.save(jpaPdpGroup).toAuthorative());
141 } catch (Exception exc) {
142 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
143 "Failed saving PdpGroup. " + exc.getMessage(), exc);
146 return returnPdpGroupList;
150 * Delete a PDP group.
152 * @param pdpGroup the name of the pdpGroup to delete
154 public void deletePdpGroup(String pdpGroup) {
155 PfConceptKey groupKey = new PfConceptKey(pdpGroup, "0.0.0");
156 if (pdpGroupRepository.existsById(groupKey)) {
157 pdpGroupRepository.deleteById(groupKey);
159 String errorMessage = "delete of PDP group \"" + pdpGroup + "\" failed, PDP group does not exist";
160 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
165 * Convert JPA PDP group list to an authorative PDP group list.
167 * @param jpaPdpGroupList the list to convert
168 * @return the authorative list
170 private List<PdpGroup> asPdpGroups(List<JpaPdpGroup> jpaPdpGroupList) {
171 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
172 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
173 pdpGroupList.add(jpaPdpGroup.toAuthorative());
181 * @param pdpGroupName the name of the PDP group of the PDP subgroup
182 * @param pdpSubGroup the PDP subgroup to be updated
183 * @param pdp the PDP to be updated
185 public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup,
186 @NonNull final Pdp pdp) {
188 final var pdpKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
189 final var jpaPdp = new JpaPdp(pdpKey);
190 jpaPdp.fromAuthorative(pdp);
192 BeanValidationResult validationResult = jpaPdp.validate("PDP");
193 if (!validationResult.isValid()) {
194 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
197 pdpRepository.save(jpaPdp);
201 * Update a PDP subgroup.
203 * @param pdpGroupName the name of the PDP group of the PDP subgroup
204 * @param pdpSubGroup the PDP subgroup to be updated
206 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) {
208 final var subGroupKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
209 final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
210 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
212 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
213 if (!validationResult.isValid()) {
214 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
216 pdpSubGroupRepository.save(jpaPdpSubgroup);