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