2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.sdnc.config.params.parser;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdnc.config.params.ParamsHandlerConstant;
35 import org.openecomp.sdnc.config.params.data.Parameter;
36 import org.openecomp.sdnc.config.params.data.PropertyDefinition;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.fasterxml.jackson.core.JsonParseException;
44 import com.fasterxml.jackson.databind.JsonMappingException;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
48 public class PropertyDefinitionNode implements SvcLogicJavaPlugin{
51 private static final EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
53 public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
54 log.info("Received processParamKeys call with params : " + inParams);
55 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
57 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
59 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
60 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
62 if(StringUtils.isBlank(pdContent)){
63 throw new Exception("Request Param (pdContent) is Missing ..");
66 if(StringUtils.isBlank(requestParamJson)){
67 throw new Exception("Request Param (jsonData) is Missing ..");
70 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
71 if(propertyDefinition != null){
72 requestParamJson = mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
73 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
76 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
77 } catch (Exception e) {
78 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
79 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
80 log.error("Failed in merging data to template " + e.getMessage());
81 throw new SvcLogicException(e.getMessage());
85 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
86 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
87 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
89 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
91 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
92 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
93 String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);
96 if(StringUtils.isBlank(pdContent)){
97 throw new Exception("Request Param (pdContent) is Missing ..");
100 if(StringUtils.isBlank(requestParamJson)){
101 throw new Exception("Request Param (jsonData) is Missing ..");
104 if(StringUtils.isBlank(systemName)){
105 throw new Exception("Request Param (systemName) is Missing ..");
108 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
109 if(propertyDefinition != null){
110 getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, systemName, ctx);
113 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
114 } catch (Exception e) {
115 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
116 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
117 log.error("Failed in merging data to template " + e.getMessage());
118 throw new SvcLogicException(e.getMessage());
123 public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
124 log.info("Received mergeJsonData call with params : " + inParams);
125 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
127 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
129 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
130 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
132 if(StringUtils.isBlank(requestParamJson)){
133 throw new Exception("Request Param (jsonData) is Missing ..");
136 if(StringUtils.isBlank(mergeJsonData)){
137 throw new Exception("Request Param (mergeJsonData) is Missing ..");
140 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
141 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
142 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
143 } catch (Exception e) {
144 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
145 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
146 log.error("Failed in merging data to template " + e.getMessage());
147 throw new SvcLogicException(e.getMessage());
154 private PropertyDefinition parsePDContent(String pdContent) throws JsonParseException, JsonMappingException, IOException{
155 PropertyDefinition propertyDefinition = null;
156 if(StringUtils.isNotBlank(pdContent)){
157 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
158 propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
160 return propertyDefinition;
164 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition, String requestParamJson) throws Exception{
166 if(propertyDefinition == null){
167 throw new Exception("PropertyDefinition is Missing ..");
170 if(StringUtils.isBlank(requestParamJson)){
171 throw new Exception("Request Param is Missing ..");
174 ObjectMapper mapper = new ObjectMapper();
175 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
176 if(requestParamMap != null){
177 List<Parameter> parameters = propertyDefinition.getParameters();
178 for (Parameter parameter : parameters) {
179 if(parameter != null){
181 log.info("Checking Key " + parameter.getName() + ":: Source :" +parameter.getSource());
182 // Add Only non external system keys,If it is not present in request Params
183 if( !requestParamMap.containsKey(parameter.getName())
184 && StringUtils.isBlank(parameter.getSource())
186 log.info("Adding New Key " + parameter.getName());
187 requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
191 requestParamJson = mapper.writeValueAsString(requestParamMap);
192 log.info("Processed Request Param " + requestParamJson);
195 return requestParamJson;
198 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition, String requestParamJson, String systemName, SvcLogicContext ctx) throws Exception{
200 if(propertyDefinition == null){
201 throw new Exception("PropertyDefinition is Missing ..");
204 if(StringUtils.isBlank(requestParamJson)){
205 throw new Exception("Request Param is Missing ..");
208 ObjectMapper mapper = new ObjectMapper();
209 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
210 if(requestParamMap != null){
211 List<Parameter> parameters = propertyDefinition.getParameters();
213 List<String> externalSystemKeys = new ArrayList<String>();
214 for (Parameter parameter : parameters) {
215 if(parameter != null){
216 if( !requestParamMap.containsKey(parameter.getName()) && StringUtils.isNotBlank(parameter.getSource()) && !StringUtils.equalsIgnoreCase(parameter.getSource(),"Manual") ){
217 log.info("Adding New System Key " + parameter.getName() + ":"+ mapper.writeValueAsString(parameter));
218 externalSystemKeys.add(parameter.getName());
219 ctx.setAttribute(systemName +"."+parameter.getName(), mapper.writeValueAsString(parameter));
224 String systemKeys = systemName+".keys";
225 ctx.setAttribute(systemKeys, mapper.writeValueAsString(externalSystemKeys));
230 private String mergeJson(String requestParamJson, String systemParamJson) throws Exception {
231 ObjectMapper mapper = new ObjectMapper();
232 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
233 if(requestParamMap != null){
234 Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
235 if(systemParamMap != null){
236 for (String systemParamKey : systemParamMap.keySet()) {
237 log.trace("Megging System Key Values " + systemParamKey);
238 requestParamMap.put( systemParamKey , systemParamMap.get(systemParamKey));
241 requestParamJson = mapper.writeValueAsString(requestParamMap);
242 log.info("Processed Request Param " + requestParamJson);
245 return requestParamJson;