Support TOSCA functions in sub properties
[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 static java.util.Collections.emptySet;
25
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
32 import org.openecomp.sdc.be.components.validation.UserValidations;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
36 import org.openecomp.sdc.be.model.GroupTypeDefinition;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
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
49     @Autowired
50     public GroupTypeBusinessLogic(GroupTypeOperation groupTypeOperation, JanusGraphDao janusGraphDao, UserValidations userValidations) {
51         this.groupTypeOperation = groupTypeOperation;
52         this.janusGraphDao = janusGraphDao;
53         this.userValidations = userValidations;
54     }
55
56     public List<GroupTypeDefinition> getAllGroupTypes(String userId, String internalComponentType, String internalComponentModel) {
57         List<GroupTypeDefinition> groupTypes = null;
58         try {
59             userValidations.validateUserExists(userId);
60             Set<String> excludeGroupTypes = getExcludedGroupTypes(internalComponentType);
61             groupTypes = groupTypeOperation.getAllGroupTypes(excludeGroupTypes, internalComponentModel);
62             return groupTypes;
63         } finally {
64             if (CollectionUtils.isNotEmpty(groupTypes)) {
65                 janusGraphDao.commit();
66             }
67         }
68     }
69
70     public GroupTypeDefinition getLatestGroupTypeByType(String groupTypeName, String modelName) {
71         return groupTypeOperation.getLatestGroupTypeByType(groupTypeName, modelName).left().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()
79             .getExcludedGroupTypesMapping();
80         Set<String> excludedTypes = excludedGroupTypesMapping.get(internalComponentType);
81         return excludedTypes == null ? emptySet() : excludedTypes;
82     }
83
84     private GroupTypeDefinition failOnGetGroupType(StorageOperationStatus status, String groupType) {
85         janusGraphDao.rollback();
86         if (status == StorageOperationStatus.NOT_FOUND) {
87             throw new ByActionStatusComponentException(ActionStatus.GROUP_TYPE_IS_INVALID, groupType);
88         } else {
89             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
90         }
91     }
92 }