2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.features.model.service;
\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.features.model.ConfigModelConstant;
\r
24 import org.onap.ccsdk.features.model.ConfigModelException;
\r
25 import org.onap.ccsdk.features.model.data.CapabilityAssignment;
\r
26 import org.onap.ccsdk.features.model.data.NodeTemplate;
\r
27 import org.onap.ccsdk.features.model.data.NodeType;
\r
28 import org.onap.ccsdk.features.model.data.ServiceTemplate;
\r
29 import org.onap.ccsdk.features.model.utils.NodePropertyUtils;
\r
30 import org.onap.ccsdk.features.model.utils.PrepareContextUtils;
\r
31 import org.onap.ccsdk.features.model.utils.ServiceTemplateUtils;
\r
32 import org.onap.ccsdk.features.model.utils.TransformationUtils;
\r
33 import org.onap.ccsdk.features.model.validator.ServiceTemplateValidator;
\r
34 import org.onap.ccsdk.features.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
40 public class ConfigModelServiceImpl implements ConfigModelService {
\r
42 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigModelServiceImpl.class);
\r
43 private static final String CLASS_NAME = "ConfigModelServiceImpl";
\r
45 private final ConfigRestAdaptorService configRestAdaptorService;
\r
47 public ConfigModelServiceImpl(ConfigRestAdaptorService configRestAdaptorService) {
\r
48 logger.info("{} Constuctor Initated...", CLASS_NAME);
\r
49 this.configRestAdaptorService = configRestAdaptorService;
\r
53 public Boolean validateServiceTemplate(ServiceTemplate serviceTemplate) throws SvcLogicException {
\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
63 public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateName,
\r
64 String serviceTemplateVersion) throws SvcLogicException {
\r
66 ConfigBlueprintService configBlueprintService = new ConfigBlueprintService(configRestAdaptorService);
\r
67 return configBlueprintService.prepareContext(context, input, serviceTemplateName, serviceTemplateVersion);
\r
71 public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateContent)
\r
72 throws SvcLogicException {
\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
82 public Map<String, String> convertJson2properties(Map<String, String> context, String jsonContent,
\r
83 List<String> blockKeys) throws SvcLogicException {
\r
85 return TransformationUtils.convertJson2Properties(context, jsonContent, blockKeys);
\r
86 } catch (Exception e) {
\r
87 throw new SvcLogicException(e.getMessage());
\r
92 public Map<String, String> convertServiceTemplate2Properties(String serviceTemplateContent,
\r
93 final Map<String, String> context) throws SvcLogicException {
\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
103 public Map<String, String> convertServiceTemplate2Properties(ServiceTemplate serviceTemplate,
\r
104 final Map<String, String> context) throws SvcLogicException {
\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
113 @SuppressWarnings("squid:S3776")
\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
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
138 throw new SvcLogicException(
\r
139 String.format("Failed to get node type (%s) for node template (%s).",
\r
140 nodeTemplate.getType(), componentKey));
\r
144 throw new SvcLogicException(String
\r
145 .format("Failed to convert content (%s) to node template.", nodeTemplateContent));
\r
148 throw new SvcLogicException(String
\r
149 .format("Couldn't get node template content for component key (%s).", componentKey));
\r
152 throw new SvcLogicException(
\r
153 String.format("Couldn't get component key (prefix) from inparam (%s)", inParams));
\r
156 } catch (Exception e) {
\r
157 throw new SvcLogicException(e.getMessage());
\r
163 public SvcLogicContext assignOutParamsFromModel(final SvcLogicContext context, final Map<String, String> inParams)
\r
164 throws SvcLogicException {
\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
185 } catch (Exception e) {
\r
186 throw new SvcLogicException(e.getMessage());
\r
192 public String getNodeTemplateContent(final SvcLogicContext context, String templateName) throws SvcLogicException {
\r
193 String content = null;
\r
195 if (context != null && StringUtils.isNotBlank(templateName)) {
\r
196 logger.info("Processing Artifact Node Template for content : ({})", templateName);
\r
198 .getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + templateName + ".content");
\r
200 } catch (Exception e) {
\r
201 throw new SvcLogicException(e.getMessage());
\r
206 @SuppressWarnings("squid:S3776")
\r
208 public String getNodeTemplateMapping(final SvcLogicContext context, String templateName) throws SvcLogicException {
\r
209 String mapping = null;
\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
218 NodeTemplate nodeTemplate =
\r
219 TransformationUtils.readValue(nodeTemplateContent, NodeTemplate.class);
\r
221 if (nodeTemplate != null && nodeTemplate.getCapabilities() != null && nodeTemplate
\r
222 .getCapabilities().containsKey(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING)) {
\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
238 } catch (Exception e) {
\r
239 throw new SvcLogicException(e.getMessage());
\r