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.onap.sdnc.config.params.parser;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 import org.onap.sdnc.config.params.ParamsHandlerConstant;
37 import org.onap.sdnc.config.params.data.Parameter;
38 import org.onap.sdnc.config.params.data.PropertyDefinition;
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 =
50 EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
52 public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
53 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) {
73 mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
75 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
79 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
80 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
81 } catch (Exception e) {
82 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
83 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
84 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
86 log.error("Failed in merging data to template " + e.getMessage());
87 throw new SvcLogicException(e.getMessage());
91 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
92 throws SvcLogicException {
93 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
95 "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!");
96 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
98 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
100 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
101 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
102 String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);
105 if (StringUtils.isBlank(pdContent)) {
106 throw new Exception("Request Param (pdContent) is Missing ..");
109 if (StringUtils.isBlank(requestParamJson)) {
110 throw new Exception("Request Param (jsonData) is Missing ..");
113 if (StringUtils.isBlank(systemName)) {
114 throw new Exception("Request Param (systemName) is Missing ..");
117 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
118 if (propertyDefinition != null) {
119 getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx);
122 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
123 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
124 } catch (Exception e) {
125 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
126 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
127 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
129 log.error("Failed in merging data to template " + e.getMessage());
130 throw new SvcLogicException(e.getMessage());
135 public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx)
136 throws SvcLogicException {
137 log.info("Received mergeJsonData call with params : " + inParams);
138 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
140 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
142 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
143 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
145 if (StringUtils.isBlank(requestParamJson)) {
146 throw new Exception("Request Param (jsonData) is Missing ..");
149 if (StringUtils.isBlank(mergeJsonData)) {
150 throw new Exception("Request Param (mergeJsonData) is Missing ..");
153 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
155 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
157 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
158 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
159 } catch (Exception e) {
160 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
161 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
162 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
164 log.error("Failed in merging data to template " + e.getMessage());
165 throw new SvcLogicException(e.getMessage());
172 private PropertyDefinition parsePDContent(String pdContent)
173 throws JsonParseException, JsonMappingException, IOException {
174 PropertyDefinition propertyDefinition = null;
175 if (StringUtils.isNotBlank(pdContent)) {
176 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
177 propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
179 return propertyDefinition;
183 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
184 String requestParamJson) throws Exception {
186 if (propertyDefinition == null) {
187 throw new Exception("PropertyDefinition is Missing ..");
190 if (StringUtils.isBlank(requestParamJson)) {
191 throw new Exception("Request Param is Missing ..");
194 ObjectMapper mapper = new ObjectMapper();
195 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
196 if (requestParamMap != null) {
197 List<Parameter> parameters = propertyDefinition.getParameters();
198 for (Parameter parameter : parameters) {
199 if (parameter != null) {
201 log.info("Checking Key " + parameter.getName() + ":: Source :"
202 + parameter.getSource());
203 // Add Only non external system keys,If it is not present in request Params
204 if (!requestParamMap.containsKey(parameter.getName())
205 && StringUtils.isBlank(parameter.getSource())) {
206 log.info("Adding New Key " + parameter.getName());
207 requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
211 requestParamJson = mapper.writeValueAsString(requestParamMap);
212 log.info("Processed Request Param " + requestParamJson);
215 return requestParamJson;
218 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
219 String requestParamJson, SvcLogicContext ctx) throws Exception {
221 if (propertyDefinition == null) {
222 throw new Exception("PropertyDefinition is Missing ..");
225 if (StringUtils.isBlank(requestParamJson)) {
226 throw new Exception("Request Param is Missing ..");
229 ObjectMapper mapper = new ObjectMapper();
230 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
231 Map<String, List<String>> systemKeysMap = new HashMap<String, List<String>>();
232 if (requestParamMap != null) {
233 List<Parameter> parameters = propertyDefinition.getParameters();
235 List<String> externalSystemKeys = new ArrayList<String>();
236 for (Parameter parameter : parameters) {
237 if (parameter != null) {
238 if (!requestParamMap.containsKey(parameter.getName())
239 && StringUtils.isNotBlank(parameter.getSource())
240 && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual")) {
241 String source = parameter.getSource();
242 if (StringUtils.equalsIgnoreCase(source, "A&AI"))
244 source = StringUtils.upperCase(source);
245 if (systemKeysMap.containsKey(source)) {
246 log.info("Adding New System Key " + parameter.getName() + ":"
247 + mapper.writeValueAsString(parameter));
248 List l = systemKeysMap.get(source);
250 l.add(parameter.getName());
251 systemKeysMap.put(source, l);
254 log.info("Creating/Adding New System Key " + parameter.getName() + ":"
255 + mapper.writeValueAsString(parameter));
256 List<String> l = new ArrayList<String>();
258 l.add(parameter.getName());
259 systemKeysMap.put(source, l);
263 externalSystemKeys.add(parameter.getName());
264 ctx.setAttribute(source + "." + parameter.getName(),
265 mapper.writeValueAsString(parameter));
269 for (String sysName : systemKeysMap.keySet()) {
270 String systemKeys = sysName + ".keys";
271 ctx.setAttribute(systemKeys, mapper.writeValueAsString(systemKeysMap.get(sysName)));
274 * String systemKeys = systemName+".keys"; ctx.setAttribute(systemKeys,
275 * mapper.writeValueAsString(externalSystemKeys));
282 private String mergeJson(String requestParamJson, String systemParamJson) throws Exception {
283 ObjectMapper mapper = new ObjectMapper();
284 Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
285 if (requestParamMap != null) {
286 Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
287 if (systemParamMap != null) {
288 for (String systemParamKey : systemParamMap.keySet()) {
289 log.trace("Megging System Key Values " + systemParamKey);
290 requestParamMap.put(systemParamKey, systemParamMap.get(systemParamKey));
293 requestParamJson = mapper.writeValueAsString(requestParamMap);
294 log.info("Processed Request Param " + requestParamJson);
297 return requestParamJson;
301 public void validateParams(Map<String, String> inParams, SvcLogicContext ctx)
302 throws SvcLogicException
305 String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
306 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
307 String configParams =
308 inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
309 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
310 log.info("Processed pdContent Param " + pdContent);
311 log.info("Processed config Param " + configParams);
314 if (StringUtils.isBlank(pdContent)) {
315 throw new Exception("Request Param (pdContent) is Missing ..");
318 if (StringUtils.isBlank(configParams)) {
319 throw new Exception("Request Param (configParams) is Missing ..");
321 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
322 ObjectMapper mapper = new ObjectMapper();
323 Map<String, String> requestParamMap = mapper.readValue(configParams, HashMap.class);
324 List<Parameter> parameters = propertyDefinition.getParameters();
325 Map<String, String> missingKeys = new HashMap<String, String>();
326 for (Parameter parameter : parameters) {
327 if (parameter != null) {
328 if (parameter.isRequired()) {
329 if (!requestParamMap.containsKey(parameter.getName())) {
330 missingKeys.put(parameter.getName(), parameter.getSource());
332 if ((requestParamMap.get(parameter.getName()) == null)
333 || (requestParamMap.get(parameter.getName()).equals("")))
336 missingKeys.put(parameter.getName(), parameter.getSource());
347 if (missingKeys != null && missingKeys.size() > 0) {
349 String requiredFields = mapper.writeValueAsString(missingKeys);
350 log.info(" Below mentioned keys and respective source type are mandatory");
351 log.info(requiredFields);
353 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
354 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
355 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
356 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
357 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
358 "Missing Mandatory Keys and source are" + requiredFields);
359 throw new SvcLogicException(
360 " Missing Mandatory Keys and source are" + requiredFields);
364 log.info("success ");
365 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
366 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
370 } catch (Exception e) {
372 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
373 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
374 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
375 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
376 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
378 log.error(e.getMessage());
379 throw new SvcLogicException(e.getMessage());