Update vulnerable package dependencies
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / PolicyTypeBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 package org.openecomp.sdc.be.components.impl;
21
22 import static java.util.Collections.emptySet;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
29 import org.openecomp.sdc.be.components.validation.UserValidations;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
37 import org.springframework.stereotype.Component;
38 import org.springframework.transaction.annotation.Transactional;
39
40 @Component
41 public class PolicyTypeBusinessLogic {
42
43     private PolicyTypeOperation policyTypeOperation;
44     private JanusGraphDao janusGraphDao;
45     private ComponentsUtils componentsUtils;
46     private UserValidations userValidations;
47
48     public PolicyTypeBusinessLogic(PolicyTypeOperation policyTypeOperation, JanusGraphDao janusGraphDao, ComponentsUtils componentsUtils,
49                                    UserValidations userValidations) {
50         this.policyTypeOperation = policyTypeOperation;
51         this.janusGraphDao = janusGraphDao;
52         this.componentsUtils = componentsUtils;
53         this.userValidations = userValidations;
54     }
55
56     @Transactional
57     public List<PolicyTypeDefinition> getAllPolicyTypes(String userId, String internalComponentType) {
58         Set<String> excludedPolicyTypes = getExcludedPolicyTypes(internalComponentType);
59         userValidations.validateUserExists(userId);
60         return getPolicyTypes(excludedPolicyTypes);
61     }
62
63     public PolicyTypeDefinition getLatestPolicyTypeByType(String policyTypeName) {
64         return policyTypeOperation.getLatestPolicyTypeByType(policyTypeName).left().on(e -> failOnPolicyType(e, policyTypeName));
65     }
66
67     public Set<String> getExcludedPolicyTypes(String internalComponentType) {
68         if (StringUtils.isEmpty(internalComponentType)) {
69             return emptySet();
70         }
71         Map<String, Set<String>> excludedPolicyTypesMapping = ConfigurationManager.getConfigurationManager().getConfiguration()
72             .getExcludedPolicyTypesMapping();
73         Set<String> excludedTypes = excludedPolicyTypesMapping.get(internalComponentType);
74         return excludedTypes == null ? emptySet() : excludedTypes;
75     }
76
77     private List<PolicyTypeDefinition> getPolicyTypes(Set<String> excludedTypes) {
78         return policyTypeOperation.getAllPolicyTypes(excludedTypes);
79     }
80
81     private PolicyTypeDefinition failOnPolicyType(StorageOperationStatus status, String policyType) {
82         janusGraphDao.rollback();
83         if (status == StorageOperationStatus.INVALID_ID) {
84             throw new ByActionStatusComponentException(ActionStatus.POLICY_TYPE_IS_INVALID, policyType);
85         } else {
86             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
87         }
88     }
89 }