Change to CCSDK and ODL Carbon
[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  * 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.sdnc.config.params.parser;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
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;
40
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;
47
48 public class PropertyDefinitionNode implements SvcLogicJavaPlugin{
49
50
51         private static final  EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
52
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);                
56                 try{
57                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
58
59                         String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);    
60                         String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);  
61
62                         if(StringUtils.isBlank(pdContent)){
63                                 throw new Exception("Request Param (pdContent) is Missing ..");
64                         }
65
66                         if(StringUtils.isBlank(requestParamJson)){
67                                 throw new Exception("Request Param (jsonData) is Missing ..");
68                         }
69
70                         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
71                         if(propertyDefinition != null){
72                                 requestParamJson = mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
73                                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, requestParamJson);
74                         }
75                         
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());
82                 }
83         }
84
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);                
88                 try{
89                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
90
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);        
94
95
96                         if(StringUtils.isBlank(pdContent)){
97                                 throw new Exception("Request Param (pdContent) is Missing ..");
98                         }
99
100                         if(StringUtils.isBlank(requestParamJson)){
101                                 throw new Exception("Request Param (jsonData) is Missing ..");
102                         }
103
104                         if(StringUtils.isBlank(systemName)){
105                                 throw new Exception("Request Param (systemName) is Missing ..");
106                         }
107
108                         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
109                         if(propertyDefinition != null){
110                                 getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, systemName, ctx);
111                         }
112
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());
119                 }
120         }
121
122
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);                
126                 try{
127                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
128
129                         String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);    
130                         String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);                        
131
132                         if(StringUtils.isBlank(requestParamJson)){
133                                 throw new Exception("Request Param (jsonData) is Missing ..");
134                         }
135
136                         if(StringUtils.isBlank(mergeJsonData)){
137                                 throw new Exception("Request Param (mergeJsonData) is Missing ..");
138                         }
139
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());
148                 }
149         }
150
151
152         /* */
153
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);
159                 }
160                 return propertyDefinition;
161         }
162
163
164         private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition, String requestParamJson) throws Exception{
165
166                 if(propertyDefinition == null){
167                         throw new Exception("PropertyDefinition is Missing ..");
168                 }
169
170                 if(StringUtils.isBlank(requestParamJson)){
171                         throw new Exception("Request Param is Missing ..");
172                 }
173
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){
180                                         
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())
185                                                         ){
186                                                 log.info("Adding New Key " + parameter.getName());      
187                                                 requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
188                                         }                                       
189                                 }                               
190                         }                       
191                         requestParamJson = mapper.writeValueAsString(requestParamMap);
192                         log.info("Processed Request Param " + requestParamJson);        
193                 }
194
195                 return requestParamJson;
196         }
197
198         private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition, String requestParamJson, String systemName, SvcLogicContext ctx) throws Exception{
199
200                 if(propertyDefinition == null){
201                         throw new Exception("PropertyDefinition is Missing ..");
202                 }
203
204                 if(StringUtils.isBlank(requestParamJson)){
205                         throw new Exception("Request Param is Missing ..");
206                 }
207
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();
212
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()) ){
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));
220                                         }                                       
221                                 }                               
222                         }       
223
224                         String systemKeys = systemName+".keys";
225                         ctx.setAttribute(systemKeys, mapper.writeValueAsString(externalSystemKeys));
226                 }
227         }
228
229
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));              
239                                 }       
240                         }
241                         requestParamJson = mapper.writeValueAsString(requestParamMap);
242                         log.info("Processed Request Param " + requestParamJson);        
243                 }
244
245                 return requestParamJson;
246         }
247
248
249
250
251 }