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.generator.service;
\r
20 import java.util.Map;
\r
21 import org.apache.commons.lang3.StringUtils;
\r
22 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;
\r
23 import org.onap.ccsdk.features.generator.ConfigGeneratorConstant;
\r
24 import org.onap.ccsdk.features.generator.data.ConfigGeneratorInfo;
\r
25 import org.onap.ccsdk.features.model.ConfigModelConstant;
\r
26 import org.onap.ccsdk.features.model.service.ComponentNode;
\r
27 import org.onap.ccsdk.features.model.service.ConfigModelService;
\r
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
\r
29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
\r
30 import com.att.eelf.configuration.EELFLogger;
\r
31 import com.att.eelf.configuration.EELFManager;
\r
33 public class ConfigGeneratorNode implements ComponentNode {
\r
35 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNode.class);
\r
37 private ConfigModelService configModelService;
\r
38 private ConfigResourceService configResourceService;
\r
39 private ConfigGeneratorService configGeneratorService;
\r
41 public ConfigGeneratorNode(ConfigResourceService configResourceService, ConfigModelService configModelService) {
\r
42 this.configResourceService = configResourceService;
\r
43 this.configModelService = configModelService;
\r
44 this.configGeneratorService = new ConfigGeneratorServiceImpl(this.configResourceService);
\r
48 public Boolean preCondition(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)
\r
49 throws SvcLogicException {
\r
50 logger.trace("Received generateConfiguration preCondition call with params : ({})", inParams);
\r
51 return Boolean.TRUE;
\r
55 public void preProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)
\r
56 throws SvcLogicException {
\r
57 logger.trace("Received generateConfiguration preProcess call with params : ({})", inParams);
\r
61 public void process(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
\r
62 logger.trace("Received generateConfiguration process call with params : ({})", inParams);
\r
65 @SuppressWarnings("squid:S3776")
\r
67 public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)
\r
68 throws SvcLogicException {
\r
69 logger.trace("Received generateConfiguration process with params : ({})", inParams);
\r
70 String prifix = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR);
\r
72 prifix = StringUtils.isNotBlank(prifix) ? (prifix + ".") : "";
\r
74 String templateContent = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT);
\r
75 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
\r
76 String requestId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_REQUEST_ID);
\r
77 String resourceId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_ID);
\r
78 String resourceType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_TYPE);
\r
79 String recipeName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ACTION_NAME);
\r
80 String templateName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_NAME);
\r
82 ConfigGeneratorInfo configGeneratorInfo = null;
\r
83 if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) {
\r
84 configGeneratorInfo = this.configGeneratorService.generateConfiguration(templateContent, templateData);
\r
86 if (StringUtils.isBlank(requestId)) {
\r
87 throw new SvcLogicException("Config Generator Request Id is missing.");
\r
89 if (StringUtils.isBlank(resourceId)) {
\r
90 throw new SvcLogicException("Config Generator Resource Id is missing.");
\r
92 if (StringUtils.isBlank(resourceType)) {
\r
93 throw new SvcLogicException("Config Generator Resource Type is missing.");
\r
95 if (StringUtils.isBlank(recipeName)) {
\r
96 throw new SvcLogicException("Config Generator Action Name is missing.");
\r
98 if (StringUtils.isBlank(templateName)) {
\r
99 throw new SvcLogicException("Config Generator Template Name Id is missing.");
\r
102 templateContent = configModelService.getNodeTemplateContent(ctx, templateName);
\r
104 if (StringUtils.isBlank(templateContent)) {
\r
105 throw new SvcLogicException(
\r
106 "Failed to get the Template Content for the Temaple Name :" + templateName);
\r
109 configGeneratorInfo = new ConfigGeneratorInfo();
\r
110 configGeneratorInfo.setRequestId(requestId);
\r
111 configGeneratorInfo.setResourceId(resourceId);
\r
112 configGeneratorInfo.setResourceType(resourceType);
\r
113 configGeneratorInfo.setRecipeName(recipeName);
\r
114 configGeneratorInfo.setTemplateName(templateName);
\r
115 configGeneratorInfo.setTemplateContent(templateContent);
\r
117 this.configGeneratorService.generateConfiguration(configGeneratorInfo);
\r
119 if (configGeneratorInfo != null) {
\r
120 ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG,
\r
121 configGeneratorInfo.getMashedData());
\r
123 ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
\r
124 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
\r
125 } catch (Exception e) {
\r
126 ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
\r
127 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
\r
128 ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
\r
129 logger.error("Failed in generateConfiguration ({})", e);
\r
130 throw new SvcLogicException(e.getMessage());
\r
135 public void postProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)
\r
136 throws SvcLogicException {
\r
137 logger.info("Received generateConfiguration postProcess with params : ({})", inParams);
\r