71407916ddf2fb253770918c036c0ea504945044
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
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;
48
49 /**
50  * This class provides information on control loop concepts in the database to callers.
51  */
52 @Component
53 public class ControlLoopProvider extends AbstractModelsProvider {
54
55     /**
56      * Create a provider for control loops.
57      *
58      * @param parameters the parameters for database access
59      * @throws PfModelException on initiation errors
60      */
61     public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
62         super(parameters);
63         this.init();
64     }
65
66     /**
67      * Get Control Loop.
68      *
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
72      */
73     public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
74         var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
75
76         return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
77     }
78
79     /**
80      * Update Control Loop.
81      *
82      * @param controlLoop the control loop to update
83      * @return the updated control loop
84      * @throws PfModelException on errors updating the control loop
85      */
86     public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
87         return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
88     }
89
90     /**
91      * Get Control Loops.
92      *
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
97      */
98     public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
99
100         return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
101     }
102
103     /**
104      * Get filtered control loops.
105      *
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
109      */
110     public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
111
112         return filter.filter(
113                 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
114     }
115
116     /**
117      * Creates control loops.
118      *
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
122      */
123     public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
124
125         List<JpaControlLoop> jpaControlLoopList =
126                 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
127
128         jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
129
130         // Return the created control loops
131         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
132
133         for (ControlLoop controlLoop : controlLoops) {
134             var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
135                     new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
136             returnControlLoops.add(jpaControlLoop.toAuthorative());
137         }
138
139         return returnControlLoops;
140     }
141
142     /**
143      * Updates control loops.
144      *
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
148      */
149     public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
150
151         List<JpaControlLoop> jpaControlLoopList =
152                 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
153
154         // Return the created control loops
155         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
156
157         jpaControlLoopList.forEach(jpaControlLoop -> {
158             var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
159             returnControlLoops.add(returnJpaControlLoop.toAuthorative());
160         });
161
162         return returnControlLoops;
163     }
164
165     /**
166      * Delete a control loop.
167      *
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
172      */
173     public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
174
175         var controlLoopKey = new PfConceptKey(name, version);
176
177         var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
178
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);
183         }
184
185         getPfDao().delete(jpaDeleteControlLoop);
186
187         return jpaDeleteControlLoop.toAuthorative();
188     }
189
190     /**
191      * Saves Instance Properties to the database.
192      * @param serviceTemplate the service template
193      * @return a Map of tosca node templates
194      */
195     public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
196
197         Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
198
199         serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, template) -> {
200             JpaToscaNodeTemplate jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
201
202             getPfDao().create(jpaToscaNodeTemplate);
203
204             savedNodeTemplates.put(key, template);
205         });
206
207         return savedNodeTemplates;
208     }
209
210     /**
211      * Get Node Templates.
212      *
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
217      */
218     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
219         return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
220     }
221
222     /**
223      * Get filtered node templates.
224      *
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
228      */
229     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
230             @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
231
232         return filter.filter(asEntityList(
233                 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
234     }
235
236     /**
237      * Convert JPA control loop list to an authorative control loop list.
238      *
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
243      */
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());
246     }
247
248 }