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