Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceInputsRedeclareHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
23 import org.openecomp.sdc.be.components.merge.input.DeclaredInputsResolver;
24 import org.openecomp.sdc.be.components.merge.input.InputsValuesMergingBusinessLogic;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.elements.Annotation;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.InputDefinition;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.utils.ComponentUtilities;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.stream.Stream;
39
40 import static java.util.Collections.singletonMap;
41 import static java.util.stream.Collectors.toList;
42 import static org.apache.commons.collections.CollectionUtils.isEmpty;
43 import static org.openecomp.sdc.be.dao.utils.MapUtil.toMap;
44 import static org.openecomp.sdc.be.utils.PropertyDefinitionUtils.resolveGetInputProperties;
45
46 @org.springframework.stereotype.Component
47 public class ComponentInstanceInputsRedeclareHandler {
48
49     private static final Logger log = Logger.getLogger(ComponentInstanceInputsRedeclareHandler.class);
50     private final DeclaredInputsResolver declaredInputsResolver;
51     private final ToscaOperationFacade toscaOperationFacade;
52     private final ComponentsUtils componentsUtils;
53     private final InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;
54     
55     public ComponentInstanceInputsRedeclareHandler(DeclaredInputsResolver declaredInputsResolver, ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic) {
56         this.declaredInputsResolver = declaredInputsResolver;
57         this.toscaOperationFacade = toscaOperationFacade;
58         this.componentsUtils = componentsUtils;
59         this.inputsValuesMergingBusinessLogic = inputsValuesMergingBusinessLogic;
60     }
61
62     ActionStatus redeclareComponentInputsForInstance(Component container, String newInstanceId, Component newInstanceOriginType, List<InputDefinition> oldInputs) {
63         log.debug("#redeclareComponentInputsForInstance - getting inputs that were previously declared from instance {} and setting on current component {}", newInstanceId, container.getUniqueId());
64         Map<String, List<PropertyDataDefinition>> allPropertiesForInstance = getAllGetPropertiesForInstance(container, newInstanceId);
65         List<InputDefinition> previouslyDeclaredInputs = declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(oldInputs, container, allPropertiesForInstance);
66         inputsValuesMergingBusinessLogic.mergeComponentInputs(oldInputs, previouslyDeclaredInputs);
67         Map<String, List<PropertyDataDefinition>> getInputProperties = resolveGetInputProperties(allPropertiesForInstance);
68         updateInputsAnnotations(getInputProperties.get(newInstanceId), newInstanceOriginType, previouslyDeclaredInputs);
69
70         return updateInputs(container.getUniqueId(), previouslyDeclaredInputs);
71     }
72
73     private void updateInputsAnnotations(List<PropertyDataDefinition> instanceProps, Component newInstanceOriginType, List<InputDefinition> previouslyDeclaredInputs) {
74         Map<String, PropertyDataDefinition> instancePropsById = toMap(instanceProps, PropertyDataDefinition::getUniqueId);
75         for (InputDefinition input : previouslyDeclaredInputs) {
76             List<Annotation> originPropInputAnnotations = getAnnotationsFromOriginType(newInstanceOriginType, input.getPropertyId(), instancePropsById);
77             if(!isEmpty(originPropInputAnnotations)){
78                 input.setAnnotations(originPropInputAnnotations);
79             }
80         }
81     }
82
83     private List<Annotation> getAnnotationsFromOriginType(Component originType, String propertyId, Map<String, PropertyDataDefinition> instancePropsById) {
84         PropertyDataDefinition instanceProp = instancePropsById.get(propertyId);
85         String originPropInputName = instanceProp.getName();
86         return ComponentUtilities.getInputAnnotations(originType, originPropInputName);
87     }
88
89     private Map<String, List<PropertyDataDefinition>> getAllGetPropertiesForInstance(Component newComponent, String instanceId) {
90         List<PropertyDataDefinition> allInstanceProps = Stream.of(newComponent.safeGetComponentInstanceProperties(instanceId),
91                 newComponent.safeGetComponentInstanceInput(instanceId))
92                 .flatMap(Collection::stream)
93                 .map(PropertyDataDefinition::new)
94                 .collect(toList());
95         return singletonMap(instanceId, allInstanceProps);
96     }
97
98     private ActionStatus updateInputs(String containerId, List<InputDefinition> inputsToUpdate) {
99         log.debug("#updateInputs - updating inputs for container {}", containerId);
100         return toscaOperationFacade.updateInputsToComponent(inputsToUpdate, containerId)
101                 .either(updatedInputs -> ActionStatus.OK,
102                         componentsUtils::convertFromStorageResponse);
103     }
104
105 }