re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / ComponentInstancePropertyDeclarator.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.openecomp.sdc.common.log.wrappers.Logger;
16
17 import java.util.*;
18
19 @org.springframework.stereotype.Component
20 public class ComponentInstancePropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceProperty> {
21
22     private static final Logger log = Logger.getLogger(ComponentInstancePropertyDeclarator.class);
23     private ToscaOperationFacade toscaOperationFacade;
24     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
25
26     public ComponentInstancePropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
27         super(componentsUtils, propertyOperation);
28         this.toscaOperationFacade = toscaOperationFacade;
29         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
30     }
31
32     @Override
33     ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
34         return new ComponentInstanceProperty(prop);
35     }
36
37     @Override
38     Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceProperty> properties) {
39         log.debug("#updatePropertiesValues - updating component instance properties for instance {} on component {}", cmptInstanceId, component.getUniqueId());
40         Map<String, List<ComponentInstanceProperty>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
41         return toscaOperationFacade.addComponentInstancePropertiesToComponent(component, instProperties);
42     }
43
44     @Override
45     Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
46         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
47         return component.getComponentInstanceById(propertiesOwnerId);
48     }
49
50     @Override
51     void addPropertiesListToInput(ComponentInstanceProperty declaredProp, InputDefinition input) {
52         List<ComponentInstanceProperty> propertiesList = input.getProperties();
53         if(propertiesList == null) {
54             propertiesList = new ArrayList<>(); // adding the property with the new value for UI
55         }
56         propertiesList.add(declaredProp);
57         input.setProperties(propertiesList);
58     }
59
60     @Override
61     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
62         List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, input.getUniqueId());
63         if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
64             return StorageOperationStatus.OK;
65         }
66         componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input, cmptInstanceProperty, cmptInstanceProperty.getPath()));
67         return toscaOperationFacade.updateComponentInstanceProperties(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(), componentInstancePropertiesDeclaredAsInput);
68     }
69
70 }