762b927ab3ce064b5213ea45101e27763e6df6e0
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
35 import org.onap.policy.models.base.PfAuthorative;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
47 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
48 import org.springframework.stereotype.Component;
49
50 /**
51  * This class provides information on control loop concepts in the database to callers.
52  */
53 @Component
54 public class ControlLoopProvider extends AbstractModelsProvider {
55
56     /**
57      * Create a provider for control loops.
58      *
59      * @param parameters the parameters for database access
60      * @throws PfModelException on initiation errors
61      */
62     public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
63         super(parameters);
64         this.init();
65     }
66
67     /**
68      * Get Control Loop.
69      *
70      * @param controlLoopId the ID of the control loop to get
71      * @return the control loop found
72      * @throws PfModelException on errors getting the control loop
73      */
74     public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
75         var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
76
77         return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
78     }
79
80     /**
81      * Update Control Loop.
82      *
83      * @param controlLoop the control loop to update
84      * @return the updated control loop
85      * @throws PfModelException on errors updating the control loop
86      */
87     public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
88         return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
89     }
90
91     /**
92      * Get Control Loops.
93      *
94      * @param name the name of the control loop to get, null to get all control loops
95      * @param version the version of the control loop to get, null to get all control loops
96      * @return the control loops found
97      * @throws PfModelException on errors getting control loops
98      */
99     public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
100
101         return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
102     }
103
104     /**
105      * Get filtered control loops.
106      *
107      * @param filter the filter for the control loops to get
108      * @return the control loops found
109      * @throws PfModelException on errors getting control loops
110      */
111     public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
112
113         return filter.filter(
114                 asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
115     }
116
117     /**
118      * Creates control loops.
119      *
120      * @param controlLoops a specification of the control loops to create
121      * @return the control loops created
122      * @throws PfModelException on errors creating control loops
123      */
124     public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
125
126         List<JpaControlLoop> jpaControlLoopList =
127                 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
128
129         jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
130
131         // Return the created control loops
132         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
133
134         for (ControlLoop controlLoop : controlLoops) {
135             var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
136                     new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
137             returnControlLoops.add(jpaControlLoop.toAuthorative());
138         }
139
140         return returnControlLoops;
141     }
142
143     /**
144      * Updates control loops.
145      *
146      * @param controlLoops a specification of the control loops to update
147      * @return the control loops updated
148      * @throws PfModelException on errors updating control loops
149      */
150     public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
151
152         List<JpaControlLoop> jpaControlLoopList =
153                 ProviderUtils.getJpaAndValidate(controlLoops, JpaControlLoop::new, "control loop");
154
155         // Return the created control loops
156         List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
157
158         jpaControlLoopList.forEach(jpaControlLoop -> {
159             var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
160             returnControlLoops.add(returnJpaControlLoop.toAuthorative());
161         });
162
163         return returnControlLoops;
164     }
165
166     /**
167      * Delete a control loop.
168      *
169      * @param name the name of the control loop to delete
170      * @param version the version of the control loop to delete
171      * @return the control loop deleted
172      * @throws PfModelRuntimeException on errors deleting the control loop
173      */
174     public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
175
176         var controlLoopKey = new PfConceptKey(name, version);
177
178         var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
179
180         if (jpaDeleteControlLoop == null) {
181             String errorMessage =
182                     "delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
183             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
184         }
185
186         getPfDao().delete(jpaDeleteControlLoop);
187
188         return jpaDeleteControlLoop.toAuthorative();
189     }
190
191     /**
192      * Saves Instance Properties to the database.
193      * @param serviceTemplate the service template
194      * @return a Map of tosca node templates
195      */
196     public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
197
198         Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
199
200         serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, template) -> {
201             var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
202
203             getPfDao().create(jpaToscaNodeTemplate);
204
205             savedNodeTemplates.put(key, template);
206         });
207
208         return savedNodeTemplates;
209     }
210
211     /**
212      * Get Node Templates.
213      *
214      * @param name the name of the node template to get, null to get all node templates
215      * @param version the version of the node template to get, null to get all node templates
216      * @return the node templates found
217      * @throws PfModelException on errors getting node templates
218      */
219     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
220         return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
221     }
222
223     /**
224      * Get filtered node templates.
225      *
226      * @param filter the filter for the node templates to get
227      * @return the node templates found
228      * @throws PfModelException on errors getting node templates
229      */
230     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
231             @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
232
233         return filter.filter(asEntityList(
234                 getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
235     }
236
237     /**
238      * Convert JPA control loop list to an authorative control loop list.
239      *
240      * @param <T> the type of TOSCA entity
241      * @param <J> the type of JPA TOSCA entity
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
249 }