1 package org.openecomp.sdc.be.components.impl;
3 import com.google.common.annotations.VisibleForTesting;
5 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
6 import org.openecomp.sdc.be.components.validation.AccessValidations;
7 import org.openecomp.sdc.be.dao.api.ActionStatus;
8 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
9 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
10 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
11 import org.openecomp.sdc.be.impl.ComponentsUtils;
12 import org.openecomp.sdc.be.model.Component;
13 import org.openecomp.sdc.be.model.ComponentParametersView;
14 import org.openecomp.sdc.be.model.User;
15 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
16 import org.openecomp.sdc.be.model.jsontitan.operations.ArchiveOperation;
17 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
18 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
19 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
20 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
21 import org.openecomp.sdc.common.log.wrappers.Logger;
24 import java.util.stream.Collectors;
26 @org.springframework.stereotype.Component
27 public class ArchiveBusinessLogic {
29 private static final Logger log = Logger.getLogger(ArchiveBusinessLogic.class.getName());
31 private final TitanDao titanDao;
32 private final AccessValidations accessValidations;
33 private final ArchiveOperation archiveOperation;
34 private final ToscaOperationFacade toscaOperationFacade;
35 private final ComponentsUtils componentUtils;
37 public ArchiveBusinessLogic(TitanDao titanDao, AccessValidations accessValidations, ArchiveOperation archiveOperation, ToscaOperationFacade tof, ComponentsUtils componentsUtils) {
38 this.titanDao = titanDao;
39 this.accessValidations = accessValidations;
40 this.archiveOperation = archiveOperation;
41 this.toscaOperationFacade = tof;
42 this.componentUtils = componentsUtils;
45 public void archiveComponent(String containerComponentType, String userId, String componentId) {
46 User user = accessValidations.userIsAdminOrDesigner(userId, containerComponentType + "_ARCHIVE");
47 Either<List<String>, ActionStatus> result = this.archiveOperation.archiveComponent(componentId);
49 if (result.isRight()){
50 throw new ComponentException(result.right().value(), componentId);
52 this.auditAction(ArchiveOperation.Action.ARCHIVE, result.left().value(), user, containerComponentType);
55 public void restoreComponent(String containerComponentType, String userId, String componentId) {
56 User user = accessValidations.userIsAdminOrDesigner(userId, containerComponentType + "_RESTORE");
57 Either<List<String>, ActionStatus> result = this.archiveOperation.restoreComponent(componentId);
58 if (result.isRight()){
59 throw new ComponentException(result.right().value(), componentId);
61 this.auditAction(ArchiveOperation.Action.RESTORE, result.left().value(), user, containerComponentType);
64 public List<String> onVspArchive(String userId, List<String> csarUuids){
65 return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.ARCHIVE);
68 public List<String> onVspRestore(String userId, List<String> csarUuids){
69 return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.RESTORE);
72 private List<String> onVspArchiveOrRestore(String userId, List<String> csarUuids, ArchiveOperation.Action action) {
74 accessValidations.userIsAdminOrDesigner(userId, action.name() + "_VSP");
76 ActionStatus actionStatus;
77 List<String> failedCsarIDs = new LinkedList<>();
79 for (String csarUuid : csarUuids) {
82 if (action.equals(ArchiveOperation.Action.ARCHIVE)) {
83 actionStatus = this.archiveOperation.onVspArchived(csarUuid);
85 actionStatus = this.archiveOperation.onVspRestored(csarUuid);
88 //If not found VFs with this CSAR ID we still want a success (nothing is actually done)
89 if (actionStatus == ActionStatus.RESOURCE_NOT_FOUND) {
90 actionStatus = ActionStatus.OK;
93 if (actionStatus != ActionStatus.OK) {
94 failedCsarIDs.add(csarUuid);
97 } catch (Exception e) {
98 log.error("Failed to handle notification: {} on VSP for csarUuid: {}", action.name(), csarUuid);
99 log.error("Exception Thrown:", e);
100 failedCsarIDs.add(csarUuid);
104 return failedCsarIDs;
107 public Map<String, List<CatalogComponent>> getArchiveComponents(String userId, List<OriginTypeEnum> excludeTypes) {
110 accessValidations.validateUserExist(userId, "GET ARCHIVED COMPONENTS");
112 Either<List<CatalogComponent>, StorageOperationStatus> components = toscaOperationFacade.getCatalogOrArchiveComponents(false, excludeTypes);
113 if (components.isLeft()) {
114 List<CatalogComponent> comps = components.left().value();
115 return comps.stream().collect(Collectors.groupingBy(cmpt -> ComponentTypeEnum.findParamByType(cmpt.getComponentType())));
117 log.info("No components found");
118 return new HashMap();
120 } catch (Exception e){
121 log.error("Error fetching archived elements", e);
131 void auditAction(ArchiveOperation.Action action, List<String> affectedCompIds, User user, String containerComponentType) {
132 String comment = String.format("All versions of this component were %s", action == ArchiveOperation.Action.ARCHIVE ? "archived" : "restored");
133 HashSet<String> auditDoneUUIDs = new HashSet<>();
134 for (String componentId : affectedCompIds){
135 Either<Component, StorageOperationStatus> result = toscaOperationFacade.getToscaElement(componentId, new ComponentParametersView());
136 if (result.isRight()) {
137 log.error(EcompLoggerErrorCode.DATA_ERROR, null, "GetToscaElement",
138 result.right().value().name() + "for component with id {}", componentId);
141 if (auditDoneUUIDs.add(result.left().value().getUUID())) {
142 //a component with this UUID is not added to audit DB/log for current archive/restore operation yet - add to audit DB now
143 AuditingActionEnum auditAction = action == ArchiveOperation.Action.ARCHIVE ? AuditingActionEnum.ARCHIVE_COMPONENT : AuditingActionEnum.RESTORE_COMPONENT; //The audit Action
144 result.left().foreachDoEffect(
146 // The archive/restore records have been retrieved from Cassandra using the separate queries.
147 // Setting current version as null allows to avoid appearing same records in ActivityLog twice:
148 // - first time as per current version query
149 //- second type as per archive/restore query
151 componentUtils.auditComponentAdmin(componentUtils.getResponseFormat(ActionStatus.OK), user, c, auditAction, ComponentTypeEnum.findByParamName(containerComponentType), comment);