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;
45 * This class provides information on control loop concepts in the database to callers.
47 public class ControlLoopProvider extends AbstractModelsProvider {
50 * Create a provider for control loops.
52 * @param parameters the parameters for database access
53 * @throws PfModelException on initiation errors
55 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
63 * @param controlLoopId the ID of the control loop to get
64 * @return the control loop found
65 * @throws PfModelException on errors getting the control loop
67 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
68 var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
70 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
74 * Update Control Loop.
76 * @param controlLoop the control loop to update
77 * @return the updated control loop
78 * @throws PfModelException on errors updating the control loop
80 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
81 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
87 * @param name the name of the control loop to get, null to get all control loops
88 * @param version the version of the control loop to get, null to get all control loops
89 * @return the control loops found
90 * @throws PfModelException on errors getting control loops
92 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
94 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
98 * Get filtered control loops.
100 * @param filter the filter for the control loops to get
101 * @return the control loops found
102 * @throws PfModelException on errors getting control loops
104 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
106 return filter.filter(
107 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
111 * Creates control loops.
113 * @param controlLoops a specification of the control loops to create
114 * @return the control loops created
115 * @throws PfModelException on errors creating control loops
117 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
119 List<JpaControlLoop> jpaControlLoopList =
120 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
122 jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
124 // Return the created control loops
125 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
127 for (ControlLoop controlLoop : controlLoops) {
128 var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
129 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
130 returnControlLoops.add(jpaControlLoop.toAuthorative());
133 return returnControlLoops;
137 * Updates control loops.
139 * @param controlLoops a specification of the control loops to update
140 * @return the control loops updated
141 * @throws PfModelException on errors updating control loops
143 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
145 List<JpaControlLoop> jpaControlLoopList =
146 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
148 // Return the created control loops
149 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
151 jpaControlLoopList.forEach(jpaControlLoop -> {
152 var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
153 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
156 return returnControlLoops;
160 * Delete a control loop.
162 * @param name the name of the control loop to delete
163 * @param version the version of the control loop to delete
164 * @return the control loop deleted
165 * @throws PfModelRuntimeException on errors deleting the control loop
167 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
169 var controlLoopKey = new PfConceptKey(name, version);
171 var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
173 if (jpaDeleteControlLoop == null) {
174 String errorMessage =
175 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
176 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
179 getPfDao().delete(jpaDeleteControlLoop);
181 return jpaDeleteControlLoop.toAuthorative();
185 * Get Node Templates.
187 * @param name the name of the node template to get, null to get all node templates
188 * @param version the version of the node template to get, null to get all node templates
189 * @return the node templates found
190 * @throws PfModelException on errors getting node templates
192 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
193 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
197 * Get filtered node templates.
199 * @param filter the filter for the node templates to get
200 * @return the node templates found
201 * @throws PfModelException on errors getting node templates
203 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
204 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
206 return filter.filter(asEntityList(
207 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
211 * Convert JPA control loop list to an authorative control loop list.
213 * @param <T> the type of TOSCA entity
214 * @param <J> the type of JPA TOSCA entity
215 * @param jpaEntityList the list to convert
216 * @return the authorative list
218 private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
219 return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());