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.List;
24 import java.util.Optional;
25 import javax.ws.rs.core.Response.Status;
26 import lombok.AllArgsConstructor;
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.clamp.controlloop.models.controlloop.persistence.repository.ParticipantRepository;
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.tosca.authorative.concepts.ToscaTypedEntityFilter;
35 import org.springframework.stereotype.Component;
36 import org.springframework.transaction.annotation.Transactional;
39 * This class provides information on participant concepts in the database to callers.
44 public class ParticipantProvider {
46 private ParticipantRepository participantRepository;
51 * @param name the name of the participant to get, null to get all participants
52 * @param version the version of the participant to get, null to get all participants
53 * @return the participants found
54 * @throws PfModelException on errors getting participants
56 @Transactional(readOnly = true)
57 public List<Participant> getParticipants(final String name, final String version) throws PfModelException {
59 return ProviderUtils.asEntityList(participantRepository.getFiltered(JpaParticipant.class, name, version));
63 * Get all participants.
65 * @return the participants found
66 * @throws PfModelException on errors getting policies
68 @Transactional(readOnly = true)
69 public List<Participant> getParticipants() throws PfModelException {
70 return ProviderUtils.asEntityList(participantRepository.findAll());
76 * @param name the name of the participant to get
77 * @param version the version of the participant to get
78 * @return the participant found
79 * @throws PfModelException on errors getting participant
81 @Transactional(readOnly = true)
82 public Optional<Participant> findParticipant(@NonNull final String name, @NonNull final String version)
83 throws PfModelException {
85 return participantRepository.findById(new PfConceptKey(name, version)).map(JpaParticipant::toAuthorative);
86 } catch (IllegalArgumentException e) {
87 throw new PfModelException(Status.BAD_REQUEST, "Error in find Participant", e);
92 * Get filtered participants.
94 * @param filter the filter for the participants to get
95 * @return the participants found
96 * @throws PfModelException on errors getting policies
98 @Transactional(readOnly = true)
99 public List<Participant> getFilteredParticipants(@NonNull final ToscaTypedEntityFilter<Participant> filter)
100 throws PfModelException {
102 return filter.filter(ProviderUtils.asEntityList(
103 participantRepository.getFiltered(JpaParticipant.class, filter.getName(), filter.getVersion())));
109 * @param participant participant to save
110 * @return the participant created
111 * @throws PfModelException on errors creating participants
113 public Participant saveParticipant(@NonNull final Participant participant) throws PfModelException {
115 var result = participantRepository
116 .save(ProviderUtils.getJpaAndValidate(participant, JpaParticipant::new, "participant"));
118 // Return the saved participant
119 return result.toAuthorative();
120 } catch (IllegalArgumentException e) {
121 throw new PfModelException(Status.BAD_REQUEST, "Error in save Participant", e);
126 * Delete a participant.
128 * @param name the name of the participant to delete
129 * @param version the version of the participant to get
130 * @return the participant deleted
131 * @throws PfModelRuntimeException on errors deleting participants
133 public Participant deleteParticipant(@NonNull final String name, @NonNull final String version)
134 throws PfModelException {
136 var participantKey = new PfConceptKey(name, version);
138 var jpaDeleteParticipantOpt = participantRepository.findById(participantKey);
140 if (jpaDeleteParticipantOpt.isEmpty()) {
141 String errorMessage =
142 "delete of participant \"" + participantKey.getId() + "\" failed, participant does not exist";
143 throw new PfModelRuntimeException(Status.BAD_REQUEST, errorMessage);
145 participantRepository.delete(jpaDeleteParticipantOpt.get());
147 return jpaDeleteParticipantOpt.get().toAuthorative();
148 } catch (IllegalArgumentException e) {
149 throw new PfModelException(Status.BAD_REQUEST, "Error in delete Participant", e);