a1b1045eb1f06a5cc8d40e8e04fea30ccc6f9613
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import javax.persistence.EntityNotFoundException;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.Status;
32 import lombok.AllArgsConstructor;
33 import lombok.NonNull;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplateRepository;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplatesRepository;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
46 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
49
50 /**
51  * This class provides information on control loop concepts in the database to callers.
52  */
53 @Service
54 @Transactional
55 @AllArgsConstructor
56 public class ControlLoopProvider {
57
58     private final ControlLoopRepository controlLoopRepository;
59     private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
60     private final ToscaNodeTemplatesRepository toscaNodeTemplatesRepository;
61
62     /**
63      * Get Control Loop.
64      *
65      * @param controlLoopId the ID of the control loop to get
66      * @return the control loop found
67      * @throws PfModelException on errors getting the control loop
68      */
69     @Transactional(readOnly = true)
70     public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
71         try {
72             return controlLoopRepository.getById(controlLoopId.asConceptKey()).toAuthorative();
73         } catch (EntityNotFoundException e) {
74             throw new PfModelException(Status.NOT_FOUND, "ControlLoop not found", e);
75         }
76     }
77
78     /**
79      * Find Control Loop by controlLoopId.
80      *
81      * @param name the name of the control loop to get, null to get all control loops
82      * @param version the version of the control loop to get, null to get all control loops
83      * @return the control loop found
84      * @throws PfModelException on errors getting the control loop
85      */
86     @Transactional(readOnly = true)
87     public Optional<ControlLoop> findControlLoop(@NonNull final String name, @NonNull final String version)
88             throws PfModelException {
89         return findControlLoop(new PfConceptKey(name, version));
90     }
91
92     /**
93      * Find Control Loop by controlLoopId.
94      *
95      * @param controlLoopId the ID of the control loop to get
96      * @return the control loop found
97      * @throws PfModelException on errors getting the control loop
98      */
99     @Transactional(readOnly = true)
100     public Optional<ControlLoop> findControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
101         return findControlLoop(controlLoopId.asConceptKey());
102     }
103
104     private Optional<ControlLoop> findControlLoop(@NonNull final PfConceptKey key) throws PfModelException {
105         try {
106             return controlLoopRepository.findById(key).map(JpaControlLoop::toAuthorative);
107         } catch (IllegalArgumentException e) {
108             throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
109         }
110     }
111
112     /**
113      * Save Control Loop.
114      *
115      * @param controlLoop the control loop to update
116      * @return the updated control loop
117      * @throws PfModelException on errors updating the control loop
118      */
119     public ControlLoop saveControlLoop(final ControlLoop controlLoop) throws PfModelException {
120         try {
121             var result = controlLoopRepository
122                     .save(ProviderUtils.getJpaAndValidate(controlLoop, JpaControlLoop::new, "control loop"));
123
124             // Return the saved participant
125             return result.toAuthorative();
126         } catch (IllegalArgumentException e) {
127             throw new PfModelException(Status.BAD_REQUEST, "Error in save controlLoop", e);
128         }
129     }
130
131     /**
132      * Get All Control Loops.
133      *
134      * @return all control loops found
135      * @throws PfModelException on errors getting control loops
136      */
137     @Transactional(readOnly = true)
138     public List<ControlLoop> getControlLoops() throws PfModelException {
139
140         return ProviderUtils.asEntityList(controlLoopRepository.findAll());
141     }
142
143     /**
144      * Get Control Loops.
145      *
146      * @param name the name of the control loop to get, null to get all control loops
147      * @param version the version of the control loop to get, null to get all control loops
148      * @return the control loops found
149      * @throws PfModelException on errors getting control loops
150      */
151     @Transactional(readOnly = true)
152     public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
153
154         return ProviderUtils.asEntityList(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version));
155     }
156
157     /**
158      * Saves control loops.
159      *
160      * @param controlLoops a specification of the control loops to create
161      * @return the control loops created
162      * @throws PfModelException on errors creating control loops
163      */
164     public List<ControlLoop> saveControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
165         try {
166             var result = controlLoopRepository
167                     .saveAll(ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loops"));
168
169             // Return the saved participant
170             return ProviderUtils.asEntityList(result);
171         } catch (IllegalArgumentException e) {
172             throw new PfModelException(Status.BAD_REQUEST, "Error in save ControlLoops", e);
173         }
174     }
175
176     /**
177      * Saves Instance Properties to the database.
178      *
179      * @param serviceTemplate the service template
180      * @return a Map of tosca node templates
181      */
182     public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
183         Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
184
185         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
186         jpaToscaNodeTemplates.fromAuthorative(List.of(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()));
187
188         toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
189         serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
190
191         return savedNodeTemplates;
192     }
193
194     /**
195      * Delete a control loop.
196      *
197      * @param name the name of the control loop to delete
198      * @param version the version of the control loop to delete
199      * @return the control loop deleted
200      * @throws PfModelException on errors deleting the control loop
201      */
202     public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version)
203             throws PfModelException {
204
205         var controlLoopKey = new PfConceptKey(name, version);
206         var jpaDeleteControlLoop = controlLoopRepository.findById(controlLoopKey);
207
208         if (jpaDeleteControlLoop.isEmpty()) {
209             String errorMessage =
210                     "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
211             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
212         }
213
214         controlLoopRepository.deleteById(controlLoopKey);
215
216         return jpaDeleteControlLoop.get().toAuthorative();
217     }
218
219     /**
220      * Deletes Instance Properties on the database.
221      *
222      * @param filteredToscaNodeTemplateMap filtered node templates map to delete
223      * @param filteredToscaNodeTemplateList filtered node template list to delete
224      */
225     public void deleteInstanceProperties(Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
226             List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
227
228         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
229         jpaToscaNodeTemplates.fromAuthorative(List.of(filteredToscaNodeTemplateMap));
230
231         toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
232
233         filteredToscaNodeTemplateList.forEach(template -> {
234             var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
235
236             toscaNodeTemplateRepository.delete(jpaToscaNodeTemplate);
237         });
238     }
239
240     /**
241      * Get All Node Templates.
242      *
243      * @return the list of node templates found
244      * @throws PfModelException on errors getting node templates
245      */
246     @Transactional(readOnly = true)
247     public List<ToscaNodeTemplate> getAllNodeTemplates() {
248         return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
249     }
250
251     /**
252      * Get Node Templates.
253      *
254      * @param name the name of the node template to get, null to get all node templates
255      * @param version the version of the node template to get, null to get all node templates
256      * @return the node templates found
257      * @throws PfModelException on errors getting node templates
258      */
259     @Transactional(readOnly = true)
260     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
261         return ProviderUtils
262                 .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
263     }
264
265     /**
266      * Get filtered node templates.
267      *
268      * @param filter the filter for the node templates to get
269      * @return the node templates found
270      * @throws PfModelException on errors getting node templates
271      */
272     @Transactional(readOnly = true)
273     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
274             @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
275
276         return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
277                 .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
278     }
279 }