re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / scheduledtasks / ComponentsCleanBusinessLogic.java
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  */
20
21 package org.openecomp.sdc.be.components.scheduledtasks;
22
23 import com.google.common.annotations.VisibleForTesting;
24 import fj.data.Either;
25 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
28 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 @Component("componentsCleanBusinessLogic")
42 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
43
44     @Autowired
45     private ResourceBusinessLogic resourceBusinessLogic;
46
47     @Autowired
48     private ServiceBusinessLogic serviceBusinessLogic;
49
50     @VisibleForTesting
51     public static final String DELETE_LOCKER = "DELETE_LOCKER";
52
53     private static final Logger log = Logger.getLogger(ComponentsCleanBusinessLogic.class.getName());
54
55     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean){
56         return cleanComponents(componentsToClean, false);
57     }
58
59     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean, boolean isAlreadyLocked) {
60
61         Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<>();
62
63         boolean isLockSucceeded = false;
64         log.trace("start cleanComponents");
65         try {
66             if (!isAlreadyLocked) {
67                 //lock if the delete node is not locked yet
68                 isLockSucceeded = !isDeleteOperationLockFailed();
69             }
70             for (NodeTypeEnum type : componentsToClean) {
71                 if (!isAlreadyLocked && !isLockSucceeded) {
72                     log.info("{}s won't be deleted as another process is locking the delete operation", type.getName());
73                     cleanedComponents.put(type, Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)));
74                     break;
75                 }
76                 switch (type) {
77                     case Resource:
78                         processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
79                         break;
80                     case Service:
81                         processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
82                         break;
83                     default:
84                         log.debug("{} component type does not have cleaning method defined", type);
85                         break;
86                 }
87             }
88         }
89         finally {
90             if (!isAlreadyLocked && isLockSucceeded) {
91                 unlockDeleteOperation();
92             }
93         }
94
95         log.trace("end cleanComponents");
96         return cleanedComponents;
97     }
98
99     private void processDeletionForType(Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents, NodeTypeEnum type, ComponentBusinessLogic componentBusinessLogic) {
100         Either<List<String>, ResponseFormat> deleteMarkedResources = componentBusinessLogic.deleteMarkedComponents();
101         if (deleteMarkedResources.isRight()) {
102             log.debug("failed to clean deleted components of type {}. error: {}", type, deleteMarkedResources.right().value().getFormattedMessage());
103         } else {
104             log.debug("list of deleted components - type {}: {}", type, deleteMarkedResources.left().value());
105         }
106         cleanedComponents.put(type, deleteMarkedResources);
107     }
108
109     public StorageOperationStatus lockDeleteOperation() {
110         StorageOperationStatus result = graphLockOperation.lockComponentByName(DELETE_LOCKER, NodeTypeEnum.Component);
111         log.info("Lock cleanup operation is done with result = {}", result);
112         return result;
113     }
114
115     public StorageOperationStatus unlockDeleteOperation() {
116         StorageOperationStatus result = graphLockOperation.unlockComponentByName(DELETE_LOCKER, "", NodeTypeEnum.Component);
117         log.info("Unlock cleanup operation is done with result = {}", result);
118         return result;
119     }
120
121     public boolean isDeleteOperationLockFailed() {
122         return lockDeleteOperation() != StorageOperationStatus.OK;
123     }
124
125 }