a61b97fa8d5efb4aa98092ebbbc1e974ecf21e67
[policy/clamp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
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.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * This class provides information on control loop concepts in the database to callers.
53  */
54 @Component
55 public class ControlLoopProvider extends AbstractModelsProvider {
56
57     /**
58      * Create a provider for control loops.
59      *
60      * @param parameters the parameters for database access
61      * @throws PfModelException on initiation errors
62      */
63     public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
64         super(parameters);
65         this.init();
66     }
67
68     /**
69      * Get Control Loop.
70      *
71      * @param controlLoopId the ID of the control loop to get
72      * @return the control loop found
73      * @throws PfModelException on errors getting the control loop
74      */
75     public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
76         var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
77
78         return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
79     }
80
81     /**
82      * Update Control Loop.
83      *
84      * @param controlLoop the control loop to update
85      * @return the updated control loop
86      * @throws PfModelException on errors updating the control loop
87      */
88     public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
89         return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
90     }
91
92     /**
93      * Get Control Loops.
94      *
95      * @param name the name of the control loop to get, null to get all control loops
96      * @param version the version of the control loop to get, null to get all control loops
97      * @return the control loops found
98      * @throws PfModelException on errors getting control loops
99      */
100     public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
101
102         return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
103     }
104
105     /**
106      * Get filtered control loops.
107      *
108      * @param filter the filter for the control loops to get
109      * @return the control loops found
110      * @throws PfModelException on errors getting control loops
111      */
112     public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
113
114         return filter.filter(
115                 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
116     }
117
118     /**
119      * Creates control loops.
120      *
121      * @param controlLoops a specification of the control loops to create
122      * @return the control loops created
123      * @throws PfModelException on errors creating control loops
124      */
125     public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
126
127         List<JpaControlLoop> jpaControlLoopList =
128                 ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
129
130         jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
131
132         // Return the created control loops
133         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
134
135         for (ControlLoop controlLoop : controlLoops) {
136             var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
137                     new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
138             returnControlLoops.add(jpaControlLoop.toAuthorative());
139         }
140
141         return returnControlLoops;
142     }
143
144     /**
145      * Updates control loops.
146      *
147      * @param controlLoops a specification of the control loops to update
148      * @return the control loops updated
149      * @throws PfModelException on errors updating control loops
150      */
151     public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
152
153         List<JpaControlLoop> jpaControlLoopList =
154                 ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
155
156         // Return the created control loops
157         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
158
159         jpaControlLoopList.forEach(jpaControlLoop -> {
160             var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
161             returnControlLoops.add(returnJpaControlLoop.toAuthorative());
162         });
163
164         return returnControlLoops;
165     }
166
167     /**
168      * Delete a control loop.
169      *
170      * @param name the name of the control loop to delete
171      * @param version the version of the control loop to delete
172      * @return the control loop deleted
173      * @throws PfModelRuntimeException on errors deleting the control loop
174      */
175     public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
176
177         var controlLoopKey = new PfConceptKey(name, version);
178
179         var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
180
181         if (jpaDeleteControlLoop == null) {
182             String errorMessage =
183                     "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
184             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
185         }
186
187         getPfDao().delete(jpaDeleteControlLoop);
188
189         return jpaDeleteControlLoop.toAuthorative();
190     }
191
192     /**
193      * Saves Instance Properties to the database.
194      * @param serviceTemplate the service template
195      * @return a Map of tosca node templates
196      */
197     public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
198
199         Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
200
201         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
202         jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(serviceTemplate.getToscaTopologyTemplate()
203             .getNodeTemplates()));
204
205         getPfDao().create(jpaToscaNodeTemplates);
206         serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
207
208         return savedNodeTemplates;
209     }
210
211     /**
212      * Deletes Instance Properties on the database.
213      *
214      * @param filteredToscaNodeTemplateMap filtered node templates map to delete
215      * @param filteredToscaNodeTemplateList filtered node template list to delete
216      */
217     public void deleteInstanceProperties(
218         Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
219         List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
220
221         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
222         jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(filteredToscaNodeTemplateMap));
223
224         getPfDao().create(jpaToscaNodeTemplates);
225
226         filteredToscaNodeTemplateList.forEach(template -> {
227             var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
228
229             getPfDao().delete(jpaToscaNodeTemplate);
230         });
231     }
232
233     /**
234      * Get Node Templates.
235      *
236      * @param name the name of the node template to get, null to get all node templates
237      * @param version the version of the node template to get, null to get all node templates
238      * @return the node templates found
239      * @throws PfModelException on errors getting node templates
240      */
241     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
242         return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
243     }
244
245     /**
246      * Get filtered node templates.
247      *
248      * @param filter the filter for the node templates to get
249      * @return the node templates found
250      * @throws PfModelException on errors getting node templates
251      */
252     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
253             @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
254
255         return filter.filter(asEntityList(
256                 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
257     }
258
259     /**
260      * Convert JPA control loop list to an authorative control loop list.
261      *
262      * @param <T> the type of TOSCA entity
263      * @param <J> the type of JPA TOSCA entity
264      * @param jpaEntityList the list to convert
265      * @return the authorative list
266      */
267     private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
268         return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());
269     }
270
271 }