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.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.function.UnaryOperator;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.AllArgsConstructor;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
39 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
40 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
41 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
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.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.springframework.stereotype.Component;
52 * This class is dedicated to the Instantiation of Commissioned control loop.
56 public class ControlLoopInstantiationProvider {
57 private final ControlLoopProvider controlLoopProvider;
58 private final CommissioningProvider commissioningProvider;
59 private final SupervisionHandler supervisionHandler;
61 private static final Object lockit = new Object();
64 * Create control loops.
66 * @param controlLoops the control loop
67 * @return the result of the instantiation operation
68 * @throws PfModelException on creation errors
70 public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
72 synchronized (lockit) {
73 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
74 var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
75 if (checkControlLoop != null) {
76 throw new PfModelException(Response.Status.BAD_REQUEST,
77 controlLoop.getKey().asIdentifier() + " already defined");
80 BeanValidationResult validationResult = validateControlLoops(controlLoops);
81 if (!validationResult.isValid()) {
82 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
84 controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
87 var response = new InstantiationResponse();
88 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
89 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
95 * Update control loops.
97 * @param controlLoops the control loop
98 * @return the result of the instantiation operation
99 * @throws PfModelException on update errors
101 public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
102 synchronized (lockit) {
103 BeanValidationResult validationResult = validateControlLoops(controlLoops);
104 if (!validationResult.isValid()) {
105 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
107 controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
110 var response = new InstantiationResponse();
111 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
112 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
118 * Validate ControlLoops.
120 * @param controlLoops ControlLoops to validate
121 * @return the result of validation
122 * @throws PfModelException if controlLoops is not valid
124 private BeanValidationResult validateControlLoops(ControlLoops controlLoops) throws PfModelException {
126 var result = new BeanValidationResult("ControlLoops", controlLoops);
128 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
129 var subResult = new BeanValidationResult("entry " + controlLoop.getDefinition().getName(), controlLoop);
131 List<ToscaNodeTemplate> toscaNodeTemplates = commissioningProvider.getControlLoopDefinitions(
132 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
134 if (toscaNodeTemplates.isEmpty()) {
135 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
136 ValidationStatus.INVALID, "Commissioned control loop definition not FOUND"));
137 } else if (toscaNodeTemplates.size() > 1) {
138 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
139 ValidationStatus.INVALID, "Commissioned control loop definition not VALID"));
142 List<ToscaNodeTemplate> clElementDefinitions =
143 commissioningProvider.getControlLoopElementDefinitions(toscaNodeTemplates.get(0));
146 Map<String, ToscaConceptIdentifier> definitions = clElementDefinitions
148 .map(nodeTemplate -> nodeTemplate.getKey().asIdentifier())
149 .collect(Collectors.toMap(ToscaConceptIdentifier::getName, UnaryOperator.identity()));
152 for (ControlLoopElement element : controlLoop.getElements().values()) {
153 subResult.addResult(validateDefinition(definitions, element.getDefinition()));
156 result.addResult(subResult);
162 * Validate ToscaConceptIdentifier, checking if exist in ToscaConceptIdentifiers map.
164 * @param definitions map of all ToscaConceptIdentifiers
165 * @param definition ToscaConceptIdentifier to validate
166 * @return the validation result
168 private ValidationResult validateDefinition(Map<String, ToscaConceptIdentifier> definitions,
169 ToscaConceptIdentifier definition) {
170 var result = new BeanValidationResult("entry " + definition.getName(), definition);
171 ToscaConceptIdentifier identifier = definitions.get(definition.getName());
172 if (identifier == null) {
173 result.setResult(ValidationStatus.INVALID, "Not FOUND");
174 } else if (!identifier.equals(definition)) {
175 result.setResult(ValidationStatus.INVALID, "Version not matching");
177 return (result.isClean() ? null : result);
181 * Delete the control loop with the given name and version.
183 * @param name the name of the control loop to delete
184 * @param version the version of the control loop to delete
185 * @return the result of the deletion
186 * @throws PfModelException on deletion errors
188 public InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
189 var response = new InstantiationResponse();
190 synchronized (lockit) {
191 List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
192 if (controlLoops.isEmpty()) {
193 throw new PfModelException(Response.Status.NOT_FOUND, "Control Loop not found");
195 for (ControlLoop controlLoop : controlLoops) {
196 if (!ControlLoopState.UNINITIALISED.equals(controlLoop.getState())) {
197 throw new PfModelException(Response.Status.BAD_REQUEST,
198 "Control Loop State is still " + controlLoop.getState());
202 response.setAffectedControlLoops(Collections
203 .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
209 * Get the requested control loops.
211 * @param name the name of the control loop to get, null for all control loops
212 * @param version the version of the control loop to get, null for all control loops
213 * @return the control loops
214 * @throws PfModelException on errors getting control loops
216 public ControlLoops getControlLoops(String name, String version) throws PfModelException {
217 var controlLoops = new ControlLoops();
218 controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
224 * Issue a command to control loops, setting their ordered state.
226 * @param command the command to issue to control loops
227 * @return the result of the initiation command
228 * @throws PfModelException on errors setting the ordered state on the control loops
229 * @throws ControlLoopException on ordered state invalid
231 public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
232 throws ControlLoopException, PfModelException {
234 if (command.getOrderedState() == null) {
235 throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
238 synchronized (lockit) {
239 List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
240 for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
241 var controlLoop = controlLoopProvider.getControlLoop(id);
242 controlLoop.setCascadedOrderedState(command.getOrderedState());
243 controlLoops.add(controlLoop);
245 controlLoopProvider.updateControlLoops(controlLoops);
248 supervisionHandler.triggerControlLoopSupervision(command.getControlLoopIdentifierList());
249 var response = new InstantiationResponse();
250 response.setAffectedControlLoops(command.getControlLoopIdentifierList());