75a0cd625ef3f86bcdf408c7c7f92d2806ec7be1
[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 java.util.stream.Collectors;
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.openecomp.sdc.common.api.Constants;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public class ContainerInstanceTypesData {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(ContainerInstanceTypesData.class);
39     private static final String WILDCARD = "*";
40     private final ConfigurationManager configurationManager;
41
42     public ContainerInstanceTypesData() {
43         this.configurationManager = ConfigurationManager.getConfigurationManager();
44     }
45
46     /**
47      * Checks if a resource instance type is allowed in a Service component.
48      *
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
51      */
52     public boolean isAllowedForServiceComponent(final ResourceTypeEnum resourceTypeToCheck, final String modelName) {
53         final List<String> allowedResourceInstanceTypeList = getServiceAllowedList(modelName);
54         if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
55             return false;
56         }
57         return allowedResourceInstanceTypeList.contains(resourceTypeToCheck.getValue());
58     }
59
60     /**
61      * Checks if a resource instance type is allowed for a resource component.
62      *
63      * @param containerComponentResourceType the container component type that the instance will be added
64      * @param resourceToCheck                the resource instance type that will be added to the container component
65      * @return {@code true} if the resource instance is allowed in the container component, {@code false} otherwise
66      */
67     public boolean isAllowedForResourceComponent(final ResourceTypeEnum containerComponentResourceType, final ResourceTypeEnum resourceToCheck) {
68         final List<String> allowedResourceInstanceTypeList = getComponentAllowedList(ComponentTypeEnum.RESOURCE, containerComponentResourceType);
69         if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
70             return false;
71         }
72         return allowedResourceInstanceTypeList.contains(resourceToCheck.getValue());
73     }
74     
75     /**
76      * Gets the list of allowed component instances for a service of the given model.
77      *
78      * @param modelName the model
79      * @return the list of allowed component instances
80      */
81     public List<String> getServiceAllowedList(final String modelName) {
82         List<String> allowedInstanceResourceType = getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
83         if (modelName == null || modelName.isEmpty() || modelName.equals(Constants.DEFAULT_MODEL_NAME)){
84             allowedInstanceResourceType.remove("VFC");
85         }
86         return allowedInstanceResourceType;
87     }
88
89     /**
90      * Gets the list of allowed component instances for a component type.
91      *
92      * @param componentType        the component type
93      * @param resourceInstanceType the instance type to check, or null for any instance
94      * @return the list of allowed component instances for the given component
95      */
96     public List<String> getComponentAllowedList(final ComponentTypeEnum componentType, final ResourceTypeEnum resourceInstanceType) {
97         final Map<String, List<String>> componentAllowedResourceTypeMap = getComponentAllowedInstanceTypes().get(componentType.getValue());
98         if (MapUtils.isEmpty(componentAllowedResourceTypeMap)) {
99             final String resourceTypeString = resourceInstanceType == null ? WILDCARD : resourceInstanceType.getValue();
100             LOGGER.warn("No '{}' instance resource type configuration found for '{}'", componentType.getValue(), resourceTypeString);
101             return Collections.emptyList();
102         }
103         return getAllowedInstanceType(resourceInstanceType, componentAllowedResourceTypeMap);
104     }
105
106     private Map<String, Map<String, List<String>>> getComponentAllowedInstanceTypes() {
107         return configurationManager.getConfiguration().getComponentAllowedInstanceTypes();
108     }
109
110     private List<String> getAllowedInstanceType(final ResourceTypeEnum resourceInstanceType,
111                                                 final Map<String, List<String>> instanceAllowedResourceTypeMap) {
112         if (MapUtils.isEmpty(instanceAllowedResourceTypeMap)) {
113             return Collections.emptyList();
114         }
115         List<String> allowedInstanceResourceType = null;
116         if (resourceInstanceType == null) {
117             if (instanceAllowedResourceTypeMap.containsKey(WILDCARD)) {
118                 allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(WILDCARD);
119             }
120         } else {
121             allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(resourceInstanceType.getValue());
122         }
123         if (CollectionUtils.isEmpty(allowedInstanceResourceType)) {
124             return Collections.emptyList();
125         }
126         return allowedInstanceResourceType.stream().collect(Collectors.toList());
127     }
128 }