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