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.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;
46 import java.util.HashMap;
47 import java.util.List;
50 @Component("componentsCleanBusinessLogic")
51 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
53 private final ResourceBusinessLogic resourceBusinessLogic;
54 private final ServiceBusinessLogic serviceBusinessLogic;
57 static final String DELETE_LOCKER = "DELETE_LOCKER";
59 private static final Logger log = Logger.getLogger(ComponentsCleanBusinessLogic.class.getName());
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;
76 public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean){
77 return cleanComponents(componentsToClean, false);
80 public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean, boolean isAlreadyLocked) {
82 Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<>();
84 boolean isLockSucceeded = false;
85 log.trace("start cleanComponents");
87 if (!isAlreadyLocked) {
88 //lock if the delete node is not locked yet
89 isLockSucceeded = !isDeleteOperationLockFailed();
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)));
99 processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
102 processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
105 log.debug("{} component type does not have cleaning method defined", type);
111 if (!isAlreadyLocked && isLockSucceeded) {
112 unlockDeleteOperation();
116 log.trace("end cleanComponents");
117 return cleanedComponents;
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());
125 log.debug("list of deleted components - type {}: {}", type, deleteMarkedResources.left().value());
127 cleanedComponents.put(type, deleteMarkedResources);
130 public StorageOperationStatus lockDeleteOperation() {
131 StorageOperationStatus result = graphLockOperation.lockComponentByName(DELETE_LOCKER, NodeTypeEnum.Component);
132 log.info("Lock cleanup operation is done with result = {}", result);
136 public StorageOperationStatus unlockDeleteOperation() {
137 StorageOperationStatus result = graphLockOperation.unlockComponentByName(DELETE_LOCKER, "", NodeTypeEnum.Component);
138 log.info("Unlock cleanup operation is done with result = {}", result);
142 public boolean isDeleteOperationLockFailed() {
143 return lockDeleteOperation() != StorageOperationStatus.OK;