Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / service / ConfigBlueprintService.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.config.model.service;\r
19 \r
20 import java.util.List;\r
21 import java.util.Map;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.onap.ccsdk.config.model.ConfigModelConstant;\r
24 import org.onap.ccsdk.config.model.domain.ConfigModel;\r
25 import org.onap.ccsdk.config.model.domain.ConfigModelContent;\r
26 import org.onap.ccsdk.config.model.utils.PrepareContextUtils;\r
27 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;\r
28 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
29 import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService;\r
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
31 import com.att.eelf.configuration.EELFLogger;\r
32 import com.att.eelf.configuration.EELFManager;\r
33 \r
34 public class ConfigBlueprintService {\r
35 \r
36     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigBlueprintService.class);\r
37 \r
38     private final ConfigRestAdaptorService configRestAdaptorService;\r
39 \r
40     public ConfigBlueprintService(ConfigRestAdaptorService configRestAdaptorService) {\r
41         this.configRestAdaptorService = configRestAdaptorService;\r
42     }\r
43 \r
44     public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateName,\r
45             String serviceTemplateVersion) throws SvcLogicException {\r
46         try {\r
47             PrepareContextUtils prepareContextUtils = new PrepareContextUtils();\r
48             String serviceTemplateContent = getServiceModel(context, serviceTemplateName, serviceTemplateVersion);\r
49 \r
50             if (StringUtils.isBlank(serviceTemplateContent)) {\r
51                 throw new SvcLogicException(String.format("Failed to get the Service Template (%s), version (%s)",\r
52                         serviceTemplateName, serviceTemplateVersion));\r
53             }\r
54 \r
55             return prepareContextUtils.prepareContext(context, input, serviceTemplateContent);\r
56         } catch (Exception e) {\r
57             throw new SvcLogicException(e.getMessage(), e);\r
58         }\r
59     }\r
60 \r
61     @SuppressWarnings("squid:S3776")\r
62     private String getServiceModel(Map<String, String> context, String serviceTemplateName,\r
63             String serviceTemplateVersion) throws SvcLogicException, ConfigRestAdaptorException {\r
64         String content = null;\r
65 \r
66         logger.info("Getting service template ({}) of version ({}) ", serviceTemplateName, serviceTemplateVersion);\r
67 \r
68         String path = "configmodelbyname/" + serviceTemplateName + "/version/" + serviceTemplateVersion;\r
69 \r
70         ConfigModel configModel = configRestAdaptorService\r
71                 .getResource(ConfigRestAdaptorConstants.SELECTOR_MODEL_SERVICE, path, ConfigModel.class);\r
72 \r
73         if (configModel == null || configModel.getConfigModelContents() == null\r
74                 || configModel.getConfigModelContents().isEmpty()) {\r
75             throw new SvcLogicException("Service template model is missing for  service template name ("\r
76                     + serviceTemplateName + "), service template version (" + serviceTemplateVersion + ") ");\r
77         } else {\r
78             if (configModel.getPublished() == null || !configModel.getPublished().equalsIgnoreCase("Y")) {\r
79                 throw new SvcLogicException(String.format(\r
80                         "Service template model is not published for service template (%s) ", serviceTemplateName));\r
81             }\r
82 \r
83             List<ConfigModelContent> configModelContents = configModel.getConfigModelContents();\r
84             for (ConfigModelContent configModelContent : configModelContents) {\r
85                 if (configModelContent != null) {\r
86                     if (ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON.equals(configModelContent.getContentType())) {\r
87                         content = configModelContent.getContent();\r
88                     } else if (ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE\r
89                             .equals(configModelContent.getContentType())) {\r
90                         context.put(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + configModelContent.getName()\r
91                                 + ".content", configModelContent.getContent());\r
92                     }\r
93                 }\r
94             }\r
95             logger.trace("Service model data : {} ", content);\r
96         }\r
97         return content;\r
98     }\r
99 \r
100 }\r