aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/PropertyDefinitionNode.java
blob: aafebda1765b98d3f4d0557542c3b72c656613b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdnc.config.params.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
import org.onap.sdnc.config.params.ParamsHandlerConstant;
import org.onap.sdnc.config.params.data.Parameter;
import org.onap.sdnc.config.params.data.PropertyDefinition;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class PropertyDefinitionNode implements SvcLogicJavaPlugin {


    private static final EELFLogger log =
            EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);

    public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
            throws SvcLogicException {
        log.info("Received processParamKeys call with params : " + inParams);
        String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
        try {
            responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";

            String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
            String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);

            if (StringUtils.isBlank(pdContent)) {
                throw new Exception("Request Param (pdContent) is Missing ..");
            }

            if (StringUtils.isBlank(requestParamJson)) {
                throw new Exception("Request Param (jsonData) is Missing ..");
            }

            PropertyDefinition propertyDefinition = parsePDContent(pdContent);
            if (propertyDefinition != null) {
                requestParamJson =
                        mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
                ctx.setAttribute(
                        responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
                        requestParamJson);
            }

            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
        } catch (Exception e) {
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
                    e.getMessage());
            log.error("Failed in merging data to template " + e.getMessage());
            throw new SvcLogicException(e.getMessage());
        }
    }

    public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
            throws SvcLogicException {
        log.info("Received processExternalSystemParamKeys call with params : " + inParams);
        log.debug(
                "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!");
        String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
        try {
            responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";

            String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
            String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
            String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);


            if (StringUtils.isBlank(pdContent)) {
                throw new Exception("Request Param (pdContent) is Missing ..");
            }

            if (StringUtils.isBlank(requestParamJson)) {
                throw new Exception("Request Param (jsonData) is Missing ..");
            }

            if (StringUtils.isBlank(systemName)) {
                throw new Exception("Request Param (systemName) is Missing ..");
            }

            PropertyDefinition propertyDefinition = parsePDContent(pdContent);
            if (propertyDefinition != null) {
                getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx);
            }

            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
        } catch (Exception e) {
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
                    e.getMessage());
            log.error("Failed in merging data to template " + e.getMessage());
            throw new SvcLogicException(e.getMessage());
        }
    }


    public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx)
            throws SvcLogicException {
        log.info("Received mergeJsonData call with params : " + inParams);
        String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
        try {
            responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";

            String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
            String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);

            if (StringUtils.isBlank(requestParamJson)) {
                throw new Exception("Request Param (jsonData) is Missing ..");
            }

            if (StringUtils.isBlank(mergeJsonData)) {
                throw new Exception("Request Param (mergeJsonData) is Missing ..");
            }

            requestParamJson = mergeJson(requestParamJson, mergeJsonData);
            ctx.setAttribute(
                    responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
                    requestParamJson);
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
        } catch (Exception e) {
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
                    e.getMessage());
            log.error("Failed in merging data to template " + e.getMessage());
            throw new SvcLogicException(e.getMessage());
        }
    }


    /* */

    private PropertyDefinition parsePDContent(String pdContent)
            throws JsonParseException, JsonMappingException, IOException {
        PropertyDefinition propertyDefinition = null;
        if (StringUtils.isNotBlank(pdContent)) {
            ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
            propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
        }
        return propertyDefinition;
    }


    private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
            String requestParamJson) throws Exception {

        if (propertyDefinition == null) {
            throw new Exception("PropertyDefinition is Missing ..");
        }

        if (StringUtils.isBlank(requestParamJson)) {
            throw new Exception("Request Param is Missing ..");
        }

        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
        if (requestParamMap != null) {
            List<Parameter> parameters = propertyDefinition.getParameters();
            for (Parameter parameter : parameters) {
                if (parameter != null) {

                    log.info("Checking Key " + parameter.getName() + ":: Source :"
                            + parameter.getSource());
                    // Add Only non external system keys,If it is not present in request Params
                    if (!requestParamMap.containsKey(parameter.getName())
                            && StringUtils.isBlank(parameter.getSource())) {
                        log.info("Adding New Key " + parameter.getName());
                        requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
                    }
                }
            }
            requestParamJson = mapper.writeValueAsString(requestParamMap);
            log.info("Processed Request Param " + requestParamJson);
        }

        return requestParamJson;
    }

    private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
            String requestParamJson, SvcLogicContext ctx) throws Exception {

        if (propertyDefinition == null) {
            throw new Exception("PropertyDefinition is Missing ..");
        }

        if (StringUtils.isBlank(requestParamJson)) {
            throw new Exception("Request Param is Missing ..");
        }

        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
        Map<String, List<String>> systemKeysMap = new HashMap<String, List<String>>();
        if (requestParamMap != null) {
            List<Parameter> parameters = propertyDefinition.getParameters();

            List<String> externalSystemKeys = new ArrayList<String>();
            for (Parameter parameter : parameters) {
                if (parameter != null) {
                    if (!requestParamMap.containsKey(parameter.getName())
                            && StringUtils.isNotBlank(parameter.getSource())
                            && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual")) {
                        String source = parameter.getSource();
                        if (StringUtils.equalsIgnoreCase(source, "A&AI"))
                            source = "AAI";
                        source = StringUtils.upperCase(source);
                        if (systemKeysMap.containsKey(source)) {
                            log.info("Adding New System Key " + parameter.getName() + ":"
                                    + mapper.writeValueAsString(parameter));
                            List l = systemKeysMap.get(source);
                            if (null != l) {
                                l.add(parameter.getName());
                                systemKeysMap.put(source, l);
                            }
                        } else {
                            log.info("Creating/Adding New System Key " + parameter.getName() + ":"
                                    + mapper.writeValueAsString(parameter));
                            List<String> l = new ArrayList<String>();
                            if (null != l) {
                                l.add(parameter.getName());
                                systemKeysMap.put(source, l);
                            }
                        }

                        externalSystemKeys.add(parameter.getName());
                        ctx.setAttribute(source + "." + parameter.getName(),
                                mapper.writeValueAsString(parameter));
                    }
                }
            }
            for (String sysName : systemKeysMap.keySet()) {
                String systemKeys = sysName + ".keys";
                ctx.setAttribute(systemKeys, mapper.writeValueAsString(systemKeysMap.get(sysName)));
            }
            /*
             * String systemKeys = systemName+".keys"; ctx.setAttribute(systemKeys,
             * mapper.writeValueAsString(externalSystemKeys));
             */

        }
    }


    private String mergeJson(String requestParamJson, String systemParamJson) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
        if (requestParamMap != null) {
            Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
            if (systemParamMap != null) {
                for (String systemParamKey : systemParamMap.keySet()) {
                    log.trace("Megging System Key Values " + systemParamKey);
                    requestParamMap.put(systemParamKey, systemParamMap.get(systemParamKey));
                }
            }
            requestParamJson = mapper.writeValueAsString(requestParamMap);
            log.info("Processed Request Param " + requestParamJson);
        }

        return requestParamJson;
    }


    public void validateParams(Map<String, String> inParams, SvcLogicContext ctx)
            throws SvcLogicException

    {
        String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
        String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
        String configParams =
                inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
        responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
        log.info("Processed pdContent Param " + pdContent);
        log.info("Processed config Param " + configParams);

        try {
            if (StringUtils.isBlank(pdContent)) {
                throw new Exception("Request Param (pdContent) is Missing ..");
            }

            if (StringUtils.isBlank(configParams)) {
                throw new Exception("Request Param (configParams) is Missing ..");
            }
            PropertyDefinition propertyDefinition = parsePDContent(pdContent);
            ObjectMapper mapper = new ObjectMapper();
            Map<String, String> requestParamMap = mapper.readValue(configParams, HashMap.class);
            List<Parameter> parameters = propertyDefinition.getParameters();
            Map<String, String> missingKeys = new HashMap<String, String>();
            for (Parameter parameter : parameters) {
                if (parameter != null) {
                    if (parameter.isRequired()) {
                        if (!requestParamMap.containsKey(parameter.getName())) {
                            missingKeys.put(parameter.getName(), parameter.getSource());
                        } else {
                            if ((requestParamMap.get(parameter.getName()) == null)
                                    || (requestParamMap.get(parameter.getName()).equals("")))

                            {
                                missingKeys.put(parameter.getName(), parameter.getSource());
                            }

                        }

                    }

                }

            }

            if (missingKeys != null && missingKeys.size() > 0) {

                String requiredFields = mapper.writeValueAsString(missingKeys);
                log.info(" Below mentioned keys and respective  source type are mandatory");
                log.info(requiredFields);

                ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                        ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
                ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                        ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
                ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
                        "Missing Mandatory Keys and source are" + requiredFields);
                throw new SvcLogicException(
                        " Missing  Mandatory Keys and source are" + requiredFields);


            } else {
                log.info("success ");
                ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                        ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);

            }

        } catch (Exception e) {

            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
            ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
                    ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
            ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
                    e.getMessage());
            log.error(e.getMessage());
            throw new SvcLogicException(e.getMessage());
        }

    }



}