aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/input/ComponentInputsMergeBL.java
blob: 96ad87c6081f41d74004c1234ef1a3eadbc7e1f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.openecomp.sdc.be.components.merge.input;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.openecomp.sdc.be.components.merge.instance.ComponentsMergeCommand;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.utils.MapUtil;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;

import fj.data.Either;

@org.springframework.stereotype.Component
public class ComponentInputsMergeBL implements ComponentsMergeCommand {

    @javax.annotation.Resource
    private InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;

    @javax.annotation.Resource
    private ToscaOperationFacade toscaOperationFacade;

    @javax.annotation.Resource
    private ComponentsUtils componentsUtils;

    @Override
    public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
        List<InputDefinition> inputsToMerge = currentComponent.getInputs() != null ? currentComponent.getInputs() : new ArrayList<>();
        return this.mergeAndRedeclareComponentInputs(prevComponent, currentComponent, inputsToMerge);
    }

    @Override
    public String description() {
        return "merge component inputs";
    }

    public ActionStatus mergeAndRedeclareComponentInputs(Component prevComponent, Component newComponent, List<InputDefinition> inputsToMerge) {
        mergeInputs(prevComponent, inputsToMerge);
        List<InputDefinition> previouslyDeclaredInputs = inputsValuesMergingBusinessLogic.getPreviouslyDeclaredInputsToMerge(prevComponent, newComponent);
        inputsToMerge.addAll(previouslyDeclaredInputs);
        return updateInputs(newComponent.getUniqueId(), inputsToMerge);
    }

    public ActionStatus mergeComponentInputs(Component prevComponent, Component newComponent, List<InputDefinition> inputsToMerge) {
        mergeInputs(prevComponent, inputsToMerge);
        return updateInputs(newComponent.getUniqueId(), inputsToMerge);
    }

    public ActionStatus redeclareComponentInputsForInstance(List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
        List<InputDefinition> previouslyDeclaredInputs = inputsValuesMergingBusinessLogic.getPreviouslyDeclaredInputsToMerge(oldInputs, newComponent, instanceId);
        return updateInputs(newComponent.getUniqueId(), previouslyDeclaredInputs);
    }

    private void mergeInputs(Component prevComponent, List<InputDefinition> inputsToMerge) {
        Map<String, InputDefinition> oldInputsByName = prevComponent.getInputs() == null ? Collections.emptyMap() : MapUtil.toMap(prevComponent.getInputs(), InputDefinition::getName);
        Map<String, InputDefinition> inputsToMergeByName = MapUtil.toMap(inputsToMerge, InputDefinition::getName);
        inputsValuesMergingBusinessLogic.mergeComponentInputs(oldInputsByName, inputsToMergeByName);
    }

    private ActionStatus updateInputs(String containerId, List<InputDefinition> inputsToUpdate) {
        Either<List<InputDefinition>, StorageOperationStatus> updateInputsEither = toscaOperationFacade.updateInputsToComponent(inputsToUpdate, containerId);
        if (updateInputsEither.isRight()) {
            return componentsUtils.convertFromStorageResponse(updateInputsEither.right().value());
        }
        return ActionStatus.OK;
    }

}