ab8230d946e562918887b1da183fb8fb58f30f1f
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / ResourceTranslationFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca;
22
23
24 import org.openecomp.config.api.Configuration;
25 import org.openecomp.config.api.ConfigurationManager;
26 import org.openecomp.core.utilities.CommonMethods;
27 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
30
31 import java.util.Map;
32
33
34 public class ResourceTranslationFactory {
35   private static Map<String, ImplementationConfiguration> resourceTranslationImplMap;
36
37   static {
38     Configuration config = ConfigurationManager.lookup();
39     resourceTranslationImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
40         ConfigConstants.RESOURCE_TRANSLATION_IMPL_KEY, ImplementationConfiguration.class);
41     resourceTranslationImplMap.putAll(config.populateMap(ConfigConstants.MANDATORY_TRANSLATOR_NAMESPACE,
42         ConfigConstants.RESOURCE_TRANSLATION_IMPL_KEY, ImplementationConfiguration.class));
43   }
44
45   /**
46    * Gets resource translation instance.
47    *
48    * @param resource the resource
49    * @return the instance
50    */
51   public static ResourceTranslationBase getInstance(Resource resource) {
52     if (isSupportedResource(resource.getType())) {
53       return getResourceTranslationImpl(resource.getType());
54     } else if (HeatToToscaUtil.isYmlFileType(resource.getType())) {
55       return getResourceTranslationImpl(ConfigConstants.NESTED_RESOURCE_TRANSLATION_IMPL_KEY);
56     }
57     return getResourceTranslationImpl(ConfigConstants.DEFAULT_RESOURCE_TRANSLATION_IMPL_KEY);
58   }
59
60   private static ResourceTranslationBase getResourceTranslationImpl(String resourceImplKey) {
61     String resourceTranslationImplClassName = resourceTranslationImplMap
62         .get(resourceImplKey).getImplementationClass();
63     return CommonMethods
64         .newInstance(resourceTranslationImplClassName, ResourceTranslationBase.class);
65   }
66
67   private static boolean isSupportedResource(String resourceType) {
68     if (resourceTranslationImplMap.containsKey(resourceType)
69         && resourceTranslationImplMap.get(resourceType).isEnable()) {
70       return true;
71     }
72     return false;
73   }
74
75 }
76