Reformat common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / be / config / ArtifactConfigManager.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.config;
20
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
25
26 /**
27  * Singleton that manages the artifact type configuration
28  */
29 public class ArtifactConfigManager {
30
31     public static final ArtifactConfigManager INSTANCE = new ArtifactConfigManager();
32
33     private ArtifactConfigManager() {
34     }
35
36     public static ArtifactConfigManager getInstance() {
37         return INSTANCE;
38     }
39
40     /**
41      * Find an artifact configuration by artifact type.
42      *
43      * @param type the artifact type
44      * @return the artifact configuration if the type exists
45      */
46     public Optional<ArtifactConfiguration> find(final String type) {
47         final List<ArtifactConfiguration> artifactConfigurationList = getConfiguration();
48         return artifactConfigurationList.stream().filter(artifactConfiguration -> artifactConfiguration.getType().equals(type)).findFirst();
49     }
50
51     /**
52      * Find an artifact configuration by artifact type, that supports the artifact category/group and component type.
53      *
54      * @param type          the artifact type
55      * @param artifactGroup the artifact category/group
56      * @param componentType the component type
57      * @return the artifact configuration if it matches the provided filter
58      */
59     public Optional<ArtifactConfiguration> find(final String type, final ArtifactGroupTypeEnum artifactGroup, final ComponentType componentType) {
60         final ArtifactConfiguration artifactConfiguration = find(type).orElse(null);
61         if (artifactConfiguration == null) {
62             return Optional.empty();
63         }
64         final boolean hasCategory = artifactConfiguration.hasSupport(artifactGroup);
65         if (!hasCategory) {
66             return Optional.empty();
67         }
68         final boolean hasComponentType = artifactConfiguration.hasSupport(componentType);
69         if (!hasComponentType) {
70             return Optional.empty();
71         }
72         return Optional.of(artifactConfiguration);
73     }
74
75     /**
76      * Find all artifact configuration that supports an artifact category/group and a component type.
77      *
78      * @param artifactGroup the artifact group/category
79      * @param componentType the component type
80      * @return the artifact configurations that matches the filter
81      */
82     public List<ArtifactConfiguration> findAll(final ArtifactGroupTypeEnum artifactGroup, final ComponentType componentType) {
83         final List<ArtifactConfiguration> artifactConfigurationList = ConfigurationManager.getConfigurationManager().getConfiguration()
84             .getArtifacts();
85         return artifactConfigurationList.stream()
86             .filter(artifactConfiguration1 -> artifactConfiguration1.hasSupport(artifactGroup) && artifactConfiguration1.hasSupport(componentType))
87             .collect(Collectors.toList());
88     }
89
90     /**
91      * Find all artifact configuration that supports an artifact category/group.
92      *
93      * @param artifactGroup the artifact category/group
94      * @return the artifact configurations that matches the filter
95      */
96     public List<ArtifactConfiguration> findAll(final ArtifactGroupTypeEnum artifactGroup) {
97         final List<ArtifactConfiguration> artifactConfigurationList = ConfigurationManager.getConfigurationManager().getConfiguration()
98             .getArtifacts();
99         return artifactConfigurationList.stream().filter(artifactConfiguration1 -> artifactConfiguration1.hasSupport(artifactGroup))
100             .collect(Collectors.toList());
101     }
102
103     /**
104      * Find all artifact configuration that supports a component type.
105      *
106      * @param componentType the component type
107      * @return the artifact configurations that matches the filter
108      */
109     public List<ArtifactConfiguration> findAll(final ComponentType componentType) {
110         final List<ArtifactConfiguration> artifactConfigurationList = ConfigurationManager.getConfigurationManager().getConfiguration()
111             .getArtifacts();
112         return artifactConfigurationList.stream().filter(artifactConfiguration1 -> artifactConfiguration1.hasSupport(componentType))
113             .collect(Collectors.toList());
114     }
115
116     /**
117      * Gets the artifact configuration list.
118      *
119      * @return the artifact configuration list.
120      */
121     public List<ArtifactConfiguration> getConfiguration() {
122         return ConfigurationManager.getConfigurationManager().getConfiguration().getArtifacts();
123     }
124 }