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