2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
23 package org.openecomp.sdc.be.components.scheduledtasks;
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;
47 import java.util.HashMap;
48 import java.util.List;
51 @Component("componentsCleanBusinessLogic")
52 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
54 private final ResourceBusinessLogic resourceBusinessLogic;
55 private final ServiceBusinessLogic serviceBusinessLogic;
58 static final String DELETE_LOCKER = "DELETE_LOCKER";
60 private static final Logger log = Logger.getLogger(ComponentsCleanBusinessLogic.class.getName());
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;
77 public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean){
78 return cleanComponents(componentsToClean, false);
81 public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean, boolean isAlreadyLocked) {
83 Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<>();
85 boolean isLockSucceeded = false;
86 log.trace("start cleanComponents");
88 if (!isAlreadyLocked) {
89 //lock if the delete node is not locked yet
90 isLockSucceeded = !isDeleteOperationLockFailed();
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)));
100 processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
103 processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
106 log.debug("{} component type does not have cleaning method defined", type);
112 if (!isAlreadyLocked && isLockSucceeded) {
113 unlockDeleteOperation();
117 log.trace("end cleanComponents");
118 return cleanedComponents;
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());
126 log.debug("list of deleted components - type {}: {}", type, deleteMarkedResources.left().value());
128 cleanedComponents.put(type, deleteMarkedResources);
131 public StorageOperationStatus lockDeleteOperation() {
132 StorageOperationStatus result = graphLockOperation.lockComponentByName(DELETE_LOCKER, NodeTypeEnum.Component);
133 log.info("Lock cleanup operation is done with result = {}", result);
137 public StorageOperationStatus unlockDeleteOperation() {
138 StorageOperationStatus result = graphLockOperation.unlockComponentByName(DELETE_LOCKER, "", NodeTypeEnum.Component);
139 log.info("Unlock cleanup operation is done with result = {}", result);
143 public boolean isDeleteOperationLockFailed() {
144 return lockDeleteOperation() != StorageOperationStatus.OK;