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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.instantiation;
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;
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.common.parameters.BeanValidationResult;
43 import org.onap.policy.common.parameters.ObjectValidationResult;
44 import org.onap.policy.common.parameters.ValidationResult;
45 import org.onap.policy.common.parameters.ValidationStatus;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 * This class is dedicated to the Instantiation of Commissioned control loop.
55 public class ControlLoopInstantiationProvider implements Closeable {
56 private final ControlLoopProvider controlLoopProvider;
57 private final CommissioningProvider commissioningProvider;
59 private static final Object lockit = new Object();
62 * Create a instantiation provider.
64 * @param databaseProviderParameters the parameters for database access
66 public ControlLoopInstantiationProvider(PolicyModelsProviderParameters databaseProviderParameters) {
68 controlLoopProvider = new ControlLoopProvider(databaseProviderParameters);
69 commissioningProvider = new CommissioningProvider(databaseProviderParameters);
70 } catch (PfModelException e) {
71 throw new PfModelRuntimeException(e);
76 public void close() throws IOException {
77 controlLoopProvider.close();
81 * Create control loops.
83 * @param controlLoops the control loop
84 * @return the result of the instantiation operation
85 * @throws PfModelException on creation errors
87 public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
89 synchronized (lockit) {
90 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
91 ControlLoop checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
92 if (checkControlLoop != null) {
93 throw new PfModelException(Response.Status.BAD_REQUEST,
94 controlLoop.getKey().asIdentifier() + " already defined");
97 BeanValidationResult validationResult = validateControlLoops(controlLoops);
98 if (!validationResult.isValid()) {
99 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
101 controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
104 InstantiationResponse response = new InstantiationResponse();
105 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
106 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
112 * Update control loops.
114 * @param controlLoops the control loop
115 * @return the result of the instantiation operation
116 * @throws PfModelException on update errors
118 public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
119 synchronized (lockit) {
120 BeanValidationResult validationResult = validateControlLoops(controlLoops);
121 if (!validationResult.isValid()) {
122 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
124 controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
127 InstantiationResponse response = new InstantiationResponse();
128 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
129 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
135 * Validate ControlLoops.
137 * @param controlLoops ControlLoops to validate
138 * @result the result of validation
139 * @throws PfModelException if controlLoops is not valid
141 private BeanValidationResult validateControlLoops(ControlLoops controlLoops) throws PfModelException {
143 BeanValidationResult validationResult = new BeanValidationResult("ControlLoops", controlLoops);
145 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
147 List<ToscaNodeTemplate> toscaNodeTemplates = commissioningProvider.getControlLoopDefinitions(
148 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
150 if (toscaNodeTemplates.isEmpty()) {
152 .addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
153 ValidationStatus.INVALID, "Commissioned control loop definition not FOUND"));
154 } else if (toscaNodeTemplates.size() > 1) {
156 .addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
157 ValidationStatus.INVALID, "Commissioned control loop definition not VALID"));
160 List<ToscaNodeTemplate> clElementDefinitions =
161 commissioningProvider.getControlLoopElementDefinitions(toscaNodeTemplates.get(0));
164 Map<String, ToscaConceptIdentifier> definitions = clElementDefinitions
166 .map(nodeTemplate -> nodeTemplate.getKey().asIdentifier())
167 .collect(Collectors.toMap(ToscaConceptIdentifier::getName, UnaryOperator.identity()));
170 for (ControlLoopElement element : controlLoop.getElements()) {
171 validationResult.addResult(validateDefinition(definitions, element.getDefinition()));
175 return validationResult;
179 * Validate ToscaConceptIdentifier, checking if exist in ToscaConceptIdentifiers map.
181 * @param definitions map of all ToscaConceptIdentifiers
182 * @param definition ToscaConceptIdentifier to validate
183 * @result result the validation result
185 private ValidationResult validateDefinition(Map<String, ToscaConceptIdentifier> definitions,
186 ToscaConceptIdentifier definition) {
187 BeanValidationResult result = new BeanValidationResult(definition.getName(), definition);
188 ToscaConceptIdentifier identifier = definitions.get(definition.getName());
189 if (identifier == null) {
190 result.setResult(ValidationStatus.INVALID, "Not FOUND");
191 } else if (!identifier.equals(definition)) {
192 result.setResult(ValidationStatus.INVALID, "Version not matching");
194 result.setResult(ValidationStatus.CLEAN);
196 return (result.isClean() ? null : result);
200 * Delete the control loop with the given name and version.
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
207 public InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
208 InstantiationResponse 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");
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());
221 response.setAffectedControlLoops(Collections
222 .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
228 * Get the requested control loops.
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
235 public ControlLoops getControlLoops(String name, String version) throws PfModelException {
236 ControlLoops controlLoops = new ControlLoops();
237 controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
243 * Issue a command to control loops, setting their ordered state.
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
250 public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
251 throws ControlLoopException, PfModelException {
253 if (command.getOrderedState() == null) {
254 throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
257 synchronized (lockit) {
258 List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
259 for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
260 ControlLoop controlLoop = controlLoopProvider.getControlLoop(id);
261 controlLoop.setCascadedOrderedState(command.getOrderedState());
262 controlLoops.add(controlLoop);
264 controlLoopProvider.updateControlLoops(controlLoops);
267 InstantiationResponse response = new InstantiationResponse();
268 response.setAffectedControlLoops(command.getControlLoopIdentifierList());