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.PdpGroups;
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> getPdpGroupByName(@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> getPdpGroupByState(@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> getPdpGroupByNameAndState(@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 PdpGroups savePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
115 // Return the created PDP groups
116 List<PdpGroup> returnPdpGroupList = new ArrayList<>();
118 for (PdpGroup pdpGroup : pdpGroups) {
119 var jpaPdpGroup = new JpaPdpGroup();
121 jpaPdpGroup.fromAuthorative(pdpGroup);
123 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
124 if (!validationResult.isValid()) {
125 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
128 returnPdpGroupList.add(pdpGroupRepository.save(jpaPdpGroup).toAuthorative());
129 } catch (Exception exc) {
130 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
131 "Failed saving PdpGroup. " + exc.getMessage(), exc);
134 PdpGroups returnPdpGroups = new PdpGroups();
135 returnPdpGroups.setGroups(returnPdpGroupList);
136 return returnPdpGroups;
140 * Delete a PDP group.
142 * @param pdpGroup the name of the pdpGroup to delete
144 public void deletePdpGroup(String pdpGroup) {
146 pdpGroupRepository.deleteById(new PfConceptKey(pdpGroup, "0.0.0"));
147 } catch (Exception exc) {
148 String errorMessage = "delete of PDP group \"" + pdpGroup + "\" failed, PDP group does not exist";
149 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);
154 * Convert JPA PDP group list to an authorative PDP group list.
156 * @param jpaPdpGroupList the list to convert
157 * @return the authorative list
159 private List<PdpGroup> asPdpGroups(List<JpaPdpGroup> jpaPdpGroupList) {
160 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
161 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
162 pdpGroupList.add(jpaPdpGroup.toAuthorative());
170 * @param pdpGroupName the name of the PDP group of the PDP subgroup
171 * @param pdpSubGroup the PDP subgroup to be updated
172 * @param pdp the PDP to be updated
174 public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup,
175 @NonNull final Pdp pdp) {
177 final var pdpKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
178 final var jpaPdp = new JpaPdp(pdpKey);
179 jpaPdp.fromAuthorative(pdp);
181 BeanValidationResult validationResult = jpaPdp.validate("PDP");
182 if (!validationResult.isValid()) {
183 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
186 pdpRepository.save(jpaPdp);
190 * Update a PDP subgroup.
192 * @param pdpGroupName the name of the PDP group of the PDP subgroup
193 * @param pdpSubGroup the PDP subgroup to be updated
195 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) {
197 final var subGroupKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
198 final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
199 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
201 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
202 if (!validationResult.isValid()) {
203 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
205 pdpSubGroupRepository.save(jpaPdpSubgroup);