f458d7c513cb705125ea25268082b9c4ec2835b5
[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.runtime.instantiation;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.function.UnaryOperator;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.Status;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
39 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
40 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
41 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
42 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
43 import org.onap.policy.common.parameters.BeanValidationResult;
44 import org.onap.policy.common.parameters.ObjectValidationResult;
45 import org.onap.policy.common.parameters.ValidationResult;
46 import org.onap.policy.common.parameters.ValidationStatus;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.base.PfModelRuntimeException;
49 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52
53 /**
54  * This class is dedicated to the Instantiation of Commissioned control loop.
55  */
56 public class ControlLoopInstantiationProvider implements Closeable {
57     private final ControlLoopProvider controlLoopProvider;
58     private final CommissioningProvider commissioningProvider;
59
60     private static final Object lockit = new Object();
61
62     /**
63      * Create a instantiation provider.
64      *
65      * @param databaseProviderParameters the parameters for database access
66      * @throws PfModelRuntimeException on errors creating a provider
67      */
68     public ControlLoopInstantiationProvider(PolicyModelsProviderParameters databaseProviderParameters) {
69         try {
70             controlLoopProvider = new ControlLoopProvider(databaseProviderParameters);
71             commissioningProvider = new CommissioningProvider(databaseProviderParameters);
72         } catch (PfModelException e) {
73             throw new PfModelRuntimeException(e);
74         }
75     }
76
77     @Override
78     public void close() throws IOException {
79         controlLoopProvider.close();
80     }
81
82     /**
83      * Create control loops.
84      *
85      * @param controlLoops the control loop
86      * @return the result of the instantiation operation
87      * @throws PfModelException on creation errors
88      */
89     public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
90
91         synchronized (lockit) {
92             for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
93                 var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
94                 if (checkControlLoop != null) {
95                     throw new PfModelException(Response.Status.BAD_REQUEST,
96                         controlLoop.getKey().asIdentifier() + " already defined");
97                 }
98             }
99             BeanValidationResult validationResult = validateControlLoops(controlLoops);
100             if (!validationResult.isValid()) {
101                 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
102             }
103             controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
104         }
105
106         var response = new InstantiationResponse();
107         response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
108             .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
109
110         return response;
111     }
112
113     /**
114      * Update control loops.
115      *
116      * @param controlLoops the control loop
117      * @return the result of the instantiation operation
118      * @throws PfModelException on update errors
119      */
120     public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
121         synchronized (lockit) {
122             BeanValidationResult validationResult = validateControlLoops(controlLoops);
123             if (!validationResult.isValid()) {
124                 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
125             }
126             controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
127         }
128
129         var response = new InstantiationResponse();
130         response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
131             .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
132
133         return response;
134     }
135
136     /**
137      * Validate ControlLoops.
138      *
139      * @param controlLoops ControlLoops to validate
140      * @return the result of validation
141      * @throws PfModelException if controlLoops is not valid
142      */
143     private BeanValidationResult validateControlLoops(ControlLoops controlLoops) throws PfModelException {
144
145         var result = new BeanValidationResult("ControlLoops", controlLoops);
146
147         for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
148             var subResult = new BeanValidationResult("entry " + controlLoop.getDefinition().getName(), controlLoop);
149
150             List<ToscaNodeTemplate> toscaNodeTemplates = commissioningProvider.getControlLoopDefinitions(
151                 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
152
153             if (toscaNodeTemplates.isEmpty()) {
154                 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
155                     ValidationStatus.INVALID, "Commissioned control loop definition not FOUND"));
156             } else if (toscaNodeTemplates.size() > 1) {
157                 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
158                     ValidationStatus.INVALID, "Commissioned control loop definition not VALID"));
159             } else {
160
161                 List<ToscaNodeTemplate> clElementDefinitions =
162                     commissioningProvider.getControlLoopElementDefinitions(toscaNodeTemplates.get(0));
163
164                 // @formatter:off
165                 Map<String, ToscaConceptIdentifier> definitions = clElementDefinitions
166                         .stream()
167                         .map(nodeTemplate -> nodeTemplate.getKey().asIdentifier())
168                         .collect(Collectors.toMap(ToscaConceptIdentifier::getName, UnaryOperator.identity()));
169                 // @formatter:on
170
171                 for (ControlLoopElement element : controlLoop.getElements().values()) {
172                     subResult.addResult(validateDefinition(definitions, element.getDefinition()));
173                 }
174             }
175             result.addResult(subResult);
176         }
177         return result;
178     }
179
180     /**
181      * Validate ToscaConceptIdentifier, checking if exist in ToscaConceptIdentifiers map.
182      *
183      * @param definitions map of all ToscaConceptIdentifiers
184      * @param definition ToscaConceptIdentifier to validate
185      * @return the validation result
186      */
187     private ValidationResult validateDefinition(Map<String, ToscaConceptIdentifier> definitions,
188         ToscaConceptIdentifier definition) {
189         var result = new BeanValidationResult("entry " + definition.getName(), definition);
190         ToscaConceptIdentifier identifier = definitions.get(definition.getName());
191         if (identifier == null) {
192             result.setResult(ValidationStatus.INVALID, "Not FOUND");
193         } else if (!identifier.equals(definition)) {
194             result.setResult(ValidationStatus.INVALID, "Version not matching");
195         }
196         return (result.isClean() ? null : result);
197     }
198
199     /**
200      * Delete the control loop with the given name and version.
201      *
202      * @param name the name of the control loop to delete
203      * @param version the version of the control loop to delete
204      * @return the result of the deletion
205      * @throws PfModelException on deletion errors
206      */
207     public InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
208         var response = new InstantiationResponse();
209         synchronized (lockit) {
210             List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
211             if (controlLoops.isEmpty()) {
212                 throw new PfModelException(Response.Status.NOT_FOUND, "Control Loop not found");
213             }
214             for (ControlLoop controlLoop : controlLoops) {
215                 if (!ControlLoopState.UNINITIALISED.equals(controlLoop.getState())) {
216                     throw new PfModelException(Response.Status.BAD_REQUEST,
217                         "Control Loop State is still " + controlLoop.getState());
218                 }
219             }
220
221             response.setAffectedControlLoops(Collections
222                 .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
223         }
224         return response;
225     }
226
227     /**
228      * Get the requested control loops.
229      *
230      * @param name the name of the control loop to get, null for all control loops
231      * @param version the version of the control loop to get, null for all control loops
232      * @return the control loops
233      * @throws PfModelException on errors getting control loops
234      */
235     public ControlLoops getControlLoops(String name, String version) throws PfModelException {
236         var controlLoops = new ControlLoops();
237         controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
238
239         return controlLoops;
240     }
241
242     /**
243      * Issue a command to control loops, setting their ordered state.
244      *
245      * @param command the command to issue to control loops
246      * @return the result of the initiation command
247      * @throws PfModelException on errors setting the ordered state on the control loops
248      * @throws ControlLoopException on ordered state invalid
249      */
250     public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
251         throws ControlLoopException, PfModelException {
252
253         if (command.getOrderedState() == null) {
254             throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
255         }
256
257         synchronized (lockit) {
258             List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
259             for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
260                 var controlLoop = controlLoopProvider.getControlLoop(id);
261                 controlLoop.setCascadedOrderedState(command.getOrderedState());
262                 controlLoops.add(controlLoop);
263             }
264             controlLoopProvider.updateControlLoops(controlLoops);
265         }
266
267         var supervisionHandler = SupervisionHandler.getInstance();
268         supervisionHandler.triggerControlLoopSupervision(command.getControlLoopIdentifierList());
269         var response = new InstantiationResponse();
270         response.setAffectedControlLoops(command.getControlLoopIdentifierList());
271
272         return response;
273     }
274 }