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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.models.base.PfAuthorative;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
46 * This class provides information on control loop concepts in the database to callers.
48 public class ControlLoopProvider extends AbstractModelsProvider {
51 * Create a provider for control loops.
53 * @param parameters the parameters for database access
54 * @throws PfModelException on initiation errors
56 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
64 * @param controlLoopId the ID of the control loop to get
65 * @return the control loop found
66 * @throws PfModelException on errors getting the control loop
68 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
69 JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
71 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
75 * Update Control Loop.
77 * @param controlLoop the control loop to update
78 * @return the updated control loop
79 * @throws PfModelException on errors updating the control loop
81 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
82 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
88 * @param name the name of the control loop to get, null to get all control loops
89 * @param version the version of the control loop to get, null to get all control loops
90 * @return the control loops found
91 * @throws PfModelException on errors getting control loops
93 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
95 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
99 * Get filtered control loops.
101 * @param filter the filter for the control loops to get
102 * @return the control loops found
103 * @throws PfModelException on errors getting control loops
105 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
107 return filter.filter(
108 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
112 * Creates control loops.
114 * @param controlLoops a specification of the control loops to create
115 * @return the control loops created
116 * @throws PfModelException on errors creating control loops
118 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
120 BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
122 for (ControlLoop controlLoop : controlLoops) {
123 JpaControlLoop jpaControlLoop = new JpaControlLoop();
124 jpaControlLoop.fromAuthorative(controlLoop);
126 validationResult.addResult(jpaControlLoop.validate("control loop"));
129 if (!validationResult.isValid()) {
130 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
133 for (ControlLoop controlLoop : controlLoops) {
134 JpaControlLoop jpaControlLoop = new JpaControlLoop();
135 jpaControlLoop.fromAuthorative(controlLoop);
137 getPfDao().create(jpaControlLoop);
140 // Return the created control loops
141 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
143 for (ControlLoop controlLoop : controlLoops) {
144 JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class,
145 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
146 returnControlLoops.add(jpaControlLoop.toAuthorative());
149 return returnControlLoops;
153 * Updates control loops.
155 * @param controlLoops a specification of the control loops to update
156 * @return the control loops updated
157 * @throws PfModelException on errors updating control loops
159 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
161 BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
163 for (ControlLoop controlLoop : controlLoops) {
164 JpaControlLoop jpaControlLoop = new JpaControlLoop();
165 jpaControlLoop.fromAuthorative(controlLoop);
167 validationResult.addResult(jpaControlLoop.validate("control loop"));
170 if (!validationResult.isValid()) {
171 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
174 // Return the created control loops
175 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
177 for (ControlLoop controlLoop : controlLoops) {
178 JpaControlLoop jpaControlLoop = new JpaControlLoop();
179 jpaControlLoop.fromAuthorative(controlLoop);
181 JpaControlLoop returnJpaControlLoop = getPfDao().update(jpaControlLoop);
182 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
185 return returnControlLoops;
189 * Delete a control loop.
191 * @param name the name of the control loop to delete
192 * @param version the version of the control loop to delete
193 * @return the control loop deleted
194 * @throws PfModelException on errors deleting the control loop
196 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
198 PfConceptKey controlLoopKey = new PfConceptKey(name, version);
200 JpaControlLoop jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
202 if (jpaDeleteControlLoop == null) {
203 String errorMessage =
204 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
205 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
208 getPfDao().delete(jpaDeleteControlLoop);
210 return jpaDeleteControlLoop.toAuthorative();
214 * Get Node Templates.
216 * @param name the name of the node template to get, null to get all node templates
217 * @param version the version of the node template to get, null to get all node templates
218 * @return the node templates found
219 * @throws PfModelException on errors getting node templates
221 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
222 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
226 * Get filtered node templates.
228 * @param filter the filter for the node templates to get
229 * @return the node templates found
230 * @throws PfModelException on errors getting node templates
232 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
233 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
235 return filter.filter(asEntityList(
236 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
240 * Convert JPA control loop list to an authorative control loop list.
242 * @param jpaEntityList the list to convert
243 * @return the authorative list
245 private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
246 return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());