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.PfConceptKey;
33 import org.onap.policy.models.base.PfKey;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
37 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
42 * This class provides information on control loop concepts in the database to callers.
44 public class ControlLoopProvider extends AbstractModelsProvider {
47 * Create a provider for control loops.
49 * @param parameters the parameters for database access
50 * @throws PfModelException on initiation errors
52 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
60 * @param controlLoopId the ID of the control loop to get
61 * @return the control loop found
62 * @throws PfModelException on errors getting the control loop
64 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
65 JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
67 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
71 * Update Control Loop.
73 * @param controlLoop the control loop to update
74 * @return the updated control loop
75 * @throws PfModelException on errors updating the control loop
77 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
78 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
84 * @param name the name of the control loop to get, null to get all control loops
85 * @param version the version of the control loop to get, null to get all control loops
86 * @return the control loops found
87 * @throws PfModelException on errors getting control loops
89 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
91 return asControlLoopList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
95 * Get filtered control loops.
97 * @param filter the filter for the control loops to get
98 * @return the control loops found
99 * @throws PfModelException on errors getting policies
101 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
103 return filter.filter(asControlLoopList(
104 getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
108 * Creates control loops.
110 * @param controlLoops a specification of the control loops to create
111 * @return the control loops created
112 * @throws PfModelException on errors creating control loops
114 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
116 BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
118 for (ControlLoop controlLoop : controlLoops) {
119 JpaControlLoop jpaControlLoop = new JpaControlLoop();
120 jpaControlLoop.fromAuthorative(controlLoop);
122 validationResult.addResult(jpaControlLoop.validate("control loop"));
125 if (!validationResult.isValid()) {
126 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
129 for (ControlLoop controlLoop : controlLoops) {
130 JpaControlLoop jpaControlLoop = new JpaControlLoop();
131 jpaControlLoop.fromAuthorative(controlLoop);
133 getPfDao().create(jpaControlLoop);
136 // Return the created control loops
137 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
139 for (ControlLoop controlLoop : controlLoops) {
140 JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class,
141 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
142 returnControlLoops.add(jpaControlLoop.toAuthorative());
145 return returnControlLoops;
149 * Updates control loops.
151 * @param controlLoops a specification of the control loops to update
152 * @return the control loops updated
153 * @throws PfModelException on errors updating control loops
155 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
157 BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
159 for (ControlLoop controlLoop : controlLoops) {
160 JpaControlLoop jpaControlLoop = new JpaControlLoop();
161 jpaControlLoop.fromAuthorative(controlLoop);
163 validationResult.addResult(jpaControlLoop.validate("control loop"));
166 if (!validationResult.isValid()) {
167 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
170 // Return the created control loops
171 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
173 for (ControlLoop controlLoop : controlLoops) {
174 JpaControlLoop jpaControlLoop = new JpaControlLoop();
175 jpaControlLoop.fromAuthorative(controlLoop);
177 JpaControlLoop returnJpaControlLoop = getPfDao().update(jpaControlLoop);
178 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
181 return returnControlLoops;
185 * Delete a control loop.
187 * @param name the name of the control loop to delete
188 * @param version the version of the control loop to delete
189 * @return the control loop deleted
190 * @throws PfModelException on errors deleting the control loop
192 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
194 PfConceptKey controlLoopKey = new PfConceptKey(name, version);
196 JpaControlLoop jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
198 if (jpaDeleteControlLoop == null) {
199 String errorMessage =
200 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
201 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
204 getPfDao().delete(jpaDeleteControlLoop);
206 return jpaDeleteControlLoop.toAuthorative();
210 * Convert JPA control loop list to an authorative control loop list.
212 * @param jpaControlLoopList the list to convert
213 * @return the authorative list
215 private List<ControlLoop> asControlLoopList(List<JpaControlLoop> jpaControlLoopList) {
216 return jpaControlLoopList.stream().map(JpaControlLoop::toAuthorative).collect(Collectors.toList());