a60b0f07732f5326432ec33d83a7c8b427174e52
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.instance;
2
3 import org.openecomp.sdc.be.components.merge.input.DeclaredInputsResolver;
4 import org.openecomp.sdc.be.components.merge.input.InputsValuesMergingBusinessLogic;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
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.Component;
10 import org.openecomp.sdc.be.model.InputDefinition;
11 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
12 import org.openecomp.sdc.be.model.utils.ComponentUtilities;
13 import org.openecomp.sdc.common.log.wrappers.Logger;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.stream.Stream;
19
20 import static java.util.Collections.singletonMap;
21 import static java.util.stream.Collectors.toList;
22 import static org.apache.commons.collections.CollectionUtils.isEmpty;
23 import static org.openecomp.sdc.be.dao.utils.MapUtil.toMap;
24
25 @org.springframework.stereotype.Component
26 public class ComponentInstanceInputsRedeclareHandler {
27
28     private static final Logger log = Logger.getLogger(ComponentInstanceInputsRedeclareHandler.class);
29     private final DeclaredInputsResolver declaredInputsResolver;
30     private final ToscaOperationFacade toscaOperationFacade;
31     private final ComponentsUtils componentsUtils;
32     private final InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;
33     
34     public ComponentInstanceInputsRedeclareHandler(DeclaredInputsResolver declaredInputsResolver, ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic) {
35         this.declaredInputsResolver = declaredInputsResolver;
36         this.toscaOperationFacade = toscaOperationFacade;
37         this.componentsUtils = componentsUtils;
38         this.inputsValuesMergingBusinessLogic = inputsValuesMergingBusinessLogic;
39     }
40
41     ActionStatus redeclareComponentInputsForInstance(Component container, String newInstanceId, Component newInstanceOriginType, List<InputDefinition> oldInputs) {
42         log.debug("#redeclareComponentInputsForInstance - getting inputs that were previously declared from instance {} and setting on current component {}", newInstanceId, container.getUniqueId());
43         Map<String, List<PropertyDataDefinition>> allPropertiesForInstance = getAllGetPropertiesForInstance(container, newInstanceId);
44         List<InputDefinition> previouslyDeclaredInputs = declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(oldInputs, container, allPropertiesForInstance);
45         inputsValuesMergingBusinessLogic.mergeComponentInputs(oldInputs, previouslyDeclaredInputs);
46         updateInputsAnnotations(allPropertiesForInstance.get(newInstanceId), newInstanceOriginType, previouslyDeclaredInputs);
47
48         return updateInputs(container.getUniqueId(), previouslyDeclaredInputs);
49     }
50
51     private void updateInputsAnnotations(List<PropertyDataDefinition> instanceProps, Component newInstanceOriginType, List<InputDefinition> previouslyDeclaredInputs) {
52         Map<String, PropertyDataDefinition> instancePropsById = toMap(instanceProps, PropertyDataDefinition::getUniqueId);
53         for (InputDefinition input : previouslyDeclaredInputs) {
54             List<Annotation> originPropInputAnnotations = getAnnotationsFromOriginType(newInstanceOriginType, input.getPropertyId(), instancePropsById);
55             if(!isEmpty(originPropInputAnnotations)){
56                 input.setAnnotations(originPropInputAnnotations);
57             }
58         }
59     }
60
61     private List<Annotation> getAnnotationsFromOriginType(Component originType, String propertyId, Map<String, PropertyDataDefinition> instancePropsById) {
62         PropertyDataDefinition instanceProp = instancePropsById.get(propertyId);
63         String originPropInputName = instanceProp.getName();
64         return ComponentUtilities.getInputAnnotations(originType, originPropInputName);
65     }
66
67     private Map<String, List<PropertyDataDefinition>> getAllGetPropertiesForInstance(Component newComponent, String instanceId) {
68         List<PropertyDataDefinition> allInstanceProps = Stream.of(newComponent.safeGetComponentInstanceProperties(instanceId),
69                 newComponent.safeGetComponentInstanceInput(instanceId))
70                 .flatMap(Collection::stream)
71                 .map(PropertyDataDefinition::new)
72                 .collect(toList());
73         return singletonMap(instanceId, allInstanceProps);
74     }
75
76     private ActionStatus updateInputs(String containerId, List<InputDefinition> inputsToUpdate) {
77         log.debug("#updateInputs - updating inputs for container {}", containerId);
78         return toscaOperationFacade.updateInputsToComponent(inputsToUpdate, containerId)
79                 .either(updatedInputs -> ActionStatus.OK,
80                         componentsUtils::convertFromStorageResponse);
81     }
82
83 }