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