Catalog alignment
[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  * 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.ResourceBusinessLogic;
30 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
35 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
36 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
37 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
38 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.openecomp.sdc.exception.ResponseFormat;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49
50 @Component("componentsCleanBusinessLogic")
51 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
52
53     private final ResourceBusinessLogic resourceBusinessLogic;
54     private final ServiceBusinessLogic serviceBusinessLogic;
55
56     @VisibleForTesting
57     static final String DELETE_LOCKER = "DELETE_LOCKER";
58
59     private static final Logger log = Logger.getLogger(ComponentsCleanBusinessLogic.class.getName());
60
61     @Autowired
62     public ComponentsCleanBusinessLogic(IElementOperation elementDao,
63         IGroupOperation groupOperation,
64         IGroupInstanceOperation groupInstanceOperation,
65         IGroupTypeOperation groupTypeOperation,
66         InterfaceOperation interfaceOperation,
67         InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ResourceBusinessLogic resourceBusinessLogic,
68         ServiceBusinessLogic serviceBusinessLogic,
69         ArtifactsOperations artifactToscaOperation) {
70         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
71             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
72         this.resourceBusinessLogic = resourceBusinessLogic;
73         this.serviceBusinessLogic = serviceBusinessLogic;
74     }
75
76     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean){
77         return cleanComponents(componentsToClean, false);
78     }
79
80     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean, boolean isAlreadyLocked) {
81
82         Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<>();
83
84         boolean isLockSucceeded = false;
85         log.trace("start cleanComponents");
86         try {
87             if (!isAlreadyLocked) {
88                 //lock if the delete node is not locked yet
89                 isLockSucceeded = !isDeleteOperationLockFailed();
90             }
91             for (NodeTypeEnum type : componentsToClean) {
92                 if (!isAlreadyLocked && !isLockSucceeded) {
93                     log.info("{}s won't be deleted as another process is locking the delete operation", type.getName());
94                     cleanedComponents.put(type, Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)));
95                     break;
96                 }
97                 switch (type) {
98                     case Resource:
99                         processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
100                         break;
101                     case Service:
102                         processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
103                         break;
104                     default:
105                         log.debug("{} component type does not have cleaning method defined", type);
106                         break;
107                 }
108             }
109         }
110         finally {
111             if (!isAlreadyLocked && isLockSucceeded) {
112                 unlockDeleteOperation();
113             }
114         }
115
116         log.trace("end cleanComponents");
117         return cleanedComponents;
118     }
119
120     private void processDeletionForType(Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents, NodeTypeEnum type, ComponentBusinessLogic componentBusinessLogic) {
121         Either<List<String>, ResponseFormat> deleteMarkedResources = componentBusinessLogic.deleteMarkedComponents();
122         if (deleteMarkedResources.isRight()) {
123             log.debug("failed to clean deleted components of type {}. error: {}", type, deleteMarkedResources.right().value().getFormattedMessage());
124         } else {
125             log.debug("list of deleted components - type {}: {}", type, deleteMarkedResources.left().value());
126         }
127         cleanedComponents.put(type, deleteMarkedResources);
128     }
129
130     public StorageOperationStatus lockDeleteOperation() {
131         StorageOperationStatus result = graphLockOperation.lockComponentByName(DELETE_LOCKER, NodeTypeEnum.Component);
132         log.info("Lock cleanup operation is done with result = {}", result);
133         return result;
134     }
135
136     public StorageOperationStatus unlockDeleteOperation() {
137         StorageOperationStatus result = graphLockOperation.unlockComponentByName(DELETE_LOCKER, "", NodeTypeEnum.Component);
138         log.info("Unlock cleanup operation is done with result = {}", result);
139         return result;
140     }
141
142     public boolean isDeleteOperationLockFailed() {
143         return lockDeleteOperation() != StorageOperationStatus.OK;
144     }
145
146 }