Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / GroupTypeBusinessLogic.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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
26 import org.openecomp.sdc.be.components.validation.UserValidations;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.GroupTypeDefinition;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
34 import org.springframework.stereotype.Component;
35
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39
40 import static java.util.Collections.emptySet;
41
42 @Component("groupTypeBusinessLogic")
43 public class GroupTypeBusinessLogic {
44
45     private final GroupTypeOperation groupTypeOperation;
46     private final JanusGraphDao janusGraphDao;
47     private final UserValidations userValidations;
48     private final ComponentsUtils componentsUtils;
49
50     public GroupTypeBusinessLogic(GroupTypeOperation groupTypeOperation, JanusGraphDao janusGraphDao, UserValidations userValidations, ComponentsUtils componentsUtils) {
51         this.groupTypeOperation = groupTypeOperation;
52         this.janusGraphDao = janusGraphDao;
53         this.userValidations = userValidations;
54         this.componentsUtils = componentsUtils;
55     }
56
57
58     public List<GroupTypeDefinition> getAllGroupTypes(String userId, String internalComponentType) {
59         try {
60             userValidations.validateUserExists(userId);
61             Set<String> excludeGroupTypes = getExcludedGroupTypes(internalComponentType);
62             return groupTypeOperation.getAllGroupTypes(excludeGroupTypes);
63         } finally {
64             janusGraphDao.commit();
65         }
66     }
67
68     public GroupTypeDefinition getLatestGroupTypeByType(String groupTypeName) {
69         return groupTypeOperation.getLatestGroupTypeByType(groupTypeName, true)
70                 .left()
71                 .on(e -> failOnGetGroupType(e, groupTypeName));
72     }
73
74     public Set<String> getExcludedGroupTypes(String internalComponentType) {
75         if (StringUtils.isEmpty(internalComponentType)) {
76             return emptySet();
77         }
78         Map<String, Set<String>> excludedGroupTypesMapping = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedGroupTypesMapping();
79         Set<String> excludedTypes = excludedGroupTypesMapping.get(internalComponentType);
80         return excludedTypes == null ? emptySet() : excludedTypes;
81     }
82
83     private GroupTypeDefinition failOnGetGroupType(StorageOperationStatus status, String groupType) {
84         janusGraphDao.rollback();
85         if (status == StorageOperationStatus.NOT_FOUND) {
86             throw new ByActionStatusComponentException(ActionStatus.GROUP_TYPE_IS_INVALID, groupType);
87         } else {
88             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
89         }
90     }
91 }