Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / PdpGroupService.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
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;
47
48 @Service
49 @Transactional
50 @RequiredArgsConstructor
51 public class PdpGroupService {
52
53     private final PdpGroupRepository pdpGroupRepository;
54     private final PdpSubGroupRepository pdpSubGroupRepository;
55     private final PdpRepository pdpRepository;
56
57     /**
58      * Get all PDP groups.
59      *
60      * @return the PDP groups found
61      */
62     public List<PdpGroup> getPdpGroups() {
63         return asPdpGroups(pdpGroupRepository.findAll());
64     }
65
66     /**
67      * Get PDP groups by name.
68      *
69      * @param pdpGroup the name of group
70      * @return the PDP groups found
71      */
72     public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup) {
73         return asPdpGroups(pdpGroupRepository.findByKeyName(pdpGroup));
74     }
75
76     /**
77      * Get PDP groups by state.
78      *
79      * @param pdpState the state of pdpGroup
80      * @return the PDP groups found
81      */
82     public List<PdpGroup> getPdpGroups(@NonNull PdpState pdpState) {
83         return asPdpGroups(pdpGroupRepository.findByPdpGroupState(pdpState));
84     }
85
86     /**
87      * Get PDP groups by name and state.
88      *
89      * @param pdpGroup the name of group
90      * @param state the state of pdpGroup
91      * @return the PDP groups found
92      */
93     public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup, @NonNull PdpState state) {
94         return asPdpGroups(pdpGroupRepository.findByKeyNameAndPdpGroupState(pdpGroup, state));
95     }
96
97     /**
98      * Get filtered PDP groups.
99      *
100      * @param filter the filter for the PDP groups to get
101      * @return the PDP groups found
102      */
103     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PdpGroupFilter filter) {
104         return filter.filter(asPdpGroups(pdpGroupRepository.findAll()));
105     }
106
107     /**
108      * Creates PDP groups.
109      *
110      * @param pdpGroups the PDP groups to create
111      * @return the PDP groups created
112      */
113     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
114         return savePdpGroups(pdpGroups);
115     }
116
117     /**
118      * Updates PDP groups.
119      *
120      * @param pdpGroups the PDP groups to create
121      * @return the PDP groups created
122      */
123     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
124         return savePdpGroups(pdpGroups);
125     }
126
127     private List<PdpGroup> savePdpGroups(final List<PdpGroup> pdpGroups) {
128         List<PdpGroup> returnPdpGroupList = new ArrayList<>();
129
130         for (PdpGroup pdpGroup : pdpGroups) {
131             var jpaPdpGroup = new JpaPdpGroup();
132             try {
133                 jpaPdpGroup.fromAuthorative(pdpGroup);
134
135                 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
136                 if (!validationResult.isValid()) {
137                     throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
138                 }
139
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);
144             }
145         }
146         return returnPdpGroupList;
147     }
148
149     /**
150      * Delete a PDP group.
151      *
152      * @param pdpGroup the name of the pdpGroup to delete
153      */
154     public void deletePdpGroup(String pdpGroup) {
155         PfConceptKey groupKey = new PfConceptKey(pdpGroup, "0.0.0");
156         if (pdpGroupRepository.existsById(groupKey)) {
157             pdpGroupRepository.deleteById(groupKey);
158         } else {
159             String errorMessage = "delete of PDP group \"" + pdpGroup + "\" failed, PDP group does not exist";
160             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
161         }
162     }
163
164     /**
165      * Convert JPA PDP group list to an authorative PDP group list.
166      *
167      * @param jpaPdpGroupList the list to convert
168      * @return the authorative list
169      */
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());
174         }
175         return pdpGroupList;
176     }
177
178     /**
179      * Update a PDP.
180      *
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
184      */
185     public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup,
186         @NonNull final Pdp pdp) {
187
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);
191
192         BeanValidationResult validationResult = jpaPdp.validate("PDP");
193         if (!validationResult.isValid()) {
194             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
195         }
196
197         pdpRepository.save(jpaPdp);
198     }
199
200     /**
201      * Update a PDP subgroup.
202      *
203      * @param pdpGroupName the name of the PDP group of the PDP subgroup
204      * @param pdpSubGroup the PDP subgroup to be updated
205      */
206     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) {
207
208         final var subGroupKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
209         final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
210         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
211
212         BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
213         if (!validationResult.isValid()) {
214             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
215         }
216         pdpSubGroupRepository.save(jpaPdpSubgroup);
217     }
218
219 }