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.models.base.PfAuthorative;
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.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
43 import org.springframework.stereotype.Component;
46 * This class provides information on control loop concepts in the database to callers.
49 public class ControlLoopProvider extends AbstractModelsProvider {
52 * Create a provider for control loops.
54 * @param parameters the parameters for database access
55 * @throws PfModelException on initiation errors
57 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
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 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
70 var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
72 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
76 * Update Control Loop.
78 * @param controlLoop the control loop to update
79 * @return the updated control loop
80 * @throws PfModelException on errors updating the control loop
82 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
83 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
89 * @param name the name of the control loop to get, null to get all control loops
90 * @param version the version of the control loop to get, null to get all control loops
91 * @return the control loops found
92 * @throws PfModelException on errors getting control loops
94 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
96 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
100 * Get filtered control loops.
102 * @param filter the filter for the control loops to get
103 * @return the control loops found
104 * @throws PfModelException on errors getting control loops
106 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
108 return filter.filter(
109 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
113 * Creates control loops.
115 * @param controlLoops a specification of the control loops to create
116 * @return the control loops created
117 * @throws PfModelException on errors creating control loops
119 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
121 List<JpaControlLoop> jpaControlLoopList =
122 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
124 jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
126 // Return the created control loops
127 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
129 for (ControlLoop controlLoop : controlLoops) {
130 var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
131 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
132 returnControlLoops.add(jpaControlLoop.toAuthorative());
135 return returnControlLoops;
139 * Updates control loops.
141 * @param controlLoops a specification of the control loops to update
142 * @return the control loops updated
143 * @throws PfModelException on errors updating control loops
145 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
147 List<JpaControlLoop> jpaControlLoopList =
148 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
150 // Return the created control loops
151 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
153 jpaControlLoopList.forEach(jpaControlLoop -> {
154 var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
155 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
158 return returnControlLoops;
162 * Delete a control loop.
164 * @param name the name of the control loop to delete
165 * @param version the version of the control loop to delete
166 * @return the control loop deleted
167 * @throws PfModelRuntimeException on errors deleting the control loop
169 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
171 var controlLoopKey = new PfConceptKey(name, version);
173 var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
175 if (jpaDeleteControlLoop == null) {
176 String errorMessage =
177 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
178 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
181 getPfDao().delete(jpaDeleteControlLoop);
183 return jpaDeleteControlLoop.toAuthorative();
187 * Get Node Templates.
189 * @param name the name of the node template to get, null to get all node templates
190 * @param version the version of the node template to get, null to get all node templates
191 * @return the node templates found
192 * @throws PfModelException on errors getting node templates
194 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
195 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
199 * Get filtered node templates.
201 * @param filter the filter for the node templates to get
202 * @return the node templates found
203 * @throws PfModelException on errors getting node templates
205 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
206 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
208 return filter.filter(asEntityList(
209 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
213 * Convert JPA control loop list to an authorative control loop list.
215 * @param <T> the type of TOSCA entity
216 * @param <J> the type of JPA TOSCA entity
217 * @param jpaEntityList the list to convert
218 * @return the authorative list
220 private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
221 return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());