e82956f9358f69a677df93fd7c2a32f951f323cf
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.models.controlloop.persistence.provider;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
29 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipant;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
35 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
37
38 /**
39  * This class provides information on participant concepts in the database to callers.
40  */
41 public class ParticipantProvider extends AbstractModelsProvider {
42     /**
43      * Create a provider for participants.
44      *
45      * @param parameters the parameters for database access
46      * @throws PfModelException on initiation errors
47      */
48     public ParticipantProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
49         super(parameters);
50         this.init();
51     }
52
53     /**
54      * Get participants.
55      *
56      * @param name the name of the participant to get, null to get all participants
57      * @param version the version of the participant to get, null to get all participants
58      * @return the participants found
59      * @throws PfModelException on errors getting participants
60      */
61     public List<Participant> getParticipants(final String name, final String version) throws PfModelException {
62
63         return asParticipantList(getPfDao().getFiltered(JpaParticipant.class, name, version));
64     }
65
66     /**
67      * Get filtered participants.
68      *
69      * @param filter the filter for the participants to get
70      * @return the participants found
71      * @throws PfModelException on errors getting policies
72      */
73     public List<Participant> getFilteredParticipants(@NonNull final ToscaTypedEntityFilter<Participant> filter) {
74
75         return filter.filter(asParticipantList(
76                 getPfDao().getFiltered(JpaParticipant.class, filter.getName(), filter.getVersion())));
77     }
78
79     /**
80      * Creates participants.
81      *
82      * @param participants a specification of the participants to create
83      * @return the participants created
84      * @throws PfModelException on errors creating participants
85      */
86     public List<Participant> createParticipants(@NonNull final List<Participant> participants) throws PfModelException {
87
88         BeanValidationResult validationResult = new BeanValidationResult("participants", participants);
89
90         for (Participant participant : participants) {
91             JpaParticipant jpaParticipant = new JpaParticipant();
92             jpaParticipant.fromAuthorative(participant);
93
94             validationResult.addResult(jpaParticipant.validate("participant"));
95         }
96
97         if (!validationResult.isValid()) {
98             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
99         }
100
101         for (Participant participant : participants) {
102             JpaParticipant jpaParticipant = new JpaParticipant();
103             jpaParticipant.fromAuthorative(participant);
104
105             getPfDao().create(jpaParticipant);
106         }
107
108         // Return the created participants
109         List<Participant> returnParticipants = new ArrayList<>(participants.size());
110
111         for (Participant participant : participants) {
112             JpaParticipant jpaParticipant = getPfDao().get(JpaParticipant.class,
113                     new PfConceptKey(participant.getName(), participant.getVersion()));
114             returnParticipants.add(jpaParticipant.toAuthorative());
115         }
116
117         return returnParticipants;
118     }
119
120     /**
121      * Updates participants.
122      *
123      * @param participants a specification of the participants to update
124      * @return the participants updated
125      * @throws PfModelException on errors updating participants
126      */
127     public List<Participant> updateParticipants(@NonNull final List<Participant> participants) throws PfModelException {
128
129         BeanValidationResult validationResult = new BeanValidationResult("participants", participants);
130
131         for (Participant participant : participants) {
132             JpaParticipant jpaParticipant = new JpaParticipant();
133             jpaParticipant.fromAuthorative(participant);
134
135             validationResult.addResult(jpaParticipant.validate("participant"));
136         }
137
138         if (!validationResult.isValid()) {
139             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
140         }
141
142         for (Participant participant : participants) {
143             JpaParticipant jpaParticipant = new JpaParticipant();
144             jpaParticipant.fromAuthorative(participant);
145
146             getPfDao().update(jpaParticipant);
147         }
148
149         // Return the created participants
150         List<Participant> returnParticipants = new ArrayList<>(participants.size());
151
152         for (Participant participant : participants) {
153             JpaParticipant jpaParticipant = getPfDao().get(JpaParticipant.class,
154                     new PfConceptKey(participant.getName(), participant.getVersion()));
155             returnParticipants.add(jpaParticipant.toAuthorative());
156         }
157
158         return returnParticipants;
159     }
160
161     /**
162      * Delete a participant.
163      *
164      * @param name the name of the participant to delete
165      * @param version the version of the participant to get
166      * @return the participant deleted
167      * @throws PfModelException on errors deleting participants
168      */
169     public Participant deleteParticipant(@NonNull final String name, @NonNull final String version) {
170
171         PfConceptKey participantKey = new PfConceptKey(name, version);
172
173         JpaParticipant jpaDeleteParticipant = getPfDao().get(JpaParticipant.class, participantKey);
174
175         if (jpaDeleteParticipant == null) {
176             String errorMessage =
177                     "delete of participant \"" + participantKey.getId() + "\" failed, participant does not exist";
178             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
179         }
180
181         getPfDao().delete(jpaDeleteParticipant);
182
183         return jpaDeleteParticipant.toAuthorative();
184     }
185
186     /**
187      * Convert JPA participant list to an authorative participant list.
188      *
189      * @param foundParticipants the list to convert
190      * @return the authorative list
191      */
192     private List<Participant> asParticipantList(List<JpaParticipant> jpaParticipantList) {
193         return jpaParticipantList.stream().map(JpaParticipant::toAuthorative).collect(Collectors.toList());
194     }
195 }