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
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.clamp.controlloop.models.controlloop.persistence.provider;
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.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
34 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
38 * This class provides information on participant concepts in the database to callers.
40 public class ParticipantProvider extends AbstractModelsProvider {
42 * Create a provider for participants.
44 * @param parameters the parameters for database access
45 * @throws PfModelException on initiation errors
47 public ParticipantProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
55 * @param name the name of the participant to get, null to get all participants
56 * @param version the version of the participant to get, null to get all participants
57 * @return the participants found
58 * @throws PfModelException on errors getting participants
60 public List<Participant> getParticipants(final String name, final String version) throws PfModelException {
62 return asParticipantList(getPfDao().getFiltered(JpaParticipant.class, name, version));
66 * Get filtered participants.
68 * @param filter the filter for the participants to get
69 * @return the participants found
70 * @throws PfModelException on errors getting policies
72 public List<Participant> getFilteredParticipants(@NonNull final ToscaTypedEntityFilter<Participant> filter) {
75 asParticipantList(getPfDao().getFiltered(JpaParticipant.class, filter.getName(), filter.getVersion())));
79 * Creates participants.
81 * @param participants a specification of the participants to create
82 * @return the participants created
83 * @throws PfModelException on errors creating participants
85 public List<Participant> createParticipants(@NonNull final List<Participant> participants) throws PfModelException {
87 List<JpaParticipant> jpaParticipantList =
88 ProviderUtils.getJpaAndValidate(participants, JpaParticipant::new, "participant");
90 jpaParticipantList.forEach(jpaParticipant -> getPfDao().create(jpaParticipant));
92 // Return the created participants
93 List<Participant> returnParticipants = new ArrayList<>(participants.size());
95 for (Participant participant : participants) {
96 var jpaParticipant = getPfDao().get(JpaParticipant.class,
97 new PfConceptKey(participant.getName(), participant.getVersion()));
98 returnParticipants.add(jpaParticipant.toAuthorative());
101 return returnParticipants;
105 * Updates participants.
107 * @param participants a specification of the participants to update
108 * @return the participants updated
109 * @throws PfModelException on errors updating participants
111 public List<Participant> updateParticipants(@NonNull final List<Participant> participants) throws PfModelException {
113 List<JpaParticipant> jpaParticipantList =
114 ProviderUtils.getJpaAndValidate(participants, JpaParticipant::new, "participant");
116 jpaParticipantList.forEach(jpaParticipant -> getPfDao().update(jpaParticipant));
118 // Return the created participants
119 List<Participant> returnParticipants = new ArrayList<>(participants.size());
121 for (Participant participant : participants) {
122 var jpaParticipant = getPfDao().get(JpaParticipant.class,
123 new PfConceptKey(participant.getName(), participant.getVersion()));
124 returnParticipants.add(jpaParticipant.toAuthorative());
127 return returnParticipants;
131 * Delete a participant.
133 * @param name the name of the participant to delete
134 * @param version the version of the participant to get
135 * @return the participant deleted
136 * @throws PfModelRuntimeException on errors deleting participants
138 public Participant deleteParticipant(@NonNull final String name, @NonNull final String version) {
140 var participantKey = new PfConceptKey(name, version);
142 JpaParticipant jpaDeleteParticipant = getPfDao().get(JpaParticipant.class, participantKey);
144 if (jpaDeleteParticipant == null) {
145 String errorMessage =
146 "delete of participant \"" + participantKey.getId() + "\" failed, participant does not exist";
147 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
150 getPfDao().delete(jpaDeleteParticipant);
152 return jpaDeleteParticipant.toAuthorative();
156 * Convert JPA participant list to an authorative participant list.
158 * @param jpaParticipantList the list to convert
159 * @return the authorative list
161 private List<Participant> asParticipantList(List<JpaParticipant> jpaParticipantList) {
162 return jpaParticipantList.stream().map(JpaParticipant::toAuthorative).collect(Collectors.toList());