Initial commit for appc-config-params
[appc.git] / appc-config / appc-config-params / provider / src / main / java / org / openecomp / sdnc / config / params / parser / PropertyDefinitionNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  * 
11  *         http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.openecomp.sdnc.config.params.parser;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
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;
38
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;
45
46 public class PropertyDefinitionNode implements SvcLogicJavaPlugin{
47
48
49         private static final  EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
50
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);                
54                 try{
55                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
56
57                         String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);    
58                         String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);  
59
60                         if(StringUtils.isBlank(pdContent)){
61                                 throw new Exception("Request Param (pdContent) is Missing ..");
62                         }
63
64                         if(StringUtils.isBlank(requestParamJson)){
65                                 throw new Exception("Request Param (jsonData) is Missing ..");
66                         }
67
68                         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
69                         if(propertyDefinition != null){
70                                 requestParamJson = mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
71                                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
72                         }
73                         
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());
80                 }
81         }
82
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);                
86                 try{
87                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
88
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);        
92
93
94                         if(StringUtils.isBlank(pdContent)){
95                                 throw new Exception("Request Param (pdContent) is Missing ..");
96                         }
97
98                         if(StringUtils.isBlank(requestParamJson)){
99                                 throw new Exception("Request Param (jsonData) is Missing ..");
100                         }
101
102                         if(StringUtils.isBlank(systemName)){
103                                 throw new Exception("Request Param (systemName) is Missing ..");
104                         }
105
106                         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
107                         if(propertyDefinition != null){
108                                 getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, systemName, ctx);
109                         }
110
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());
117                 }
118         }
119
120
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);                
124                 try{
125                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
126
127                         String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);    
128                         String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);                        
129
130                         if(StringUtils.isBlank(requestParamJson)){
131                                 throw new Exception("Request Param (jsonData) is Missing ..");
132                         }
133
134                         if(StringUtils.isBlank(mergeJsonData)){
135                                 throw new Exception("Request Param (mergeJsonData) is Missing ..");
136                         }
137
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());
146                 }
147         }
148
149
150         /* */
151
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);
157                 }
158                 return propertyDefinition;
159         }
160
161
162         private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition, String requestParamJson) throws Exception{
163
164                 if(propertyDefinition == null){
165                         throw new Exception("PropertyDefinition is Missing ..");
166                 }
167
168                 if(StringUtils.isBlank(requestParamJson)){
169                         throw new Exception("Request Param is Missing ..");
170                 }
171
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){
178                                         
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())
183                                                         ){
184                                                 log.info("Adding New Key " + parameter.getName());      
185                                                 requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
186                                         }                                       
187                                 }                               
188                         }                       
189                         requestParamJson = mapper.writeValueAsString(requestParamMap);
190                         log.info("Processed Request Param " + requestParamJson);        
191                 }
192
193                 return requestParamJson;
194         }
195
196         private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition, String requestParamJson, String systemName, SvcLogicContext ctx) throws Exception{
197
198                 if(propertyDefinition == null){
199                         throw new Exception("PropertyDefinition is Missing ..");
200                 }
201
202                 if(StringUtils.isBlank(requestParamJson)){
203                         throw new Exception("Request Param is Missing ..");
204                 }
205
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();
210
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));
218                                         }                                       
219                                 }                               
220                         }       
221
222                         String systemKeys = systemName+".keys";
223                         ctx.setAttribute(systemKeys, mapper.writeValueAsString(externalSystemKeys));
224                 }
225         }
226
227
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));              
237                                 }       
238                         }
239                         requestParamJson = mapper.writeValueAsString(requestParamMap);
240                         log.info("Processed Request Param " + requestParamJson);        
241                 }
242
243                 return requestParamJson;
244         }
245
246
247
248
249 }