2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.openecomp.sdnc.config.params.parser;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.apache.commons.lang3.StringUtils;
32 import org.openecomp.sdnc.config.params.ParamsHandlerConstant;
33 import org.openecomp.sdnc.config.params.data.Parameter;
34 import org.openecomp.sdnc.config.params.data.PropertyDefinition;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.openecomp.sdnc.sli.SvcLogicException;
37 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.databind.JsonMappingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
46 public class PropertyDefinitionNode implements SvcLogicJavaPlugin{
49 private static final EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
51 public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
52 log.info("Received processParamKeys call with params : " + inParams);
53 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
55 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
57 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
58 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
60 if(StringUtils.isBlank(pdContent)){
61 throw new Exception("Request Param (pdContent) is Missing ..");
64 if(StringUtils.isBlank(requestParamJson)){
65 throw new Exception("Request Param (jsonData) is Missing ..");
68 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
69 if(propertyDefinition != null){
70 requestParamJson = mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
71 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
74 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
75 } catch (Exception e) {
76 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
77 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
78 log.error("Failed in merging data to template " + e.getMessage());
79 throw new SvcLogicException(e.getMessage());
83 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
84 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
85 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
87 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
89 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
90 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
91 String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);
94 if(StringUtils.isBlank(pdContent)){
95 throw new Exception("Request Param (pdContent) is Missing ..");
98 if(StringUtils.isBlank(requestParamJson)){
99 throw new Exception("Request Param (jsonData) is Missing ..");
102 if(StringUtils.isBlank(systemName)){
103 throw new Exception("Request Param (systemName) is Missing ..");
106 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
107 if(propertyDefinition != null){
108 getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, systemName, ctx);
111 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
112 } catch (Exception e) {
113 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
114 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
115 log.error("Failed in merging data to template " + e.getMessage());
116 throw new SvcLogicException(e.getMessage());
121 public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
122 log.info("Received mergeJsonData call with params : " + inParams);
123 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
125 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
127 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
128 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
130 if(StringUtils.isBlank(requestParamJson)){
131 throw new Exception("Request Param (jsonData) is Missing ..");
134 if(StringUtils.isBlank(mergeJsonData)){
135 throw new Exception("Request Param (mergeJsonData) is Missing ..");
138 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
139 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
140 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
141 } catch (Exception e) {
142 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
143 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
144 log.error("Failed in merging data to template " + e.getMessage());
145 throw new SvcLogicException(e.getMessage());
152 private PropertyDefinition parsePDContent(String pdContent) throws JsonParseException, JsonMappingException, IOException{
153 PropertyDefinition propertyDefinition = null;
154 if(StringUtils.isNotBlank(pdContent)){
155 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
156 propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
158 return propertyDefinition;
162 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition, String requestParamJson) throws Exception{
164 if(propertyDefinition == null){
165 throw new Exception("PropertyDefinition is Missing ..");
168 if(StringUtils.isBlank(requestParamJson)){
169 throw new Exception("Request Param is Missing ..");
172 ObjectMapper mapper = new ObjectMapper();
173 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
174 if(requestParamMap != null){
175 List<Parameter> parameters = propertyDefinition.getParameters();
176 for (Parameter parameter : parameters) {
177 if(parameter != null){
179 log.info("Checking Key " + parameter.getName() + ":: Source :" +parameter.getSource());
180 // Add Only non external system keys,If it is not present in request Params
181 if( !requestParamMap.containsKey(parameter.getName())
182 && StringUtils.isBlank(parameter.getSource())
184 log.info("Adding New Key " + parameter.getName());
185 requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
189 requestParamJson = mapper.writeValueAsString(requestParamMap);
190 log.info("Processed Request Param " + requestParamJson);
193 return requestParamJson;
196 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition, String requestParamJson, String systemName, SvcLogicContext ctx) throws Exception{
198 if(propertyDefinition == null){
199 throw new Exception("PropertyDefinition is Missing ..");
202 if(StringUtils.isBlank(requestParamJson)){
203 throw new Exception("Request Param is Missing ..");
206 ObjectMapper mapper = new ObjectMapper();
207 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
208 if(requestParamMap != null){
209 List<Parameter> parameters = propertyDefinition.getParameters();
211 List<String> externalSystemKeys = new ArrayList<String>();
212 for (Parameter parameter : parameters) {
213 if(parameter != null){
214 if( !requestParamMap.containsKey(parameter.getName()) && StringUtils.isNotBlank(parameter.getSource()) ){
215 log.info("Adding New System Key " + parameter.getName() + ":"+ mapper.writeValueAsString(parameter));
216 externalSystemKeys.add(parameter.getName());
217 ctx.setAttribute(systemName +"."+parameter.getName(), mapper.writeValueAsString(parameter));
222 String systemKeys = systemName+".keys";
223 ctx.setAttribute(systemKeys, mapper.writeValueAsString(externalSystemKeys));
228 private String mergeJson(String requestParamJson, String systemParamJson) throws Exception {
229 ObjectMapper mapper = new ObjectMapper();
230 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
231 if(requestParamMap != null){
232 Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
233 if(systemParamMap != null){
234 for (String systemParamKey : systemParamMap.keySet()) {
235 log.trace("Megging System Key Values " + systemParamKey);
236 requestParamMap.put( systemParamKey , systemParamMap.get(systemParamKey));
239 requestParamJson = mapper.writeValueAsString(requestParamMap);
240 log.info("Processed Request Param " + requestParamJson);
243 return requestParamJson;