Sync Integ to Master
[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 fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
28 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
29 import org.openecomp.sdc.exception.ResponseFormat;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 @Component("componentsCleanBusinessLogic")
40 public class ComponentsCleanBusinessLogic extends BaseBusinessLogic {
41
42     @Autowired
43     private ResourceBusinessLogic resourceBusinessLogic;
44
45     @Autowired
46     private ServiceBusinessLogic serviceBusinessLogic;
47
48     private static final Logger log = LoggerFactory.getLogger(ComponentsCleanBusinessLogic.class);
49
50     public Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponents(List<NodeTypeEnum> componentsToClean) {
51
52         Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents = new HashMap<NodeTypeEnum, Either<List<String>, ResponseFormat>>();
53
54         log.trace("start cleanComponents");
55         for (NodeTypeEnum type : componentsToClean) {
56             switch (type) {
57             case Resource:
58                 processDeletionForType(cleanedComponents, NodeTypeEnum.Resource, resourceBusinessLogic);
59                 break;
60             case Service:
61                 processDeletionForType(cleanedComponents, NodeTypeEnum.Service, serviceBusinessLogic);
62                 break;
63             default:
64                 log.debug("{} component type does not have cleaning method defined", type);
65                 break;
66             }
67         }
68
69         log.trace("end cleanComponents");
70         return cleanedComponents;
71     }
72
73     private void processDeletionForType(Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanedComponents, NodeTypeEnum type, ComponentBusinessLogic componentBusinessLogic) {
74         Either<List<String>, ResponseFormat> deleteMarkedResources = componentBusinessLogic.deleteMarkedComponents();
75         if (deleteMarkedResources.isRight()) {
76             log.debug("failed to clean deleted components of type {}. error: {}", type, deleteMarkedResources.right().value().getFormattedMessage());
77         } else {
78             if (log.isDebugEnabled()) {
79                 StringBuilder sb = new StringBuilder("list of deleted components - type " + type + ": ");
80                 for (String id : deleteMarkedResources.left().value()) {
81                     sb.append(id).append(", ");
82                 }
83                 log.debug(sb.toString());
84             }
85         }
86         cleanedComponents.put(type, deleteMarkedResources);
87     }
88 }