Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / service / ConfigModelServiceImpl.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.ConfigModelException;\r
25 import org.onap.ccsdk.config.model.data.CapabilityAssignment;\r
26 import org.onap.ccsdk.config.model.data.NodeTemplate;\r
27 import org.onap.ccsdk.config.model.data.NodeType;\r
28 import org.onap.ccsdk.config.model.data.ServiceTemplate;\r
29 import org.onap.ccsdk.config.model.utils.NodePropertyUtils;\r
30 import org.onap.ccsdk.config.model.utils.PrepareContextUtils;\r
31 import org.onap.ccsdk.config.model.utils.ServiceTemplateUtils;\r
32 import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
33 import org.onap.ccsdk.config.model.validator.ServiceTemplateValidator;\r
34 import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService;\r
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
37 import com.att.eelf.configuration.EELFLogger;\r
38 import com.att.eelf.configuration.EELFManager;\r
39 \r
40 public class ConfigModelServiceImpl implements ConfigModelService {\r
41 \r
42     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigModelServiceImpl.class);\r
43     private static final String CLASS_NAME = "ConfigModelServiceImpl";\r
44 \r
45     private final ConfigRestAdaptorService configRestAdaptorService;\r
46 \r
47     public ConfigModelServiceImpl(ConfigRestAdaptorService configRestAdaptorService) {\r
48         logger.info("{} Constuctor Initated...", CLASS_NAME);\r
49         this.configRestAdaptorService = configRestAdaptorService;\r
50     }\r
51 \r
52     @Override\r
53     public Boolean validateServiceTemplate(ServiceTemplate serviceTemplate) throws SvcLogicException {\r
54         try {\r
55             ServiceTemplateValidator serviceTemplateValidator = new ServiceTemplateValidator();\r
56             return serviceTemplateValidator.validateServiceTemplate(serviceTemplate);\r
57         } catch (ConfigModelException e) {\r
58             throw new SvcLogicException(e.getMessage());\r
59         }\r
60     }\r
61 \r
62     @Override\r
63     public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateName,\r
64             String serviceTemplateVersion) throws SvcLogicException {\r
65 \r
66         ConfigBlueprintService configBlueprintService = new ConfigBlueprintService(configRestAdaptorService);\r
67         return configBlueprintService.prepareContext(context, input, serviceTemplateName, serviceTemplateVersion);\r
68     }\r
69 \r
70     @Override\r
71     public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateContent)\r
72             throws SvcLogicException {\r
73         try {\r
74             PrepareContextUtils prepareContextUtils = new PrepareContextUtils();\r
75             return prepareContextUtils.prepareContext(context, input, serviceTemplateContent);\r
76         } catch (Exception e) {\r
77             throw new SvcLogicException(e.getMessage());\r
78         }\r
79     }\r
80 \r
81     @Override\r
82     public Map<String, String> convertJson2properties(Map<String, String> context, String jsonContent,\r
83             List<String> blockKeys) throws SvcLogicException {\r
84         try {\r
85             return TransformationUtils.convertJson2Properties(context, jsonContent, blockKeys);\r
86         } catch (Exception e) {\r
87             throw new SvcLogicException(e.getMessage());\r
88         }\r
89     }\r
90 \r
91     @Override\r
92     public Map<String, String> convertServiceTemplate2Properties(String serviceTemplateContent,\r
93             final Map<String, String> context) throws SvcLogicException {\r
94         try {\r
95             ServiceTemplateUtils serviceTemplateUtils = new ServiceTemplateUtils();\r
96             return serviceTemplateUtils.convertServiceTemplate2Properties(serviceTemplateContent, context);\r
97         } catch (Exception e) {\r
98             throw new SvcLogicException(e.getMessage());\r
99         }\r
100     }\r
101 \r
102     @Override\r
103     public Map<String, String> convertServiceTemplate2Properties(ServiceTemplate serviceTemplate,\r
104             final Map<String, String> context) throws SvcLogicException {\r
105         try {\r
106             ServiceTemplateUtils serviceTemplateUtils = new ServiceTemplateUtils();\r
107             return serviceTemplateUtils.convertServiceTemplate2Properties(serviceTemplate, context);\r
108         } catch (Exception e) {\r
109             throw new SvcLogicException(e.getMessage());\r
110         }\r
111     }\r
112 \r
113     @SuppressWarnings("squid:S3776")\r
114     @Override\r
115     public SvcLogicContext assignInParamsFromModel(final SvcLogicContext context, final Map<String, String> inParams)\r
116             throws SvcLogicException {\r
117         logger.debug("Processing component input param ({})", inParams);\r
118         try {\r
119             if (context != null && inParams != null && inParams.containsKey(ConfigModelConstant.PROPERTY_SELECTOR)) {\r
120                 String componentKey = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR);\r
121                 if (StringUtils.isNotBlank(componentKey)) {\r
122                     String nodeTemplateContent =\r
123                             context.getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + componentKey);\r
124                     logger.info("Processing node template content ({})", nodeTemplateContent);\r
125                     if (StringUtils.isNotBlank(nodeTemplateContent)) {\r
126                         NodeTemplate nodeTemplate =\r
127                                 TransformationUtils.readValue(nodeTemplateContent, NodeTemplate.class);\r
128                         if (nodeTemplate != null && StringUtils.isNotBlank(nodeTemplate.getType())) {\r
129                             String nodeTypeContent = context\r
130                                     .getAttribute(ConfigModelConstant.PROPERTY_NODE_TYPES_DOT + nodeTemplate.getType());\r
131                             NodeType nodetype = TransformationUtils.readValue(nodeTypeContent, NodeType.class);\r
132                             if (nodetype != null) {\r
133                                 inParams.put(ConfigModelConstant.PROPERTY_CURRENT_NODETYPE_DERIVED_FROM,\r
134                                         nodetype.getDerivedFrom());\r
135                                 NodePropertyUtils nodePropertyUtils = new NodePropertyUtils(context, inParams);\r
136                                 nodePropertyUtils.assignInParamsFromModel(nodetype, nodeTemplate);\r
137                             } else {\r
138                                 throw new SvcLogicException(\r
139                                         String.format("Failed to get node type (%s) for node template (%s).",\r
140                                                 nodeTemplate.getType(), componentKey));\r
141                             }\r
142 \r
143                         } else {\r
144                             throw new SvcLogicException(String\r
145                                     .format("Failed to convert content (%s) to node template.", nodeTemplateContent));\r
146                         }\r
147                     } else {\r
148                         throw new SvcLogicException(String\r
149                                 .format("Couldn't get node template content for component key (%s).", componentKey));\r
150                     }\r
151                 } else {\r
152                     throw new SvcLogicException(\r
153                             String.format("Couldn't get component key (prefix) from inparam (%s)", inParams));\r
154                 }\r
155             }\r
156         } catch (Exception e) {\r
157             throw new SvcLogicException(e.getMessage());\r
158         }\r
159         return context;\r
160     }\r
161 \r
162     @Override\r
163     public SvcLogicContext assignOutParamsFromModel(final SvcLogicContext context, final Map<String, String> inParams)\r
164             throws SvcLogicException {\r
165         try {\r
166             if (context != null && inParams != null && inParams.containsKey(ConfigModelConstant.PROPERTY_SELECTOR)) {\r
167                 String componentKey = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR);\r
168                 logger.info("Processing component output for prefix key ({})", componentKey);\r
169                 if (StringUtils.isNotBlank(componentKey)) {\r
170                     String nodeTemplateContent =\r
171                             context.getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + componentKey);\r
172                     if (StringUtils.isNotBlank(nodeTemplateContent)) {\r
173                         NodeTemplate nodeTemplate =\r
174                                 TransformationUtils.readValue(nodeTemplateContent, NodeTemplate.class);\r
175                         if (nodeTemplate != null && StringUtils.isNotBlank(nodeTemplate.getType())) {\r
176                             String nodeTypeContent = context\r
177                                     .getAttribute(ConfigModelConstant.PROPERTY_NODE_TYPES_DOT + nodeTemplate.getType());\r
178                             NodeType nodetype = TransformationUtils.readValue(nodeTypeContent, NodeType.class);\r
179                             NodePropertyUtils nodePropertyUtils = new NodePropertyUtils(context, inParams);\r
180                             nodePropertyUtils.assignOutParamsFromModel(nodetype, nodeTemplate);\r
181                         }\r
182                     }\r
183                 }\r
184             }\r
185         } catch (Exception e) {\r
186             throw new SvcLogicException(e.getMessage());\r
187         }\r
188         return context;\r
189     }\r
190 \r
191     @Override\r
192     public String getNodeTemplateContent(final SvcLogicContext context, String templateName) throws SvcLogicException {\r
193         String content = null;\r
194         try {\r
195             if (context != null && StringUtils.isNotBlank(templateName)) {\r
196                 logger.info("Processing Artifact Node Template  for content : ({})", templateName);\r
197                 content = context\r
198                         .getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + templateName + ".content");\r
199             }\r
200         } catch (Exception e) {\r
201             throw new SvcLogicException(e.getMessage());\r
202         }\r
203         return content;\r
204     }\r
205 \r
206     @SuppressWarnings("squid:S3776")\r
207     @Override\r
208     public String getNodeTemplateMapping(final SvcLogicContext context, String templateName) throws SvcLogicException {\r
209         String mapping = null;\r
210         try {\r
211             if (context != null && StringUtils.isNotBlank(templateName)) {\r
212                 logger.info("Processing artifact node template for mapping : ({})", templateName);\r
213                 if (StringUtils.isNotBlank(templateName)) {\r
214                     String nodeTemplateContent =\r
215                             context.getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + templateName);\r
216                     if (StringUtils.isNotBlank(nodeTemplateContent)) {\r
217 \r
218                         NodeTemplate nodeTemplate =\r
219                                 TransformationUtils.readValue(nodeTemplateContent, NodeTemplate.class);\r
220 \r
221                         if (nodeTemplate != null && nodeTemplate.getCapabilities() != null && nodeTemplate\r
222                                 .getCapabilities().containsKey(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING)) {\r
223 \r
224                             CapabilityAssignment capability =\r
225                                     nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);\r
226                             if (capability.getProperties() != null) {\r
227                                 List mappingList = (List) capability.getProperties()\r
228                                         .get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);\r
229                                 if (mappingList != null) {\r
230                                     mapping = TransformationUtils.getJson(mappingList);\r
231                                 }\r
232 \r
233                             }\r
234                         }\r
235                     }\r
236                 }\r
237             }\r
238         } catch (Exception e) {\r
239             throw new SvcLogicException(e.getMessage());\r
240         }\r
241         return mapping;\r
242     }\r
243 \r
244 }\r