aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArchiveBusinessLogic.java
blob: 3806eed0dcc0a07dd151ca29e988cc619aaa4bf4 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 * Modifications copyright (c) 2019 Nokia
 * ================================================================================
 */
package org.openecomp.sdc.be.components.impl;

import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.wrapWithTryCatch;

import com.google.common.annotations.VisibleForTesting;
import fj.data.Either;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.validation.AccessValidations;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.facade.operations.CatalogOperation;
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.jsonjanusgraph.operations.ArchiveOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.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 org.openecomp.sdc.exception.ResponseFormat;

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

    private static final Logger log = Logger.getLogger(ArchiveBusinessLogic.class.getName());
    private final JanusGraphDao janusGraphDao;
    private final AccessValidations accessValidations;
    private final ArchiveOperation archiveOperation;
    private final ToscaOperationFacade toscaOperationFacade;
    private final ComponentsUtils componentUtils;
    private final CatalogOperation catalogOperations;

    public ArchiveBusinessLogic(JanusGraphDao janusGraphDao, AccessValidations accessValidations, ArchiveOperation archiveOperation,
                                ToscaOperationFacade tof, ComponentsUtils componentsUtils, CatalogOperation catalogOperations) {
        this.janusGraphDao = janusGraphDao;
        this.accessValidations = accessValidations;
        this.archiveOperation = archiveOperation;
        this.toscaOperationFacade = tof;
        this.componentUtils = componentsUtils;
        this.catalogOperations = catalogOperations;
    }

    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()) {
            ActionStatus status = result.right().value();
            if (ActionStatus.CANNOT_ARCHIVE_SYSTEM_DEPLOYED_RESOURCES.equals(status)) {
                throw new ByActionStatusComponentException(status, containerComponentType, componentId);
            }
            throw new ByActionStatusComponentException(status, componentId);
        }
        this.auditAction(ArchiveOperation.Action.ARCHIVE, result.left().value(), user, containerComponentType);
        // Send Archive Notification To Facade
        wrapWithTryCatch(() -> sendNotificationToFacade(componentId, ChangeTypeEnum.ARCHIVE));
    }

    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 ByActionStatusComponentException(result.right().value(), componentId);
        }
        this.auditAction(ArchiveOperation.Action.RESTORE, result.left().value(), user, containerComponentType);
        // Send Archive Notification To Facade
        wrapWithTryCatch(() -> sendNotificationToFacade(componentId, ChangeTypeEnum.RESTORE));
    }

    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 == 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 {
            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 {
            janusGraphDao.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);
                });
            }
        }
    }

    protected Either<Component, ResponseFormat> sendNotificationToFacade(String componentId, ChangeTypeEnum changeStatus) {
        if (log.isDebugEnabled()) {
            log.debug("build {} notification for facade start", changeStatus.name());
        }
        Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(componentId);
        Component component = toscaElement.left().value();
        ActionStatus status = catalogOperations.updateCatalog(changeStatus, component);
        if (status != ActionStatus.OK) {
            return Either.right(componentUtils.getResponseFormat(status));
        }
        return Either.left(component);
    }
}