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.HashMap;
26 import java.util.List;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
32 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
33 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
34 import org.onap.policy.models.base.PfAuthorative;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
40 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
46 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
47 import org.springframework.stereotype.Component;
50 * This class provides information on control loop concepts in the database to callers.
53 public class ControlLoopProvider extends AbstractModelsProvider {
56 * Create a provider for control loops.
58 * @param parameters the parameters for database access
59 * @throws PfModelException on initiation errors
61 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
69 * @param controlLoopId the ID of the control loop to get
70 * @return the control loop found
71 * @throws PfModelException on errors getting the control loop
73 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
74 var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
76 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
80 * Update Control Loop.
82 * @param controlLoop the control loop to update
83 * @return the updated control loop
84 * @throws PfModelException on errors updating the control loop
86 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
87 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
93 * @param name the name of the control loop to get, null to get all control loops
94 * @param version the version of the control loop to get, null to get all control loops
95 * @return the control loops found
96 * @throws PfModelException on errors getting control loops
98 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
100 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
104 * Get filtered control loops.
106 * @param filter the filter for the control loops to get
107 * @return the control loops found
108 * @throws PfModelException on errors getting control loops
110 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
112 return filter.filter(
113 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
117 * Creates control loops.
119 * @param controlLoops a specification of the control loops to create
120 * @return the control loops created
121 * @throws PfModelException on errors creating control loops
123 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
125 List<JpaControlLoop> jpaControlLoopList =
126 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
128 jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
130 // Return the created control loops
131 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
133 for (ControlLoop controlLoop : controlLoops) {
134 var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
135 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
136 returnControlLoops.add(jpaControlLoop.toAuthorative());
139 return returnControlLoops;
143 * Updates control loops.
145 * @param controlLoops a specification of the control loops to update
146 * @return the control loops updated
147 * @throws PfModelException on errors updating control loops
149 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
151 List<JpaControlLoop> jpaControlLoopList =
152 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
154 // Return the created control loops
155 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
157 jpaControlLoopList.forEach(jpaControlLoop -> {
158 var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
159 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
162 return returnControlLoops;
166 * Delete a control loop.
168 * @param name the name of the control loop to delete
169 * @param version the version of the control loop to delete
170 * @return the control loop deleted
171 * @throws PfModelRuntimeException on errors deleting the control loop
173 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
175 var controlLoopKey = new PfConceptKey(name, version);
177 var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
179 if (jpaDeleteControlLoop == null) {
180 String errorMessage =
181 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
182 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
185 getPfDao().delete(jpaDeleteControlLoop);
187 return jpaDeleteControlLoop.toAuthorative();
191 * Saves Instance Properties to the database.
192 * @param serviceTemplate the service template
193 * @return a Map of tosca node templates
195 public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
197 Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
199 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, template) -> {
200 JpaToscaNodeTemplate jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
202 getPfDao().create(jpaToscaNodeTemplate);
204 savedNodeTemplates.put(key, template);
207 return savedNodeTemplates;
211 * Get Node Templates.
213 * @param name the name of the node template to get, null to get all node templates
214 * @param version the version of the node template to get, null to get all node templates
215 * @return the node templates found
216 * @throws PfModelException on errors getting node templates
218 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
219 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
223 * Get filtered node templates.
225 * @param filter the filter for the node templates to get
226 * @return the node templates found
227 * @throws PfModelException on errors getting node templates
229 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
230 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
232 return filter.filter(asEntityList(
233 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
237 * Convert JPA control loop list to an authorative control loop list.
239 * @param <T> the type of TOSCA entity
240 * @param <J> the type of JPA TOSCA entity
241 * @param jpaEntityList the list to convert
242 * @return the authorative list
244 private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
245 return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());