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.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
35 import org.onap.policy.models.base.PfAuthorative;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
47 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
48 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates;
49 import org.springframework.stereotype.Component;
52 * This class provides information on control loop concepts in the database to callers.
55 public class ControlLoopProvider extends AbstractModelsProvider {
58 * Create a provider for control loops.
60 * @param parameters the parameters for database access
61 * @throws PfModelException on initiation errors
63 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
71 * @param controlLoopId the ID of the control loop to get
72 * @return the control loop found
73 * @throws PfModelException on errors getting the control loop
75 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
76 var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
78 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
82 * Update Control Loop.
84 * @param controlLoop the control loop to update
85 * @return the updated control loop
86 * @throws PfModelException on errors updating the control loop
88 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
89 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
95 * @param name the name of the control loop to get, null to get all control loops
96 * @param version the version of the control loop to get, null to get all control loops
97 * @return the control loops found
98 * @throws PfModelException on errors getting control loops
100 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
102 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
106 * Get filtered control loops.
108 * @param filter the filter for the control loops to get
109 * @return the control loops found
110 * @throws PfModelException on errors getting control loops
112 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
114 return filter.filter(
115 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
119 * Creates control loops.
121 * @param controlLoops a specification of the control loops to create
122 * @return the control loops created
123 * @throws PfModelException on errors creating control loops
125 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
127 List<JpaControlLoop> jpaControlLoopList =
128 ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
130 jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
132 // Return the created control loops
133 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
135 for (ControlLoop controlLoop : controlLoops) {
136 var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
137 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
138 returnControlLoops.add(jpaControlLoop.toAuthorative());
141 return returnControlLoops;
145 * Updates control loops.
147 * @param controlLoops a specification of the control loops to update
148 * @return the control loops updated
149 * @throws PfModelException on errors updating control loops
151 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
153 List<JpaControlLoop> jpaControlLoopList =
154 ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
156 // Return the created control loops
157 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
159 jpaControlLoopList.forEach(jpaControlLoop -> {
160 var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
161 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
164 return returnControlLoops;
168 * Delete a control loop.
170 * @param name the name of the control loop to delete
171 * @param version the version of the control loop to delete
172 * @return the control loop deleted
173 * @throws PfModelRuntimeException on errors deleting the control loop
175 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
177 var controlLoopKey = new PfConceptKey(name, version);
179 var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
181 if (jpaDeleteControlLoop == null) {
182 String errorMessage =
183 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
184 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
187 getPfDao().delete(jpaDeleteControlLoop);
189 return jpaDeleteControlLoop.toAuthorative();
193 * Saves Instance Properties to the database.
194 * @param serviceTemplate the service template
195 * @return a Map of tosca node templates
197 public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
199 Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
201 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
202 jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(serviceTemplate.getToscaTopologyTemplate()
203 .getNodeTemplates()));
205 getPfDao().create(jpaToscaNodeTemplates);
206 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
208 return savedNodeTemplates;
212 * Deletes Instance Properties on the database.
214 * @param filteredToscaNodeTemplateMap filtered node templates map to delete
215 * @param filteredToscaNodeTemplateList filtered node template list to delete
217 public void deleteInstanceProperties(
218 Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
219 List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
221 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
222 jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(filteredToscaNodeTemplateMap));
224 getPfDao().create(jpaToscaNodeTemplates);
226 filteredToscaNodeTemplateList.forEach(template -> {
227 var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
229 getPfDao().delete(jpaToscaNodeTemplate);
234 * Get Node Templates.
236 * @param name the name of the node template to get, null to get all node templates
237 * @param version the version of the node template to get, null to get all node templates
238 * @return the node templates found
239 * @throws PfModelException on errors getting node templates
241 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
242 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
246 * Get filtered node templates.
248 * @param filter the filter for the node templates to get
249 * @return the node templates found
250 * @throws PfModelException on errors getting node templates
252 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
253 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
255 return filter.filter(asEntityList(
256 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
260 * Convert JPA control loop list to an authorative control loop list.
262 * @param <T> the type of TOSCA entity
263 * @param <J> the type of JPA TOSCA entity
264 * @param jpaEntityList the list to convert
265 * @return the authorative list
267 private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
268 return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());