2a6f21c07d8dcaff1961e1fd730a460f389903bb
[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.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;
37
38 /**
39  * This class provides information on participant concepts in the database to callers.
40  */
41 @Transactional
42 @AllArgsConstructor
43 @Component
44 public class ParticipantProvider {
45
46     private ParticipantRepository participantRepository;
47
48     /**
49      * Get participants.
50      *
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
55      */
56     @Transactional(readOnly = true)
57     public List<Participant> getParticipants(final String name, final String version) throws PfModelException {
58
59         return ProviderUtils.asEntityList(participantRepository.getFiltered(JpaParticipant.class, name, version));
60     }
61
62     /**
63      * Get all participants.
64      *
65      * @return the participants found
66      * @throws PfModelException on errors getting policies
67      */
68     @Transactional(readOnly = true)
69     public List<Participant> getParticipants() throws PfModelException {
70         return ProviderUtils.asEntityList(participantRepository.findAll());
71     }
72
73     /**
74      * Get participant.
75      *
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
80      */
81     @Transactional(readOnly = true)
82     public Optional<Participant> findParticipant(@NonNull final String name, @NonNull final String version)
83             throws PfModelException {
84         try {
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);
88         }
89     }
90
91     /**
92      * Get filtered participants.
93      *
94      * @param filter the filter for the participants to get
95      * @return the participants found
96      * @throws PfModelException on errors getting policies
97      */
98     @Transactional(readOnly = true)
99     public List<Participant> getFilteredParticipants(@NonNull final ToscaTypedEntityFilter<Participant> filter)
100             throws PfModelException {
101
102         return filter.filter(ProviderUtils.asEntityList(
103                 participantRepository.getFiltered(JpaParticipant.class, filter.getName(), filter.getVersion())));
104     }
105
106     /**
107      * Saves participant.
108      *
109      * @param participant participant to save
110      * @return the participant created
111      * @throws PfModelException on errors creating participants
112      */
113     public Participant saveParticipant(@NonNull final Participant participant) throws PfModelException {
114         try {
115             var result = participantRepository
116                     .save(ProviderUtils.getJpaAndValidate(participant, JpaParticipant::new, "participant"));
117
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);
122         }
123     }
124
125     /**
126      * Delete a participant.
127      *
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
132      */
133     public Participant deleteParticipant(@NonNull final String name, @NonNull final String version)
134             throws PfModelException {
135         try {
136             var participantKey = new PfConceptKey(name, version);
137
138             var jpaDeleteParticipantOpt = participantRepository.findById(participantKey);
139
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);
144             }
145             participantRepository.delete(jpaDeleteParticipantOpt.get());
146
147             return jpaDeleteParticipantOpt.get().toAuthorative();
148         } catch (IllegalArgumentException e) {
149             throw new PfModelException(Status.BAD_REQUEST, "Error in delete Participant", e);
150         }
151     }
152 }