re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ArchiveBusinessLogic.java
1 package org.openecomp.sdc.be.components.impl;
2
3 import com.google.common.annotations.VisibleForTesting;
4 import fj.data.Either;
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;
22
23 import java.util.*;
24 import java.util.stream.Collectors;
25
26 @org.springframework.stereotype.Component
27 public class ArchiveBusinessLogic {
28
29     private static final Logger log = Logger.getLogger(ArchiveBusinessLogic.class.getName());
30
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;
36
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;
43     }
44
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);
48
49         if (result.isRight()){
50             throw new ComponentException(result.right().value(), componentId);
51         }
52         this.auditAction(ArchiveOperation.Action.ARCHIVE, result.left().value(), user, containerComponentType);
53     }
54
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);
60         }
61         this.auditAction(ArchiveOperation.Action.RESTORE, result.left().value(), user, containerComponentType);
62     }
63
64     public List<String> onVspArchive(String userId, List<String> csarUuids){
65         return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.ARCHIVE);
66     }
67
68     public List<String> onVspRestore(String userId, List<String> csarUuids){
69         return this.onVspArchiveOrRestore(userId, csarUuids, ArchiveOperation.Action.RESTORE);
70     }
71
72     private List<String> onVspArchiveOrRestore(String userId, List<String> csarUuids, ArchiveOperation.Action action) {
73
74         accessValidations.userIsAdminOrDesigner(userId, action.name() + "_VSP");
75
76         ActionStatus actionStatus;
77         List<String> failedCsarIDs = new LinkedList<>();
78
79         for (String csarUuid : csarUuids) {
80             try {
81
82                 if (action.equals(ArchiveOperation.Action.ARCHIVE)) {
83                     actionStatus = this.archiveOperation.onVspArchived(csarUuid);
84                 } else {
85                     actionStatus = this.archiveOperation.onVspRestored(csarUuid);
86                 }
87
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;
91                 }
92
93                 if (actionStatus != ActionStatus.OK) {
94                     failedCsarIDs.add(csarUuid);
95                 }
96
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);
101             }
102         }
103
104         return failedCsarIDs;
105     }
106
107     public Map<String, List<CatalogComponent>> getArchiveComponents(String userId, List<OriginTypeEnum> excludeTypes) {
108         try {
109
110             accessValidations.validateUserExist(userId, "GET ARCHIVED COMPONENTS");
111
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())));
116             } else {
117                 log.info("No components found");
118                 return new HashMap();
119             }
120         } catch (Exception e){
121             log.error("Error fetching archived elements", e);
122             throw e;
123         }
124         finally {
125             titanDao.commit();
126         }
127     }
128
129
130     @VisibleForTesting
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);
139                 continue;
140             }
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(
145                     c -> {
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
150                         c.setVersion(null);
151                         componentUtils.auditComponentAdmin(componentUtils.getResponseFormat(ActionStatus.OK), user, c, auditAction, ComponentTypeEnum.findByParamName(containerComponentType), comment);
152                     });
153             }
154         }
155     }
156 }