re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / GroupTypeBusinessLogic.java
1 package org.openecomp.sdc.be.components.impl;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
5 import org.openecomp.sdc.be.components.validation.UserValidations;
6 import org.openecomp.sdc.be.config.ConfigurationManager;
7 import org.openecomp.sdc.be.dao.api.ActionStatus;
8 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
9 import org.openecomp.sdc.be.impl.ComponentsUtils;
10 import org.openecomp.sdc.be.model.GroupTypeDefinition;
11 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
12 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
13 import org.springframework.stereotype.Component;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import static java.util.Collections.emptySet;
20
21 @Component("groupTypeBusinessLogic")
22 public class GroupTypeBusinessLogic {
23
24     private final GroupTypeOperation groupTypeOperation;
25     private final TitanDao titanDao;
26     private final UserValidations userValidations;
27     private final ComponentsUtils componentsUtils;
28
29     public GroupTypeBusinessLogic(GroupTypeOperation groupTypeOperation, TitanDao titanDao, UserValidations userValidations, ComponentsUtils componentsUtils) {
30         this.groupTypeOperation = groupTypeOperation;
31         this.titanDao = titanDao;
32         this.userValidations = userValidations;
33         this.componentsUtils = componentsUtils;
34     }
35
36
37     public List<GroupTypeDefinition> getAllGroupTypes(String userId, String internalComponentType) {
38         try {
39             userValidations.validateUserExists(userId, "get group types", true);
40             Set<String> excludeGroupTypes = getExcludedGroupTypes(internalComponentType);
41             return groupTypeOperation.getAllGroupTypes(excludeGroupTypes);
42         } finally {
43             titanDao.commit();
44         }
45     }
46
47     public GroupTypeDefinition getLatestGroupTypeByType(String groupTypeName) {
48         return groupTypeOperation.getLatestGroupTypeByType(groupTypeName, true)
49                 .left()
50                 .on(e -> failOnGetGroupType(e, groupTypeName));
51     }
52
53     public Set<String> getExcludedGroupTypes(String internalComponentType) {
54         if (StringUtils.isEmpty(internalComponentType)) {
55             return emptySet();
56         }
57         Map<String, Set<String>> excludedGroupTypesMapping = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedGroupTypesMapping();
58         Set<String> excludedTypes = excludedGroupTypesMapping.get(internalComponentType);
59         return excludedTypes == null ? emptySet() : excludedTypes;
60     }
61
62     private GroupTypeDefinition failOnGetGroupType(StorageOperationStatus status, String groupType) {
63         titanDao.rollback();
64         if (status == StorageOperationStatus.NOT_FOUND) {
65             throw new ComponentException(ActionStatus.GROUP_TYPE_IS_INVALID, groupType);
66         } else {
67             throw new ComponentException(ActionStatus.GENERAL_ERROR);
68         }
69     }
70 }