2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
35 import org.onap.policy.models.base.PfAuthorative;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
47 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
48 import org.springframework.stereotype.Component;
51 * This class provides information on control loop concepts in the database to callers.
54 public class ControlLoopProvider extends AbstractModelsProvider {
57 * Create a provider for control loops.
59 * @param parameters the parameters for database access
60 * @throws PfModelException on initiation errors
62 public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
70 * @param controlLoopId the ID of the control loop to get
71 * @return the control loop found
72 * @throws PfModelException on errors getting the control loop
74 public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
75 var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
77 return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
81 * Update Control Loop.
83 * @param controlLoop the control loop to update
84 * @return the updated control loop
85 * @throws PfModelException on errors updating the control loop
87 public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
88 return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
94 * @param name the name of the control loop to get, null to get all control loops
95 * @param version the version of the control loop to get, null to get all control loops
96 * @return the control loops found
97 * @throws PfModelException on errors getting control loops
99 public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
101 return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
105 * Get filtered control loops.
107 * @param filter the filter for the control loops to get
108 * @return the control loops found
109 * @throws PfModelException on errors getting control loops
111 public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
113 return filter.filter(
114 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
118 * Creates control loops.
120 * @param controlLoops a specification of the control loops to create
121 * @return the control loops created
122 * @throws PfModelException on errors creating control loops
124 public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
126 List<JpaControlLoop> jpaControlLoopList =
127 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
129 jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
131 // Return the created control loops
132 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
134 for (ControlLoop controlLoop : controlLoops) {
135 var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
136 new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
137 returnControlLoops.add(jpaControlLoop.toAuthorative());
140 return returnControlLoops;
144 * Updates control loops.
146 * @param controlLoops a specification of the control loops to update
147 * @return the control loops updated
148 * @throws PfModelException on errors updating control loops
150 public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
152 List<JpaControlLoop> jpaControlLoopList =
153 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
155 // Return the created control loops
156 List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
158 jpaControlLoopList.forEach(jpaControlLoop -> {
159 var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
160 returnControlLoops.add(returnJpaControlLoop.toAuthorative());
163 return returnControlLoops;
167 * Delete a control loop.
169 * @param name the name of the control loop to delete
170 * @param version the version of the control loop to delete
171 * @return the control loop deleted
172 * @throws PfModelRuntimeException on errors deleting the control loop
174 public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
176 var controlLoopKey = new PfConceptKey(name, version);
178 var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
180 if (jpaDeleteControlLoop == null) {
181 String errorMessage =
182 "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
183 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
186 getPfDao().delete(jpaDeleteControlLoop);
188 return jpaDeleteControlLoop.toAuthorative();
192 * Saves Instance Properties to the database.
193 * @param serviceTemplate the service template
194 * @return a Map of tosca node templates
196 public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
198 Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
200 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, template) -> {
201 var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
203 getPfDao().create(jpaToscaNodeTemplate);
205 savedNodeTemplates.put(key, template);
208 return savedNodeTemplates;
212 * Get Node Templates.
214 * @param name the name of the node template to get, null to get all node templates
215 * @param version the version of the node template to get, null to get all node templates
216 * @return the node templates found
217 * @throws PfModelException on errors getting node templates
219 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
220 return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
224 * Get filtered node templates.
226 * @param filter the filter for the node templates to get
227 * @return the node templates found
228 * @throws PfModelException on errors getting node templates
230 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
231 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
233 return filter.filter(asEntityList(
234 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
238 * Convert JPA control loop list to an authorative control loop list.
240 * @param <T> the type of TOSCA entity
241 * @param <J> the type of JPA TOSCA entity
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());