aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/config/ContainerInstanceTypesData.java
blob: 903855b74bc4525791391d6f95f65c12095ae11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.model.jsonjanusgraph.config;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class ContainerInstanceTypesData {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContainerInstanceTypesData.class);
    private static final String WILDCARD = "*";

    private final ConfigurationManager configurationManager;

    public ContainerInstanceTypesData() {
        this.configurationManager = ConfigurationManager.getConfigurationManager();
    }

    /**
     * Checks if a resource instance type is allowed in a Service component.
     *
     * @param resourceTypeToCheck the resource instance type that will be added to the container component
     * @return {@code true} if the resource instance is allowed, {@code false} otherwise
     */
    public boolean isAllowedForServiceComponent(final ResourceTypeEnum resourceTypeToCheck) {
        final List<String> allowedResourceInstanceTypeList =
            getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
        if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
            return false;
        }
        return allowedResourceInstanceTypeList.contains(resourceTypeToCheck.getValue());
    }

    /**
     * Checks if a resource instance type is allowed for a resource component.
     *
     * @param containerComponentResourceType the container component type that the instance will be added
     * @param resourceToCheck the resource instance type that will be added to the container component
     * @return {@code true} if the resource instance is allowed in the container component, {@code false} otherwise
     */
    public boolean isAllowedForResourceComponent(final ResourceTypeEnum containerComponentResourceType,
                                                 final ResourceTypeEnum resourceToCheck) {
        final List<String> allowedResourceInstanceTypeList =
            getComponentAllowedList(ComponentTypeEnum.RESOURCE, containerComponentResourceType);
        if (CollectionUtils.isEmpty(allowedResourceInstanceTypeList)) {
            return false;
        }
        return allowedResourceInstanceTypeList.contains(resourceToCheck.getValue());
    }

    /**
     * Gets the list of allowed component instances for a component type.
     *
     * @param componentType the component type
     * @param resourceInstanceType the instance type to check, or null for any instance
     * @return the list of allowed component instances for the given component
     */
    public List<String> getComponentAllowedList(final ComponentTypeEnum componentType,
                                                final ResourceTypeEnum resourceInstanceType) {
        final Map<String, List<String>> componentAllowedResourceTypeMap =
            getComponentAllowedInstanceTypes().get(componentType.getValue());
        if (MapUtils.isEmpty(componentAllowedResourceTypeMap)) {
            final String resourceTypeString = resourceInstanceType == null ? WILDCARD : resourceInstanceType.getValue();
            LOGGER.warn("No '{}' instance resource type configuration found for '{}'",
                componentType.getValue(), resourceTypeString);
            return Collections.emptyList();
        }
        return getAllowedInstanceType(resourceInstanceType, componentAllowedResourceTypeMap);
    }

    private Map<String, Map<String, List<String>>> getComponentAllowedInstanceTypes() {
        return configurationManager.getConfiguration().getComponentAllowedInstanceTypes();
    }

    private List<String> getAllowedInstanceType(final ResourceTypeEnum resourceInstanceType,
                                                final Map<String, List<String>> instanceAllowedResourceTypeMap) {
        if (MapUtils.isEmpty(instanceAllowedResourceTypeMap)) {
            return Collections.emptyList();
        }
        List<String> allowedInstanceResourceType = null;
        if (resourceInstanceType == null) {
            if (instanceAllowedResourceTypeMap.containsKey(WILDCARD)) {
                allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(WILDCARD);
            }
        } else {
            allowedInstanceResourceType = instanceAllowedResourceTypeMap.get(resourceInstanceType.getValue());
        }

        if (CollectionUtils.isEmpty(allowedInstanceResourceType)) {
            return Collections.emptyList();
        }

        return allowedInstanceResourceType;
    }
}