re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / FunctionTranslationFactory.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.openecomp.sdc.translator.services.heattotosca;
18
19 import org.onap.config.api.Configuration;
20 import org.onap.config.api.ConfigurationManager;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
23
24 import java.util.Map;
25 import java.util.Optional;
26
27 public class FunctionTranslationFactory {
28     private static final Map<String, ImplementationConfiguration> functionTranslationImplMap;
29
30     static {
31         Configuration config = ConfigurationManager.lookup();
32         functionTranslationImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
33                 ConfigConstants.FUNCTION_TRANSLATION_IMPL_KEY, ImplementationConfiguration.class);
34         functionTranslationImplMap.putAll(config.populateMap(ConfigConstants.MANDATORY_TRANSLATOR_NAMESPACE,
35                 ConfigConstants.FUNCTION_TRANSLATION_IMPL_KEY, ImplementationConfiguration.class));
36
37     }
38
39     /**
40      * Gets function translation instance.
41      *
42      * @param heatFunctionKey heat function key
43      * @return the instance
44      */
45     public static Optional<FunctionTranslation> getInstance(String heatFunctionKey) {
46         if (isSupportedFunction(heatFunctionKey)) {
47             String functionTranslationImplClassName =
48                     functionTranslationImplMap.get(heatFunctionKey).getImplementationClass();
49             return Optional.of(CommonMethods
50                     .newInstance(functionTranslationImplClassName, FunctionTranslation.class));
51         }
52         return Optional.empty();
53     }
54
55     private static boolean isSupportedFunction(String heatFunctionKey) {
56         return functionTranslationImplMap.containsKey(heatFunctionKey);
57     }
58
59 }
60