Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / config / ContainerInstanceTypesData.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.model.jsonjanusgraph.config;
20
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.apache.commons.collections4.MapUtils;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class ContainerInstanceTypesData {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(ContainerInstanceTypesData.class);
37     private static final String WILDCARD = "*";
38     private final ConfigurationManager configurationManager;
39
40     public ContainerInstanceTypesData() {
41         this.configurationManager = ConfigurationManager.getConfigurationManager();
42     }
43
44     /**
45      * Checks if a resource instance type is allowed in a Service component.
46      *
47      * @param resourceTypeToCheck the resource instance type that will be added to the container component
48      * @return {@code true} if the resource instance is allowed, {@code false} otherwise
49      */
50     public boolean isAllowedForServiceComponent(final ResourceTypeEnum resourceTypeToCheck) {
51         final List<String> allowedResourceInstanceTypeList = getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
52         if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
53             return false;
54         }
55         return allowedResourceInstanceTypeList.contains(resourceTypeToCheck.getValue());
56     }
57
58     /**
59      * Checks if a resource instance type is allowed for a resource component.
60      *
61      * @param containerComponentResourceType the container component type that the instance will be added
62      * @param resourceToCheck                the resource instance type that will be added to the container component
63      * @return {@code true} if the resource instance is allowed in the container component, {@code false} otherwise
64      */
65     public boolean isAllowedForResourceComponent(final ResourceTypeEnum containerComponentResourceType, final ResourceTypeEnum resourceToCheck) {
66         final List<String> allowedResourceInstanceTypeList = getComponentAllowedList(ComponentTypeEnum.RESOURCE, containerComponentResourceType);
67         if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
68             return false;
69         }
70         return allowedResourceInstanceTypeList.contains(resourceToCheck.getValue());
71     }
72
73     /**
74      * Gets the list of allowed component instances for a component type.
75      *
76      * @param componentType        the component type
77      * @param resourceInstanceType the instance type to check, or null for any instance
78      * @return the list of allowed component instances for the given component
79      */
80     public List<String> getComponentAllowedList(final ComponentTypeEnum componentType, final ResourceTypeEnum resourceInstanceType) {
81         final Map<String, List<String>> componentAllowedResourceTypeMap = getComponentAllowedInstanceTypes().get(componentType.getValue());
82         if (MapUtils.isEmpty(componentAllowedResourceTypeMap)) {
83             final String resourceTypeString = resourceInstanceType == null ? WILDCARD : resourceInstanceType.getValue();
84             LOGGER.warn("No '{}' instance resource type configuration found for '{}'", componentType.getValue(), resourceTypeString);
85             return Collections.emptyList();
86         }
87         return getAllowedInstanceType(resourceInstanceType, componentAllowedResourceTypeMap);
88     }
89
90     private Map<String, Map<String, List<String>>> getComponentAllowedInstanceTypes() {
91         return configurationManager.getConfiguration().getComponentAllowedInstanceTypes();
92     }
93
94     private List<String> getAllowedInstanceType(final ResourceTypeEnum resourceInstanceType,
95                                                 final Map<String, List<String>> instanceAllowedResourceTypeMap) {
96         if (MapUtils.isEmpty(instanceAllowedResourceTypeMap)) {
97             return Collections.emptyList();
98         }
99         List<String> allowedInstanceResourceType = null;
100         if (resourceInstanceType == null) {
101             if (instanceAllowedResourceTypeMap.containsKey(WILDCARD)) {
102                 allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(WILDCARD);
103             }
104         } else {
105             allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(resourceInstanceType.getValue());
106         }
107         if (CollectionUtils.isEmpty(allowedInstanceResourceType)) {
108             return Collections.emptyList();
109         }
110         return allowedInstanceResourceType;
111     }
112 }