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