summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArchiveBusinessLogic.java
blob: 70898fbec2e0f1cdc49c7ff2fee82cb5657e1b90 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.openecomp.sdc.be.components.impl;

import com.google.common.annotations.VisibleForTesting;
import fj.data.Either;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.validation.AccessValidations;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.catalog.CatalogComponent;
import org.openecomp.sdc.be.model.jsontitan.operations.ArchiveOperation;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;

import java.util.*;
import java.util.stream.Collectors;

@org.springframework.stereotype.Component
public class ArchiveBusinessLogic {

    private static final Logger log = Logger.getLogger(ArchiveBusinessLogic.class.getName());

    private final TitanDao titanDao;
    private final AccessValidations accessValidations;
    private final ArchiveOperation archiveOperation;
    private final ToscaOperationFacade toscaOperationFacade;
    private final ComponentsUtils componentUtils;

    public ArchiveBusinessLogic(TitanDao titanDao, AccessValidations accessValidations, ArchiveOperation archiveOperation, ToscaOperationFacade tof, ComponentsUtils componentsUtils) {
        this.titanDao = titanDao;
        this.accessValidations = accessValidations;
        this.archiveOperation = archiveOperation;
        this.toscaOperationFacade = tof;
        this.componentUtils = componentsUtils;
    }

    public void archiveComponent(String containerComponentType, String userId, String componentId) {
        User user = accessValidations.userIsAdminOrDesigner(userId, containerComponentType + "_ARCHIVE");
        Either<List<String>, ActionStatus> result = this.archiveOperation.archiveComponent(componentId);

        if (result.isRight()){
            throw new ComponentException(result.right().value(), componentId);
        }
        this.auditAction(ArchiveOperation.Action.ARCHIVE, result.left().value(), user, containerComponentType);
    }

    public void restoreComponent(String containerComponentType, String userId, String componentId) {
        User user = accessValidations.userIsAdminOrDesigner(userId, containerComponentType + "_RESTORE");
        Either<List<String>, ActionStatus> result = this.archiveOperation.restoreComponent(componentId);
        if (result.isRight()){
            throw new ComponentException(result.right().value(), componentId);
        }
        this.auditAction(ArchiveOperation.Action.RESTORE, result.left().value(), user, containerComponentType);
    }

    public List<String> onVspArchive(String userId, List<String> csarUuids){
        return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.ARCHIVE);
    }

    public List<String> onVspRestore(String userId, List<String> csarUuids){
        return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.RESTORE);
    }

    private List<String> onVspArchiveOrRestore(String userId, List<String> csarUuids, ArchiveOperation.Action action) {

        accessValidations.userIsAdminOrDesigner(userId, action.name() + "_VSP");

        ActionStatus actionStatus;
        List<String> failedCsarIDs = new LinkedList<>();

        for (String csarUuid : csarUuids) {
            try {

                if (action.equals(ArchiveOperation.Action.ARCHIVE)) {
                    actionStatus = this.archiveOperation.onVspArchived(csarUuid);
                } else {
                    actionStatus = this.archiveOperation.onVspRestored(csarUuid);
                }

                //If not found VFs with this CSAR ID we still want a success (nothing is actually done)
                if (actionStatus == ActionStatus.RESOURCE_NOT_FOUND) {
                    actionStatus = ActionStatus.OK;
                }

                if (actionStatus != ActionStatus.OK) {
                    failedCsarIDs.add(csarUuid);
                }

            } catch (Exception e) {
                log.error("Failed to handle notification: {} on VSP for csarUuid: {}", action.name(), csarUuid);
                log.error("Exception Thrown:", e);
                failedCsarIDs.add(csarUuid);
            }
        }

        return failedCsarIDs;
    }

    public Map<String, List<CatalogComponent>> getArchiveComponents(String userId, List<OriginTypeEnum> excludeTypes) {
        try {

            accessValidations.validateUserExist(userId, "GET ARCHIVED COMPONENTS");

            Either<List<CatalogComponent>, StorageOperationStatus> components = toscaOperationFacade.getCatalogOrArchiveComponents(false, excludeTypes);
            if (components.isLeft()) {
                List<CatalogComponent> comps = components.left().value();
                return comps.stream().collect(Collectors.groupingBy(cmpt -> ComponentTypeEnum.findParamByType(cmpt.getComponentType())));
            } else {
                log.info("No components found");
                return new HashMap();
            }
        } catch (Exception e){
            log.error("Error fetching archived elements", e);
            throw e;
        }
        finally {
            titanDao.commit();
        }
    }


    @VisibleForTesting
    void auditAction(ArchiveOperation.Action action, List<String> affectedCompIds, User user, String containerComponentType) {
        String comment = String.format("All versions of this component were %s", action == ArchiveOperation.Action.ARCHIVE ? "archived" : "restored");
        HashSet<String> auditDoneUUIDs = new HashSet<>();
        for (String componentId : affectedCompIds){
            Either<Component, StorageOperationStatus> result = toscaOperationFacade.getToscaElement(componentId, new ComponentParametersView());
            if (result.isRight()) {
                log.error(EcompLoggerErrorCode.DATA_ERROR, null, "GetToscaElement",
                        result.right().value().name() + "for component with id {}", componentId);
                continue;
            }
            if (auditDoneUUIDs.add(result.left().value().getUUID())) {
                //a component with this UUID is not added to audit DB/log for current archive/restore operation yet - add to audit DB now
                AuditingActionEnum auditAction = action == ArchiveOperation.Action.ARCHIVE ? AuditingActionEnum.ARCHIVE_COMPONENT : AuditingActionEnum.RESTORE_COMPONENT; //The audit Action
                result.left().foreachDoEffect(
                    c -> {
                        // The archive/restore records have been retrieved from Cassandra using the separate queries.
                        // Setting current version as null allows to avoid appearing same records in ActivityLog twice:
                        // - first time as per current version query
                        //- second type as per archive/restore query
                        c.setVersion(null);
                        componentUtils.auditComponentAdmin(componentUtils.getResponseFormat(ActionStatus.OK), user, c, auditAction, ComponentTypeEnum.findByParamName(containerComponentType), comment);
                    });
            }
        }
    }
}