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