1 package org.openecomp.sdc.be.components.property;
4 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
5 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
6 import org.openecomp.sdc.be.datatypes.elements.Annotation;
7 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.model.*;
10 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
11 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
12 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
13 import org.openecomp.sdc.common.log.wrappers.Logger;
17 import static org.apache.commons.collections.CollectionUtils.isEmpty;
18 import static org.openecomp.sdc.be.model.utils.ComponentUtilities.getInputAnnotations;
20 @org.springframework.stereotype.Component
21 public class ComponentInstanceInputPropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceInput> {
23 private static final Logger log = Logger.getLogger(ComponentInstanceInputPropertyDeclarator.class);
24 private final ToscaOperationFacade toscaOperationFacade;
25 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
26 private final ExceptionUtils exceptionUtils;
28 public ComponentInstanceInputPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, ExceptionUtils exceptionUtils) {
29 super(componentsUtils, propertyOperation);
30 this.toscaOperationFacade = toscaOperationFacade;
31 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
32 this.exceptionUtils = exceptionUtils;
36 ComponentInstanceInput createDeclaredProperty(PropertyDataDefinition prop) {
37 return new ComponentInstanceInput(prop);
41 Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceInput> properties) {
42 log.debug("#updatePropertiesValues - updating component instance inputs for instance {} on component {}", cmptInstanceId, component.getUniqueId());
43 Map<String, List<ComponentInstanceInput>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
44 return toscaOperationFacade.addComponentInstanceInputsToComponent(component, instProperties);
48 Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
49 log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
50 return component.getComponentInstanceById(propertiesOwnerId);
54 void addPropertiesListToInput(ComponentInstanceInput declaredProp, InputDefinition input) {
55 List<ComponentInstanceInput> inputsValueList = input.getInputs();
56 if(inputsValueList == null) {
57 inputsValueList = new ArrayList<>(); // adding the property with the new value for UI
59 inputsValueList.add(declaredProp);
60 input.setInputs(inputsValueList);
64 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
65 List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
66 if (isEmpty(componentInstanceInputsByInputId)) {
67 return StorageOperationStatus.OK;
69 componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
70 return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
74 InputDefinition createInputFromProperty(String componentId, ComponentInstance propertiesOwner, String inputName, ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
75 InputDefinition inputFromProperty = super.createInputFromProperty(componentId, propertiesOwner, inputName, propInput, prop);
76 Component propertiesOwnerNodeType = getInstanceOriginType(propertiesOwner);
77 enrichInputWithAnnotations(prop, inputFromProperty, propertiesOwnerNodeType);
78 return inputFromProperty;
81 private void enrichInputWithAnnotations(PropertyDataDefinition prop, InputDefinition inputFromProperty, Component propertiesOwnerNodeType) {
82 List<Annotation> inputAnnotations = getInputAnnotations(propertiesOwnerNodeType, prop.getName());
83 if(!isEmpty(inputAnnotations)){
84 inputFromProperty.setAnnotations(inputAnnotations);
88 private Component getInstanceOriginType(ComponentInstance propertiesOwner) {
89 return toscaOperationFacade.getToscaElement(propertiesOwner.getActualComponentUid(), getFilterComponentInputs())
91 .on(err -> exceptionUtils.rollBackAndThrow(err, propertiesOwner.getActualComponentUid()));
94 private ComponentParametersView getFilterComponentInputs() {
95 ComponentParametersView filterInputs = new ComponentParametersView(true);
96 filterInputs.setIgnoreInputs(false);