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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.model.jsonjanusgraph.config;
22 import java.util.Collections;
23 import java.util.List;
25 import org.apache.commons.collections4.CollectionUtils;
26 import org.apache.commons.collections4.MapUtils;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
35 public class ContainerInstanceTypesData {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ContainerInstanceTypesData.class);
38 private static final String WILDCARD = "*";
40 private final ConfigurationManager configurationManager;
42 public ContainerInstanceTypesData() {
43 this.configurationManager = ConfigurationManager.getConfigurationManager();
47 * Checks if a resource instance type is allowed in a Service component.
49 * @param resourceTypeToCheck the resource instance type that will be added to the container component
50 * @return {@code true} if the resource instance is allowed, {@code false} otherwise
52 public boolean isAllowedForServiceComponent(final ResourceTypeEnum resourceTypeToCheck) {
53 final List<String> allowedResourceInstanceTypeList =
54 getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
55 if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
58 return allowedResourceInstanceTypeList.contains(resourceTypeToCheck.getValue());
62 * Checks if a resource instance type is allowed for a resource component.
64 * @param containerComponentResourceType the container component type that the instance will be added
65 * @param resourceToCheck the resource instance type that will be added to the container component
66 * @return {@code true} if the resource instance is allowed in the container component, {@code false} otherwise
68 public boolean isAllowedForResourceComponent(final ResourceTypeEnum containerComponentResourceType,
69 final ResourceTypeEnum resourceToCheck) {
70 final List<String> allowedResourceInstanceTypeList =
71 getComponentAllowedList(ComponentTypeEnum.RESOURCE, containerComponentResourceType);
72 if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
75 return allowedResourceInstanceTypeList.contains(resourceToCheck.getValue());
79 * Gets the list of allowed component instances for a component type.
81 * @param componentType the component type
82 * @param resourceInstanceType the instance type to check, or null for any instance
83 * @return the list of allowed component instances for the given component
85 public List<String> getComponentAllowedList(final ComponentTypeEnum componentType,
86 final ResourceTypeEnum resourceInstanceType) {
87 final Map<String, List<String>> componentAllowedResourceTypeMap =
88 getComponentAllowedInstanceTypes().get(componentType.getValue());
89 if (MapUtils.isEmpty(componentAllowedResourceTypeMap)) {
90 final String resourceTypeString = resourceInstanceType == null ? WILDCARD : resourceInstanceType.getValue();
91 LOGGER.warn("No '{}' instance resource type configuration found for '{}'",
92 componentType.getValue(), resourceTypeString);
93 return Collections.emptyList();
95 return getAllowedInstanceType(resourceInstanceType, componentAllowedResourceTypeMap);
98 private Map<String, Map<String, List<String>>> getComponentAllowedInstanceTypes() {
99 return configurationManager.getConfiguration().getComponentAllowedInstanceTypes();
102 private List<String> getAllowedInstanceType(final ResourceTypeEnum resourceInstanceType,
103 final Map<String, List<String>> instanceAllowedResourceTypeMap) {
104 if (MapUtils.isEmpty(instanceAllowedResourceTypeMap)) {
105 return Collections.emptyList();
107 List<String> allowedInstanceResourceType = null;
108 if (resourceInstanceType == null) {
109 if (instanceAllowedResourceTypeMap.containsKey(WILDCARD)) {
110 allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(WILDCARD);
113 allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(resourceInstanceType.getValue());
116 if (CollectionUtils.isEmpty(allowedInstanceResourceType)) {
117 return Collections.emptyList();
120 return allowedInstanceResourceType;