Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / ComponentInstancePropertyDecelerator.java
1 package org.openecomp.sdc.be.components.property;
2
3 import fj.data.Either;
4 import org.apache.commons.collections.CollectionUtils;
5 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
6 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
7 import org.openecomp.sdc.be.impl.ComponentsUtils;
8 import org.openecomp.sdc.be.model.Component;
9 import org.openecomp.sdc.be.model.ComponentInstance;
10 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
11 import org.openecomp.sdc.be.model.InputDefinition;
12 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
13 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
14 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23
24 @org.springframework.stereotype.Component
25 public class ComponentInstancePropertyDecelerator extends DefaultPropertyDecelerator<ComponentInstance, ComponentInstanceProperty> {
26
27     private static final Logger log = LoggerFactory.getLogger(ComponentInstancePropertyDecelerator.class);
28     private ToscaOperationFacade toscaOperationFacade;
29     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
30
31     public ComponentInstancePropertyDecelerator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
32         super(componentsUtils, propertyOperation);
33         this.toscaOperationFacade = toscaOperationFacade;
34         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
35     }
36
37     @Override
38     ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
39         return new ComponentInstanceProperty(prop);
40     }
41
42     @Override
43     Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceProperty> properties) {
44         log.debug("#updatePropertiesValues - updating component instance properties for instance {} on component {}", cmptInstanceId, component.getUniqueId());
45         Map<String, List<ComponentInstanceProperty>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
46         return toscaOperationFacade.addComponentInstancePropertiesToComponent(component, instProperties, cmptInstanceId);
47     }
48
49     @Override
50     Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
51         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
52         return component.getComponentInstanceById(propertiesOwnerId);
53     }
54
55     @Override
56     void addPropertiesListToInput(ComponentInstanceProperty declaredProp, PropertyDataDefinition originalProp, InputDefinition input) {
57         List<ComponentInstanceProperty> propertiesList = input.getProperties();
58         if(propertiesList == null) {
59             propertiesList = new ArrayList<>(); // adding the property with the new value for UI
60         }
61         propertiesList.add(declaredProp);
62         input.setProperties(propertiesList);
63     }
64
65     @Override
66     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
67         List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, input.getUniqueId());
68         if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
69             return StorageOperationStatus.OK;
70         }
71         componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input, cmptInstanceProperty, cmptInstanceProperty.getPath()));
72         return toscaOperationFacade.updateComponentInstanceProperties(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(), componentInstancePropertiesDeclaredAsInput);
73     }
74
75 }