03dcf766198384bae81e475066b5f38a1a61b434
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components.scheduledtasks;
24
25 import com.google.common.annotations.VisibleForTesting;
26 import fj.data.Either;
27 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
28 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
30 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
31 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
36 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
37 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
38 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
39 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
46
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 @Component("componentsCleanBusinessLogic")
52 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
53
54     private final ResourceBusinessLogic resourceBusinessLogic;
55     private final ServiceBusinessLogic serviceBusinessLogic;
56
57     @VisibleForTesting
58     static final String DELETE_LOCKER = "DELETE_LOCKER";
59
60     private static final Logger log = Logger.getLogger(ComponentsCleanBusinessLogic.class.getName());
61
62     @Autowired
63     public ComponentsCleanBusinessLogic(IElementOperation elementDao,
64         IGroupOperation groupOperation,
65         IGroupInstanceOperation groupInstanceOperation,
66         IGroupTypeOperation groupTypeOperation,
67         InterfaceOperation interfaceOperation,
68         InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ResourceBusinessLogic resourceBusinessLogic,
69         ServiceBusinessLogic serviceBusinessLogic,
70         ArtifactsOperations artifactToscaOperation) {
71         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
72             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
73         this.resourceBusinessLogic = resourceBusinessLogic;
74         this.serviceBusinessLogic = serviceBusinessLogic;
75     }
76
77     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean){
78         return cleanComponents(componentsToClean, false);
79     }
80
81     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean, boolean isAlreadyLocked) {
82
83         Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<>();
84
85         boolean isLockSucceeded = false;
86         log.trace("start cleanComponents");
87         try {
88             if (!isAlreadyLocked) {
89                 //lock if the delete node is not locked yet
90                 isLockSucceeded = !isDeleteOperationLockFailed();
91             }
92             for (NodeTypeEnum type : componentsToClean) {
93                 if (!isAlreadyLocked && !isLockSucceeded) {
94                     log.info("{}s won't be deleted as another process is locking the delete operation", type.getName());
95                     cleanedComponents.put(type, Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)));
96                     break;
97                 }
98                 switch (type) {
99                     case Resource:
100                         processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
101                         break;
102                     case Service:
103                         processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
104                         break;
105                     default:
106                         log.debug("{} component type does not have cleaning method defined", type);
107                         break;
108                 }
109             }
110         }
111         finally {
112             if (!isAlreadyLocked && isLockSucceeded) {
113                 unlockDeleteOperation();
114             }
115         }
116
117         log.trace("end cleanComponents");
118         return cleanedComponents;
119     }
120
121     private void processDeletionForType(Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents, NodeTypeEnum type, ComponentBusinessLogic componentBusinessLogic) {
122         Either<List<String>, ResponseFormat> deleteMarkedResources = componentBusinessLogic.deleteMarkedComponents();
123         if (deleteMarkedResources.isRight()) {
124             log.debug("failed to clean deleted components of type {}. error: {}", type, deleteMarkedResources.right().value().getFormattedMessage());
125         } else {
126             log.debug("list of deleted components - type {}: {}", type, deleteMarkedResources.left().value());
127         }
128         cleanedComponents.put(type, deleteMarkedResources);
129     }
130
131     public StorageOperationStatus lockDeleteOperation() {
132         StorageOperationStatus result = graphLockOperation.lockComponentByName(DELETE_LOCKER, NodeTypeEnum.Component);
133         log.info("Lock cleanup operation is done with result = {}", result);
134         return result;
135     }
136
137     public StorageOperationStatus unlockDeleteOperation() {
138         StorageOperationStatus result = graphLockOperation.unlockComponentByName(DELETE_LOCKER, "", NodeTypeEnum.Component);
139         log.info("Unlock cleanup operation is done with result = {}", result);
140         return result;
141     }
142
143     public boolean isDeleteOperationLockFailed() {
144         return lockDeleteOperation() != StorageOperationStatus.OK;
145     }
146
147 }