dd669a6d8fe77f72214cb1d6b67ffa6929bbc3a7
[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.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;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * This class provides information on participant concepts in the database to callers.
40  */
41 @Component
42 public class ParticipantProvider extends AbstractModelsProvider {
43     /**
44      * Create a provider for participants.
45      *
46      * @param parameters the parameters for database access
47      * @throws PfModelException on initiation errors
48      */
49     public ParticipantProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
50         super(parameters);
51         this.init();
52     }
53
54     /**
55      * Get participants.
56      *
57      * @param name the name of the participant to get, null to get all participants
58      * @param version the version of the participant to get, null to get all participants
59      * @return the participants found
60      * @throws PfModelException on errors getting participants
61      */
62     public List<Participant> getParticipants(final String name, final String version) throws PfModelException {
63
64         return asParticipantList(getPfDao().getFiltered(JpaParticipant.class, name, version));
65     }
66
67     /**
68      * Get filtered participants.
69      *
70      * @param filter the filter for the participants to get
71      * @return the participants found
72      * @throws PfModelException on errors getting policies
73      */
74     public List<Participant> getFilteredParticipants(@NonNull final ToscaTypedEntityFilter<Participant> filter) {
75
76         return filter.filter(
77                 asParticipantList(getPfDao().getFiltered(JpaParticipant.class, filter.getName(), filter.getVersion())));
78     }
79
80     /**
81      * Creates participants.
82      *
83      * @param participants a specification of the participants to create
84      * @return the participants created
85      * @throws PfModelException on errors creating participants
86      */
87     public List<Participant> createParticipants(@NonNull final List<Participant> participants) throws PfModelException {
88
89         List<JpaParticipant> jpaParticipantList =
90                 ProviderUtils.getJpaAndValidate(participants, JpaParticipant::new, "participant");
91
92         jpaParticipantList.forEach(jpaParticipant -> getPfDao().create(jpaParticipant));
93
94         // Return the created participants
95         List<Participant> returnParticipants = new ArrayList<>(participants.size());
96
97         for (Participant participant : participants) {
98             var jpaParticipant = getPfDao().get(JpaParticipant.class,
99                     new PfConceptKey(participant.getName(), participant.getVersion()));
100             returnParticipants.add(jpaParticipant.toAuthorative());
101         }
102
103         return returnParticipants;
104     }
105
106     /**
107      * Updates participants.
108      *
109      * @param participants a specification of the participants to update
110      * @return the participants updated
111      * @throws PfModelException on errors updating participants
112      */
113     public List<Participant> updateParticipants(@NonNull final List<Participant> participants) throws PfModelException {
114
115         List<JpaParticipant> jpaParticipantList =
116                 ProviderUtils.getJpaAndValidate(participants, JpaParticipant::new, "participant");
117
118         jpaParticipantList.forEach(jpaParticipant -> getPfDao().update(jpaParticipant));
119
120         // Return the created participants
121         List<Participant> returnParticipants = new ArrayList<>(participants.size());
122
123         for (Participant participant : participants) {
124             var jpaParticipant = getPfDao().get(JpaParticipant.class,
125                     new PfConceptKey(participant.getName(), participant.getVersion()));
126             returnParticipants.add(jpaParticipant.toAuthorative());
127         }
128
129         return returnParticipants;
130     }
131
132     /**
133      * Delete a participant.
134      *
135      * @param name the name of the participant to delete
136      * @param version the version of the participant to get
137      * @return the participant deleted
138      * @throws PfModelRuntimeException on errors deleting participants
139      */
140     public Participant deleteParticipant(@NonNull final String name, @NonNull final String version) {
141
142         var participantKey = new PfConceptKey(name, version);
143
144         JpaParticipant jpaDeleteParticipant = getPfDao().get(JpaParticipant.class, participantKey);
145
146         if (jpaDeleteParticipant == null) {
147             String errorMessage =
148                     "delete of participant \"" + participantKey.getId() + "\" failed, participant does not exist";
149             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
150         }
151
152         getPfDao().delete(jpaDeleteParticipant);
153
154         return jpaDeleteParticipant.toAuthorative();
155     }
156
157     /**
158      * Convert JPA participant list to an authorative participant list.
159      *
160      * @param jpaParticipantList the list to convert
161      * @return the authorative list
162      */
163     private List<Participant> asParticipantList(List<JpaParticipant> jpaParticipantList) {
164         return jpaParticipantList.stream().map(JpaParticipant::toAuthorative).collect(Collectors.toList());
165     }
166 }