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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
25 import java.util.HashMap;
26 import java.util.List;
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;
51 * This class provides information on control loop concepts in the database to callers.
56 public class ControlLoopProvider {
58 private final ControlLoopRepository controlLoopRepository;
59 private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
60 private final ToscaNodeTemplatesRepository toscaNodeTemplatesRepository;
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
69 @Transactional(readOnly = true)
70 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
72 return controlLoopRepository.getById(controlLoopId.asConceptKey()).toAuthorative();
73 } catch (EntityNotFoundException e) {
74 throw new PfModelException(Status.NOT_FOUND, "ControlLoop not found", e);
79 * Find Control Loop by controlLoopId.
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
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));
93 * Find Control Loop by controlLoopId.
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
99 @Transactional(readOnly = true)
100 public Optional<ControlLoop> findControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
101 return findControlLoop(controlLoopId.asConceptKey());
104 private Optional<ControlLoop> findControlLoop(@NonNull final PfConceptKey key) throws PfModelException {
106 return controlLoopRepository.findById(key).map(JpaControlLoop::toAuthorative);
107 } catch (IllegalArgumentException e) {
108 throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
115 * @param controlLoop the control loop to update
116 * @return the updated control loop
117 * @throws PfModelException on errors updating the control loop
119 public ControlLoop saveControlLoop(final ControlLoop controlLoop) throws PfModelException {
121 var result = controlLoopRepository
122 .save(ProviderUtils.getJpaAndValidate(controlLoop, JpaControlLoop::new, "control loop"));
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);
132 * Get All Control Loops.
134 * @return all control loops found
135 * @throws PfModelException on errors getting control loops
137 @Transactional(readOnly = true)
138 public List<ControlLoop> getControlLoops() throws PfModelException {
140 return ProviderUtils.asEntityList(controlLoopRepository.findAll());
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
151 @Transactional(readOnly = true)
152 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
154 return ProviderUtils.asEntityList(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version));
158 * Saves control loops.
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
164 public List<ControlLoop> saveControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
166 var result = controlLoopRepository
167 .saveAll(ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loops"));
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);
177 * Saves Instance Properties to the database.
179 * @param serviceTemplate the service template
180 * @return a Map of tosca node templates
182 public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
183 Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
185 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
186 jpaToscaNodeTemplates.fromAuthorative(List.of(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()));
188 toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
189 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
191 return savedNodeTemplates;
195 * Delete a control loop.
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
202 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version)
203 throws PfModelException {
205 var controlLoopKey = new PfConceptKey(name, version);
206 var jpaDeleteControlLoop = controlLoopRepository.findById(controlLoopKey);
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);
214 controlLoopRepository.deleteById(controlLoopKey);
216 return jpaDeleteControlLoop.get().toAuthorative();
220 * Deletes Instance Properties on the database.
222 * @param filteredToscaNodeTemplateMap filtered node templates map to delete
223 * @param filteredToscaNodeTemplateList filtered node template list to delete
225 public void deleteInstanceProperties(Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
226 List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
228 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
229 jpaToscaNodeTemplates.fromAuthorative(List.of(filteredToscaNodeTemplateMap));
231 toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
233 filteredToscaNodeTemplateList.forEach(template -> {
234 var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
236 toscaNodeTemplateRepository.delete(jpaToscaNodeTemplate);
241 * Get All Node Templates.
243 * @return the list of node templates found
244 * @throws PfModelException on errors getting node templates
246 @Transactional(readOnly = true)
247 public List<ToscaNodeTemplate> getAllNodeTemplates() {
248 return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
252 * Get Node Templates.
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
259 @Transactional(readOnly = true)
260 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
262 .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
266 * Get filtered node templates.
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
272 @Transactional(readOnly = true)
273 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
274 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
276 return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
277 .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));