520e9b86403441eca132b0193a988f59b666af21
[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.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.common.parameters.BeanValidationResult;
32 import org.onap.policy.models.base.PfAuthorative;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
44
45 /**
46  * This class provides information on control loop concepts in the database to callers.
47  */
48 public class ControlLoopProvider extends AbstractModelsProvider {
49
50     /**
51      * Create a provider for control loops.
52      *
53      * @param parameters the parameters for database access
54      * @throws PfModelException on initiation errors
55      */
56     public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
57         super(parameters);
58         this.init();
59     }
60
61     /**
62      * Get Control Loop.
63      *
64      * @param controlLoopId the ID of the control loop to get
65      * @return the control loop found
66      * @throws PfModelException on errors getting the control loop
67      */
68     public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
69         JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
70
71         return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
72     }
73
74     /**
75      * Update Control Loop.
76      *
77      * @param controlLoop the control loop to update
78      * @return the updated control loop
79      * @throws PfModelException on errors updating the control loop
80      */
81     public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
82         return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
83     }
84
85     /**
86      * Get Control Loops.
87      *
88      * @param name the name of the control loop to get, null to get all control loops
89      * @param version the version of the control loop to get, null to get all control loops
90      * @return the control loops found
91      * @throws PfModelException on errors getting control loops
92      */
93     public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
94
95         return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
96     }
97
98     /**
99      * Get filtered control loops.
100      *
101      * @param filter the filter for the control loops to get
102      * @return the control loops found
103      * @throws PfModelException on errors getting control loops
104      */
105     public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
106
107         return filter.filter(
108                 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
109     }
110
111     /**
112      * Creates control loops.
113      *
114      * @param controlLoops a specification of the control loops to create
115      * @return the control loops created
116      * @throws PfModelException on errors creating control loops
117      */
118     public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
119
120         BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
121
122         for (ControlLoop controlLoop : controlLoops) {
123             JpaControlLoop jpaControlLoop = new JpaControlLoop();
124             jpaControlLoop.fromAuthorative(controlLoop);
125
126             validationResult.addResult(jpaControlLoop.validate("control loop"));
127         }
128
129         if (!validationResult.isValid()) {
130             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
131         }
132
133         for (ControlLoop controlLoop : controlLoops) {
134             JpaControlLoop jpaControlLoop = new JpaControlLoop();
135             jpaControlLoop.fromAuthorative(controlLoop);
136
137             getPfDao().create(jpaControlLoop);
138         }
139
140         // Return the created control loops
141         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
142
143         for (ControlLoop controlLoop : controlLoops) {
144             JpaControlLoop jpaControlLoop = getPfDao().get(JpaControlLoop.class,
145                     new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
146             returnControlLoops.add(jpaControlLoop.toAuthorative());
147         }
148
149         return returnControlLoops;
150     }
151
152     /**
153      * Updates control loops.
154      *
155      * @param controlLoops a specification of the control loops to update
156      * @return the control loops updated
157      * @throws PfModelException on errors updating control loops
158      */
159     public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
160
161         BeanValidationResult validationResult = new BeanValidationResult("control loops", controlLoops);
162
163         for (ControlLoop controlLoop : controlLoops) {
164             JpaControlLoop jpaControlLoop = new JpaControlLoop();
165             jpaControlLoop.fromAuthorative(controlLoop);
166
167             validationResult.addResult(jpaControlLoop.validate("control loop"));
168         }
169
170         if (!validationResult.isValid()) {
171             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
172         }
173
174         // Return the created control loops
175         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
176
177         for (ControlLoop controlLoop : controlLoops) {
178             JpaControlLoop jpaControlLoop = new JpaControlLoop();
179             jpaControlLoop.fromAuthorative(controlLoop);
180
181             JpaControlLoop returnJpaControlLoop = getPfDao().update(jpaControlLoop);
182             returnControlLoops.add(returnJpaControlLoop.toAuthorative());
183         }
184
185         return returnControlLoops;
186     }
187
188     /**
189      * Delete a control loop.
190      *
191      * @param name the name of the control loop to delete
192      * @param version the version of the control loop to delete
193      * @return the control loop deleted
194      * @throws PfModelException on errors deleting the control loop
195      */
196     public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
197
198         PfConceptKey controlLoopKey = new PfConceptKey(name, version);
199
200         JpaControlLoop jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
201
202         if (jpaDeleteControlLoop == null) {
203             String errorMessage =
204                     "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
205             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
206         }
207
208         getPfDao().delete(jpaDeleteControlLoop);
209
210         return jpaDeleteControlLoop.toAuthorative();
211     }
212
213     /**
214      * Get Node Templates.
215      *
216      * @param name the name of the node template to get, null to get all node templates
217      * @param version the version of the node template to get, null to get all node templates
218      * @return the node templates found
219      * @throws PfModelException on errors getting node templates
220      */
221     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
222         return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
223     }
224
225     /**
226      * Get filtered node templates.
227      *
228      * @param filter the filter for the node templates to get
229      * @return the node templates found
230      * @throws PfModelException on errors getting node templates
231      */
232     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
233             @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
234
235         return filter.filter(asEntityList(
236                 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
237     }
238
239     /**
240      * Convert JPA control loop list to an authorative control loop list.
241      *
242      * @param jpaEntityList the list to convert
243      * @return the authorative list
244      */
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());
247     }
248 }