Fix for radio buttons
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / ComponentInstanceInputsMergeBL.java
1 package org.openecomp.sdc.be.components.merge.property;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.openecomp.sdc.be.dao.api.ActionStatus;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.model.Component;
10 import org.openecomp.sdc.be.model.ComponentInstance;
11 import org.openecomp.sdc.be.model.ComponentInstanceInput;
12 import org.openecomp.sdc.be.model.InputDefinition;
13 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
14 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
15
16 import fj.data.Either;
17
18 @org.springframework.stereotype.Component
19 public class ComponentInstanceInputsMergeBL {
20
21     @javax.annotation.Resource
22     private ToscaOperationFacade toscaOperationFacade;
23
24     @javax.annotation.Resource
25     private ComponentsUtils componentsUtils;
26
27     @javax.annotation.Resource
28     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
29
30     public ActionStatus mergeComponentInstancesInputs(Component oldComponent, Component newComponent) {
31         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = newComponent.getComponentInstancesInputs();
32         if (componentInstancesInputs == null) {
33             return ActionStatus.OK;
34         }
35         componentInstancesInputs.forEach((instanceId, instInputs) -> mergeOldInstanceInputsValues(oldComponent, newComponent, instanceId, instInputs));
36         return updateComponentInstancesInputs(newComponent, componentInstancesInputs);
37     }
38
39     public ActionStatus mergeComponentInstanceInputs(List<ComponentInstanceInput> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
40         List<ComponentInstanceInput> newInstInput = newComponent.safeGetComponentInstanceInput(instanceId);
41         if (newInstInput == null) {
42             return ActionStatus.OK;
43         }
44         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstInput, newComponent.getInputs());
45         return updateComponentInstanceInputs(newComponent, instanceId, newInstInput);
46     }
47
48     private ActionStatus updateComponentInstanceInputs(Component newComponent, String instanceId, List<ComponentInstanceInput> newInstInput) {
49         StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceInputs(newComponent, instanceId, newInstInput);
50         if (storageOperationStatus != StorageOperationStatus.OK) {
51             return componentsUtils.convertFromStorageResponse(storageOperationStatus);
52         }
53         return ActionStatus.OK;
54     }
55
56     private ActionStatus updateComponentInstancesInputs(Component component, Map<String, List<ComponentInstanceInput>> componentInstancesInputs) {
57         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstanceInputsToComponent(componentInstancesInputs, component.getUniqueId());
58         if (mapStorageOperationStatusEither.isRight()) {
59             return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
60         }
61         return ActionStatus.OK;
62     }
63
64     private void mergeOldInstanceInputsValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceInput> instInputs) {
65         ComponentInstance currentCompInstance = newComponent.getComponentInstanceById(instanceId).get();
66         List<ComponentInstanceInput> oldInstInputs = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceInputsByName(currentCompInstance.getName());
67         List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
68         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstInputs, oldInputs, instInputs, newComponent.getInputs());
69     }
70
71
72 }