Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstancePropertiesMergeBL.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 fj.data.Either;
24 import org.openecomp.sdc.be.components.merge.VspComponentsMergeCommand;
25 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.springframework.core.annotation.Order;
35
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.function.Function;
40 import java.util.stream.Collectors;
41
42 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
43
44 @org.springframework.stereotype.Component
45 @Order(ANY_ORDER_COMMAND)
46 public class ComponentInstancePropertiesMergeBL implements VspComponentsMergeCommand {
47
48     private final ToscaOperationFacade toscaOperationFacade;
49     private final ComponentsUtils componentsUtils;
50     private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
51
52     public ComponentInstancePropertiesMergeBL(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
53         this.toscaOperationFacade = toscaOperationFacade;
54         this.componentsUtils = componentsUtils;
55         this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
56     }
57
58     @Override
59     public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
60         Map<String, List<ComponentInstanceProperty>> newInstProps = currentComponent.getComponentInstancesProperties();
61         if (newInstProps == null) {
62             return ActionStatus.OK;
63         }
64         Map<String, String> currComponentNames = getComponentNameByUniqueId(currentComponent);
65         Map<String, String> prevComponentUniqueIds = getComponentUniqueIdByName(prevComponent);
66         
67         newInstProps.forEach((instanceId, newProps) -> {
68             String instanceName = currComponentNames.get(instanceId);
69             String oldInstanceId = prevComponentUniqueIds.get(instanceName);
70             
71             mergeOldInstancePropertiesValues(prevComponent, currentComponent, oldInstanceId, newProps);
72         });
73         return updateComponentInstancesProperties(currentComponent, newInstProps);
74     }
75
76     @Override
77     public String description() {
78         return "merge component instance properties";
79     }
80
81
82     public ActionStatus mergeComponentInstanceProperties(List<ComponentInstanceProperty> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
83         List<ComponentInstanceProperty> newInstProps = newComponent.safeGetComponentInstanceProperties(instanceId);
84         if (newInstProps == null) {
85             return ActionStatus.OK;
86         }
87         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstProps, newComponent.getInputs());
88         return updateComponentInstanceProperties(newComponent, instanceId, newInstProps);
89     }
90     
91     private static Map<String, String> getComponentNameByUniqueId(Component component) {
92         return asMap(component, ComponentInstance::getUniqueId, ComponentInstance::getName);
93     }
94     
95     private static Map<String, String> getComponentUniqueIdByName(Component component) {
96         return asMap(component, ComponentInstance::getName, ComponentInstance::getUniqueId);
97     }
98     
99     private static Map<String, String> asMap(Component component, Function<? super ComponentInstance, ? extends String> keyMapper, Function<? super ComponentInstance, ? extends String> valueMapper) {
100         return component.safeGetComponentInstances().stream().
101             collect(Collectors.toMap(keyMapper, valueMapper));
102     }
103
104     private void mergeOldInstancePropertiesValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceProperty> newProps) {
105         List<ComponentInstanceProperty> oldInstProperties = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceProperties(instanceId);
106         List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
107         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProperties, oldInputs, newProps, newComponent.getInputs());
108     }
109
110     private ActionStatus updateComponentInstancesProperties(Component newComponent, Map<String, List<ComponentInstanceProperty>> newInstProps) {
111         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstancePropsToComponent(newInstProps, newComponent.getUniqueId());
112         if (mapStorageOperationStatusEither.isRight()) {
113             return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
114         }
115         return ActionStatus.OK;
116     }
117
118     private ActionStatus updateComponentInstanceProperties(Component component, String instanceId, List<ComponentInstanceProperty> newInstProps) {
119         StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceProperties(component, instanceId, newInstProps);
120         if (storageOperationStatus != StorageOperationStatus.OK) {
121             return componentsUtils.convertFromStorageResponse(storageOperationStatus);
122         }
123         return ActionStatus.OK;
124     }
125
126 }