2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.instantiation;
24 import com.google.gson.Gson;
25 import com.google.gson.internal.LinkedTreeMap;
26 import com.google.gson.reflect.TypeToken;
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.UUID;
35 import java.util.function.UnaryOperator;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40 import lombok.AllArgsConstructor;
41 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
46 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
47 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
48 import org.onap.policy.clamp.controlloop.models.messages.rest.GenericNameVersion;
49 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse;
50 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
51 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
52 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
53 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
54 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
55 import org.onap.policy.common.parameters.BeanValidationResult;
56 import org.onap.policy.common.parameters.ObjectValidationResult;
57 import org.onap.policy.common.parameters.ValidationResult;
58 import org.onap.policy.common.parameters.ValidationStatus;
59 import org.onap.policy.models.base.PfModelException;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
61 import org.onap.policy.models.tosca.authorative.concepts.ToscaNameVersion;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
63 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
64 import org.springframework.stereotype.Component;
67 * This class is dedicated to the Instantiation of Commissioned control loop.
71 public class ControlLoopInstantiationProvider {
72 private static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop";
73 private static final String CONTROL_LOOP_NODE_ELEMENT_TYPE = "ControlLoopElement";
74 private static final String PARTICIPANT_ID_PROPERTY_KEY = "participant_id";
75 private static final String CL_ELEMENT_NAME = "name";
76 private static final String CL_ELEMENT_VERSION = "version";
77 private static final String INSTANCE_TEXT = "_Instance";
79 private static final Gson GSON = new Gson();
81 private final ControlLoopProvider controlLoopProvider;
82 private final CommissioningProvider commissioningProvider;
83 private final SupervisionHandler supervisionHandler;
85 private static final Object lockit = new Object();
88 * Creates Instance Properties and Control Loop.
90 * @param serviceTemplate the service template
91 * @return the result of the instantiation operation
92 * @throws PfModelException on creation errors
94 public InstancePropertiesResponse createInstanceProperties(ToscaServiceTemplate serviceTemplate)
95 throws PfModelException {
97 String instanceName = generateSequentialInstanceName();
98 ControlLoop controlLoop = new ControlLoop();
99 Map<UUID, ControlLoopElement> controlLoopElements = new HashMap<>();
101 ToscaServiceTemplate toscaServiceTemplate = commissioningProvider
102 .getToscaServiceTemplate(null, null);
104 Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = toscaServiceTemplate
105 .getToscaTopologyTemplate().getNodeTemplates();
107 Map<String, ToscaNodeTemplate> nodeTemplates =
108 deepCloneNodeTemplate(serviceTemplate);
110 nodeTemplates.forEach((key, template) -> {
111 ToscaNodeTemplate newNodeTemplate = new ToscaNodeTemplate();
112 String name = key + instanceName;
113 String version = template.getVersion();
114 String description = template.getDescription() + instanceName;
115 newNodeTemplate.setName(name);
116 newNodeTemplate.setVersion(version);
117 newNodeTemplate.setDescription(description);
118 newNodeTemplate.setProperties(new HashMap<>(template.getProperties()));
119 newNodeTemplate.setType(template.getType());
120 newNodeTemplate.setTypeVersion(template.getTypeVersion());
121 newNodeTemplate.setMetadata(template.getMetadata());
123 crateNewControlLoopInstance(instanceName, controlLoop, controlLoopElements, template, newNodeTemplate);
125 persistedNodeTemplateMap.put(name, newNodeTemplate);
128 ControlLoops controlLoops = new ControlLoops();
130 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().putAll(persistedNodeTemplateMap);
132 controlLoop.setElements(controlLoopElements);
133 controlLoops.getControlLoopList().add(controlLoop);
135 return saveInstancePropertiesAndControlLoop(serviceTemplate, controlLoops);
139 * Deletes Instance Properties.
141 * @param name the name of the control loop to delete
142 * @param version the version of the control loop to delete
143 * @return the result of the deletion
144 * @throws PfModelException on deletion errors
146 public InstantiationResponse deleteInstanceProperties(String name, String version) throws PfModelException {
148 String instanceName = getInstancePropertyName(name, version);
150 Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap = new HashMap<>();
152 ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(name, version);
154 toscaServiceTemplate.getToscaTopologyTemplate()
155 .getNodeTemplates().forEach((key, nodeTemplate) -> {
156 if (!nodeTemplate.getName().contains(instanceName)) {
157 filteredToscaNodeTemplateMap.put(key, nodeTemplate);
161 List<ToscaNodeTemplate> filteredToscaNodeTemplateList =
162 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().values().stream()
163 .filter(nodeTemplate -> nodeTemplate.getName().contains(instanceName)).collect(Collectors.toList());
165 InstantiationResponse response = this.deleteControlLoop(name, version);
167 controlLoopProvider.deleteInstanceProperties(filteredToscaNodeTemplateMap, filteredToscaNodeTemplateList);
173 * Create control loops.
175 * @param controlLoops the control loop
176 * @return the result of the instantiation operation
177 * @throws PfModelException on creation errors
179 public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
181 synchronized (lockit) {
182 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
183 var checkControlLoop = controlLoopProvider
184 .getControlLoop(controlLoop.getKey().asIdentifier());
185 if (checkControlLoop != null) {
186 throw new PfModelException(Response.Status.BAD_REQUEST,
187 controlLoop.getKey().asIdentifier() + " already defined");
190 BeanValidationResult validationResult = validateControlLoops(controlLoops);
191 if (!validationResult.isValid()) {
192 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
194 controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
197 var response = new InstantiationResponse();
198 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
199 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
205 * Update control loops.
207 * @param controlLoops the control loop
208 * @return the result of the instantiation operation
209 * @throws PfModelException on update errors
211 public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
212 synchronized (lockit) {
213 BeanValidationResult validationResult = validateControlLoops(controlLoops);
214 if (!validationResult.isValid()) {
215 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
217 controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
220 var response = new InstantiationResponse();
221 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
222 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
228 * Validate ControlLoops.
230 * @param controlLoops ControlLoops to validate
231 * @return the result of validation
232 * @throws PfModelException if controlLoops is not valid
234 private BeanValidationResult validateControlLoops(ControlLoops controlLoops) throws PfModelException {
236 var result = new BeanValidationResult("ControlLoops", controlLoops);
238 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
239 var subResult = new BeanValidationResult("entry " + controlLoop.getDefinition().getName(), controlLoop);
241 List<ToscaNodeTemplate> toscaNodeTemplates = commissioningProvider.getControlLoopDefinitions(
242 controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());
244 if (toscaNodeTemplates.isEmpty()) {
245 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
246 ValidationStatus.INVALID, "Commissioned control loop definition not FOUND"));
247 } else if (toscaNodeTemplates.size() > 1) {
248 subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
249 ValidationStatus.INVALID, "Commissioned control loop definition not VALID"));
252 List<ToscaNodeTemplate> clElementDefinitions =
253 commissioningProvider.getControlLoopElementDefinitions(toscaNodeTemplates.get(0));
256 Map<String, ToscaConceptIdentifier> definitions = clElementDefinitions
258 .map(nodeTemplate -> nodeTemplate.getKey().asIdentifier())
259 .collect(Collectors.toMap(ToscaConceptIdentifier::getName, UnaryOperator.identity()));
262 for (ControlLoopElement element : controlLoop.getElements().values()) {
263 subResult.addResult(validateDefinition(definitions, element.getDefinition()));
266 result.addResult(subResult);
272 * Validate ToscaConceptIdentifier, checking if exist in ToscaConceptIdentifiers map.
274 * @param definitions map of all ToscaConceptIdentifiers
275 * @param definition ToscaConceptIdentifier to validate
276 * @return the validation result
278 private ValidationResult validateDefinition(Map<String, ToscaConceptIdentifier> definitions,
279 ToscaConceptIdentifier definition) {
280 var result = new BeanValidationResult("entry " + definition.getName(), definition);
281 ToscaConceptIdentifier identifier = definitions.get(definition.getName());
282 if (identifier == null) {
283 result.setResult(ValidationStatus.INVALID, "Not FOUND");
284 } else if (!identifier.equals(definition)) {
285 result.setResult(ValidationStatus.INVALID, "Version not matching");
287 return (result.isClean() ? null : result);
291 * Delete the control loop with the given name and version.
293 * @param name the name of the control loop to delete
294 * @param version the version of the control loop to delete
295 * @return the result of the deletion
296 * @throws PfModelException on deletion errors
298 public InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
299 var response = new InstantiationResponse();
300 synchronized (lockit) {
301 List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
302 if (controlLoops.isEmpty()) {
303 throw new PfModelException(Response.Status.NOT_FOUND, "Control Loop not found");
305 for (ControlLoop controlLoop : controlLoops) {
306 if (!ControlLoopState.UNINITIALISED.equals(controlLoop.getState())) {
307 throw new PfModelException(Response.Status.BAD_REQUEST,
308 "Control Loop State is still " + controlLoop.getState());
312 response.setAffectedControlLoops(Collections
313 .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
319 * Get the requested control loops.
321 * @param name the name of the control loop to get, null for all control loops
322 * @param version the version of the control loop to get, null for all control loops
323 * @return the control loops
324 * @throws PfModelException on errors getting control loops
326 public ControlLoops getControlLoops(String name, String version) throws PfModelException {
327 var controlLoops = new ControlLoops();
328 controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
334 * Issue a command to control loops, setting their ordered state.
336 * @param command the command to issue to control loops
337 * @return the result of the initiation command
338 * @throws PfModelException on errors setting the ordered state on the control loops
339 * @throws ControlLoopException on ordered state invalid
341 public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
342 throws ControlLoopException, PfModelException {
344 if (command.getOrderedState() == null) {
345 throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
348 synchronized (lockit) {
349 List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
350 for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
351 var controlLoop = controlLoopProvider.getControlLoop(id);
352 controlLoop.setCascadedOrderedState(command.getOrderedState());
353 controlLoops.add(controlLoop);
355 controlLoopProvider.updateControlLoops(controlLoops);
358 supervisionHandler.triggerControlLoopSupervision(command.getControlLoopIdentifierList());
359 var response = new InstantiationResponse();
360 response.setAffectedControlLoops(command.getControlLoopIdentifierList());
366 * Gets a list of control loops with it's ordered state.
368 * @param name the name of the control loop to get, null for all control loops
369 * @param version the version of the control loop to get, null for all control loops
370 * @return a list of Instantiation Command
371 * @throws PfModelException on errors getting control loops
373 public ControlLoopOrderStateResponse getInstantiationOrderState(String name, String version)
374 throws PfModelException {
376 List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
378 var response = new ControlLoopOrderStateResponse();
380 controlLoops.forEach(controlLoop -> {
381 var genericNameVersion = new GenericNameVersion();
382 genericNameVersion.setName(controlLoop.getName());
383 genericNameVersion.setVersion(controlLoop.getVersion());
384 response.getControlLoopIdentifierList().add(genericNameVersion);
391 * Saves Instance Properties and Control Loop.
393 * @param serviceTemplate the service template
394 * @param controlLoops a list of control loops
395 * @return the result of the instance properties and instantiation operation
396 * @throws PfModelException on creation errors
398 private InstancePropertiesResponse saveInstancePropertiesAndControlLoop(
399 ToscaServiceTemplate serviceTemplate, ControlLoops controlLoops) throws PfModelException {
401 var response = new InstancePropertiesResponse();
403 Map<String, ToscaNodeTemplate> toscaSavedNodeTemplate;
405 synchronized (lockit) {
406 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
407 var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
408 if (checkControlLoop != null) {
409 throw new PfModelException(Response.Status.BAD_REQUEST,
410 controlLoop.getKey().asIdentifier() + " already defined");
414 toscaSavedNodeTemplate = controlLoopProvider.saveInstanceProperties(serviceTemplate);
416 controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
420 List<ToscaConceptIdentifier> affectedControlLoops = controlLoops.getControlLoopList().stream()
421 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList());
423 List<ToscaConceptIdentifier> toscaAffectedProperties = toscaSavedNodeTemplate.values().stream()
424 .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList());
426 response.setAffectedInstanceProperties(Stream.of(affectedControlLoops, toscaAffectedProperties)
427 .flatMap(Collection::stream).collect(Collectors.toList()));
433 * Crates a new Control Loop instance.
434 * @param instanceName Control Loop Instance name
435 * @param controlLoop empty Control Loop
436 * @param controlLoopElements new Control Loop Element map
437 * @param template original Cloned Tosca Node Template
438 * @param newNodeTemplate new Tosca Node Template
440 private void crateNewControlLoopInstance(String instanceName, ControlLoop controlLoop,
441 Map<UUID, ControlLoopElement> controlLoopElements,
442 ToscaNodeTemplate template,
443 ToscaNodeTemplate newNodeTemplate) {
444 if (template.getType().equals(CONTROL_LOOP_NODE_TYPE)) {
445 controlLoop.setDefinition(getControlLoopDefinition(newNodeTemplate));
448 if (template.getType().contains(CONTROL_LOOP_NODE_ELEMENT_TYPE)) {
449 ControlLoopElement controlLoopElement = getControlLoopElement(instanceName, newNodeTemplate);
450 controlLoopElements.put(controlLoopElement.getId(), controlLoopElement);
453 controlLoop.setName("PMSH" + instanceName);
454 controlLoop.setVersion(template.getVersion());
455 controlLoop.setDescription("PMSH control loop " + instanceName);
456 controlLoop.setState(ControlLoopState.UNINITIALISED);
457 controlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
462 * Get's the instance property name of the control loop.
464 * @param name the name of the control loop to get, null for all control loops
465 * @param version the version of the control loop to get, null for all control loops
466 * @return the instance name of the control loop instance properties
467 * @throws PfModelException on errors getting control loops
469 private String getInstancePropertyName(String name, String version) throws PfModelException {
470 List<String> toscaDefinitionsNames =
471 controlLoopProvider.getControlLoops(name, version).stream().map(ControlLoop::getDefinition)
472 .map(ToscaNameVersion::getName).collect(Collectors.toList());
474 return toscaDefinitionsNames.stream().reduce("", (s1, s2) -> {
476 if (s2.contains(INSTANCE_TEXT)) {
477 String[] instances = s2.split(INSTANCE_TEXT);
479 return INSTANCE_TEXT + instances[1];
487 * Generates Instance Name in sequential order and return it to append to the Node Template Name.
489 * @return instanceName
491 private String generateSequentialInstanceName() {
492 List<ToscaNodeTemplate> nodeTemplates = controlLoopProvider.getNodeTemplates(null, null);
495 nodeTemplates.stream().map(ToscaNodeTemplate::getName)
496 .filter(name -> name.contains(INSTANCE_TEXT)).map(n -> {
497 String[] defNameArr = n.split(INSTANCE_TEXT);
499 return Integer.parseInt(defNameArr[1]);
500 }).reduce(0, Math::max);
502 return INSTANCE_TEXT + (instanceNumber + 1);
506 * Retrieves Control Loop Definition.
508 * @param template tosca node template
509 * @return control loop definition
511 private ToscaConceptIdentifier getControlLoopDefinition(ToscaNodeTemplate template) {
512 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
513 definition.setName(template.getName());
514 definition.setVersion(template.getVersion());
520 * Retrieves Control Loop Element.
522 * @param instanceName instance name to be appended to participant name
523 * @param template tosca node template
524 * @return a control loop element
526 @SuppressWarnings("unchecked")
527 private ControlLoopElement getControlLoopElement(String instanceName, ToscaNodeTemplate template) {
528 ControlLoopElement controlLoopElement = new ControlLoopElement();
529 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
530 definition.setName(template.getName());
531 definition.setVersion(template.getVersion());
532 controlLoopElement.setDefinition(definition);
534 LinkedTreeMap<String, Object> participantId = (LinkedTreeMap<String, Object>) template.getProperties()
535 .get(PARTICIPANT_ID_PROPERTY_KEY);
537 ToscaConceptIdentifier participantIdAndType = new ToscaConceptIdentifier();
538 participantIdAndType.setName(participantId.get(CL_ELEMENT_NAME) + instanceName);
539 participantIdAndType.setVersion(String.valueOf(participantId.get(CL_ELEMENT_VERSION)));
541 controlLoopElement.setParticipantType(participantIdAndType);
542 controlLoopElement.setParticipantId(participantIdAndType);
544 return controlLoopElement;
548 * Deep clones ToscaNodeTemplate.
550 * @param serviceTemplate ToscaServiceTemplate
551 * @return a cloned Hash Map of ToscaNodeTemplate
553 private Map<String, ToscaNodeTemplate> deepCloneNodeTemplate(ToscaServiceTemplate serviceTemplate) {
554 String jsonString = GSON.toJson(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates());
556 Type type = new TypeToken<HashMap<String, ToscaNodeTemplate>>() {}.getType();
558 return GSON.fromJson(jsonString, type);