Create spring repository layer for PAP
[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.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;
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> getPdpGroupByName(@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> getPdpGroupByState(@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> getPdpGroupByNameAndState(@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 PdpGroups savePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
114
115         // Return the created PDP groups
116         List<PdpGroup> returnPdpGroupList = new ArrayList<>();
117
118         for (PdpGroup pdpGroup : pdpGroups) {
119             var jpaPdpGroup = new JpaPdpGroup();
120             try {
121                 jpaPdpGroup.fromAuthorative(pdpGroup);
122
123                 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
124                 if (!validationResult.isValid()) {
125                     throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
126                 }
127
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);
132             }
133         }
134         PdpGroups returnPdpGroups = new PdpGroups();
135         returnPdpGroups.setGroups(returnPdpGroupList);
136         return returnPdpGroups;
137     }
138
139     /**
140      * Delete a PDP group.
141      *
142      * @param pdpGroup the name of the pdpGroup to delete
143      */
144     public void deletePdpGroup(String pdpGroup) {
145         try {
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);
150         }
151     }
152
153     /**
154      * Convert JPA PDP group list to an authorative PDP group list.
155      *
156      * @param jpaPdpGroupList the list to convert
157      * @return the authorative list
158      */
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());
163         }
164         return pdpGroupList;
165     }
166
167     /**
168      * Update a PDP.
169      *
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
173      */
174     public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup,
175         @NonNull final Pdp pdp) {
176
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);
180
181         BeanValidationResult validationResult = jpaPdp.validate("PDP");
182         if (!validationResult.isValid()) {
183             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
184         }
185
186         pdpRepository.save(jpaPdp);
187     }
188
189     /**
190      * Update a PDP subgroup.
191      *
192      * @param pdpGroupName the name of the PDP group of the PDP subgroup
193      * @param pdpSubGroup the PDP subgroup to be updated
194      */
195     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) {
196
197         final var subGroupKey = new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
198         final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
199         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
200
201         BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
202         if (!validationResult.isValid()) {
203             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
204         }
205         pdpSubGroupRepository.save(jpaPdpSubgroup);
206     }
207
208 }