2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.ws.rs.core.Response;
26 import lombok.NonNull;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.common.parameters.BeanValidationResult;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfKey;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.base.PfReferenceKey;
33 import org.onap.policy.models.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.concepts.PdpGroup;
35 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
36 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
37 import org.onap.policy.models.pdp.enums.PdpState;
38 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
39 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
41 import org.onap.policy.pap.main.repository.PdpGroupRepository;
42 import org.onap.policy.pap.main.repository.PdpRepository;
43 import org.onap.policy.pap.main.repository.PdpSubGroupRepository;
44 import org.springframework.stereotype.Service;
45 import org.springframework.transaction.annotation.Transactional;
49 @RequiredArgsConstructor
50 public class PdpGroupService {
52 private final PdpGroupRepository pdpGroupRepository;
53 private final PdpSubGroupRepository pdpSubGroupRepository;
54 private final PdpRepository pdpRepository;
59 * @return the PDP groups found
61 public List<PdpGroup> getPdpGroups() {
62 return asPdpGroups(pdpGroupRepository.findAll());
66 * Get PDP groups by name.
68 * @param pdpGroup the name of group
69 * @return the PDP groups found
71 public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup) {
72 return asPdpGroups(pdpGroupRepository.findByKeyName(pdpGroup));
76 * Get PDP groups by state.
78 * @param pdpState the state of pdpGroup
79 * @return the PDP groups found
81 public List<PdpGroup> getPdpGroups(@NonNull PdpState pdpState) {
82 return asPdpGroups(pdpGroupRepository.findByPdpGroupState(pdpState));
86 * Get PDP groups by name and state.
88 * @param pdpGroup the name of group
89 * @param state the state of pdpGroup
90 * @return the PDP groups found
92 public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup, @NonNull PdpState state) {
93 return asPdpGroups(pdpGroupRepository.findByKeyNameAndPdpGroupState(pdpGroup, state));
97 * Get filtered PDP groups.
99 * @param filter the filter for the PDP groups to get
100 * @return the PDP groups found
102 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PdpGroupFilter filter) {
103 return filter.filter(asPdpGroups(pdpGroupRepository.findAll()));
107 * Creates PDP groups.
109 * @param pdpGroups the PDP groups to create
110 * @return the PDP groups created
112 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
113 return savePdpGroups(pdpGroups);
117 * Updates PDP groups.
119 * @param pdpGroups the PDP groups to create
120 * @return the PDP groups created
122 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
123 return savePdpGroups(pdpGroups);
126 private List<PdpGroup> savePdpGroups(final List<PdpGroup> pdpGroups) {
127 List<PdpGroup> returnPdpGroupList = new ArrayList<>();
129 for (PdpGroup pdpGroup : pdpGroups) {
130 var jpaPdpGroup = new JpaPdpGroup();
132 jpaPdpGroup.fromAuthorative(pdpGroup);
134 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
135 if (!validationResult.isValid()) {
136 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
139 returnPdpGroupList.add(pdpGroupRepository.save(jpaPdpGroup).toAuthorative());
140 } catch (Exception exc) {
141 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
142 "Failed saving PdpGroup. " + exc.getMessage(), exc);
145 return returnPdpGroupList;
149 * Delete a PDP group.
151 * @param pdpGroup the name of the pdpGroup to delete
153 public void deletePdpGroup(String pdpGroup) {
155 pdpGroupRepository.deleteById(new PfConceptKey(pdpGroup, "0.0.0"));
156 } catch (Exception exc) {
157 String errorMessage = "delete of PDP group \"" + pdpGroup + "\" failed, PDP group does not exist";
158 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);
163 * Convert JPA PDP group list to an authorative PDP group list.
165 * @param jpaPdpGroupList the list to convert
166 * @return the authorative list
168 private List<PdpGroup> asPdpGroups(List<JpaPdpGroup> jpaPdpGroupList) {
169 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
170 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
171 pdpGroupList.add(jpaPdpGroup.toAuthorative());
179 * @param pdpGroupName the name of the PDP group of the PDP subgroup
180 * @param pdpSubGroup the PDP subgroup to be updated
181 * @param pdp the PDP to be updated
183 public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup,
184 @NonNull final Pdp pdp) {
186 final var pdpKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
187 final var jpaPdp = new JpaPdp(pdpKey);
188 jpaPdp.fromAuthorative(pdp);
190 BeanValidationResult validationResult = jpaPdp.validate("PDP");
191 if (!validationResult.isValid()) {
192 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
195 pdpRepository.save(jpaPdp);
199 * Update a PDP subgroup.
201 * @param pdpGroupName the name of the PDP group of the PDP subgroup
202 * @param pdpSubGroup the PDP subgroup to be updated
204 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) {
206 final var subGroupKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
207 final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
208 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
210 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
211 if (!validationResult.isValid()) {
212 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
214 pdpSubGroupRepository.save(jpaPdpSubgroup);