1 package org.openecomp.sdc.be.components.property;
3 import static org.apache.commons.collections.CollectionUtils.isEmpty;
4 import static org.openecomp.sdc.be.model.utils.ComponentUtilities.getInputAnnotations;
7 import java.util.ArrayList;
8 import java.util.Collections;
11 import java.util.Optional;
12 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
13 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
14 import org.openecomp.sdc.be.datatypes.elements.Annotation;
15 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
16 import org.openecomp.sdc.be.impl.ComponentsUtils;
17 import org.openecomp.sdc.be.model.Component;
18 import org.openecomp.sdc.be.model.ComponentInstance;
19 import org.openecomp.sdc.be.model.ComponentInstanceInput;
20 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
21 import org.openecomp.sdc.be.model.ComponentParametersView;
22 import org.openecomp.sdc.be.model.InputDefinition;
23 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
24 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
25 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
28 @org.springframework.stereotype.Component
29 public class ComponentInstanceInputPropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceInput> {
31 private static final Logger log = Logger.getLogger(ComponentInstanceInputPropertyDeclarator.class);
32 private final ToscaOperationFacade toscaOperationFacade;
33 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
34 private final ExceptionUtils exceptionUtils;
36 public ComponentInstanceInputPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, ExceptionUtils exceptionUtils) {
37 super(componentsUtils, propertyOperation);
38 this.toscaOperationFacade = toscaOperationFacade;
39 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
40 this.exceptionUtils = exceptionUtils;
44 public ComponentInstanceInput createDeclaredProperty(PropertyDataDefinition prop) {
45 return new ComponentInstanceInput(prop);
49 public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceInput> properties) {
50 log.debug("#updatePropertiesValues - updating component instance inputs for instance {} on component {}", cmptInstanceId, component.getUniqueId());
51 Map<String, List<ComponentInstanceInput>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
52 return toscaOperationFacade.addComponentInstanceInputsToComponent(component, instProperties);
56 public Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
57 log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
58 return component.getComponentInstanceById(propertiesOwnerId);
62 public void addPropertiesListToInput(ComponentInstanceInput declaredProp, InputDefinition input) {
63 List<ComponentInstanceInput> inputsValueList = input.getInputs();
64 if(inputsValueList == null) {
65 inputsValueList = new ArrayList<>(); // adding the property with the new value for UI
67 inputsValueList.add(declaredProp);
68 input.setInputs(inputsValueList);
72 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
73 List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
74 if (isEmpty(componentInstanceInputsByInputId)) {
75 return StorageOperationStatus.OK;
77 componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
78 return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
82 public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
83 List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
84 if (isEmpty(componentInstanceInputsByInputId)) {
85 return StorageOperationStatus.OK;
87 componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
88 return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
92 InputDefinition createInputFromProperty(String componentId, ComponentInstance propertiesOwner, String inputName, ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
93 InputDefinition inputFromProperty = super.createInputFromProperty(componentId, propertiesOwner, inputName, propInput, prop);
94 Component propertiesOwnerNodeType = getInstanceOriginType(propertiesOwner);
95 enrichInputWithAnnotations(prop, inputFromProperty, propertiesOwnerNodeType);
96 return inputFromProperty;
99 private void enrichInputWithAnnotations(PropertyDataDefinition prop, InputDefinition inputFromProperty, Component propertiesOwnerNodeType) {
100 List<Annotation> inputAnnotations = getInputAnnotations(propertiesOwnerNodeType, prop.getName());
101 if(!isEmpty(inputAnnotations)){
102 inputFromProperty.setAnnotations(inputAnnotations);
106 private Component getInstanceOriginType(ComponentInstance propertiesOwner) {
107 return toscaOperationFacade.getToscaElement(propertiesOwner.getActualComponentUid(), getFilterComponentInputs())
109 .on(err -> exceptionUtils.rollBackAndThrow(err, propertiesOwner.getActualComponentUid()));
112 private ComponentParametersView getFilterComponentInputs() {
113 ComponentParametersView filterInputs = new ComponentParametersView(true);
114 filterInputs.setIgnoreInputs(false);