2c4706d6e40a7e685c91609c9fd9441c454cc633
[appc.git] / appc-config / appc-config-audit / provider / src / main / java / org / onap / sdnc / config / audit / node / CompareNode.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.onap.sdnc.config.audit.node;
26
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
35
36 public class CompareNode implements SvcLogicJavaPlugin {
37
38     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareNode.class);
39
40     public void compare(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
41         log.debug("Starting Compare Node Analysis");
42
43         Map<String, String> status = new HashMap<>();
44         Parameters params = new Parameters(inParams);
45         try {
46             if (params.getCompareDataType() != null) {
47                 if (params.getPayloadX() != null && params.getPayloadY() != null) {
48                     status = getCompareResults(params);
49                     log.debug("Compare Result : " + status);
50                 } else {
51                     status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
52                     status.put(CompareConstants.ERROR_CODE, "200");
53                     status.put(CompareConstants.ERROR_MESSAGE, "One of the Data Received by CompareNode is Empty");
54                 }
55             } else {
56                 status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
57                 status.put(CompareConstants.ERROR_CODE, "200");
58                 status.put(CompareConstants.ERROR_MESSAGE,
59                     "Missing compareDataType value in input request: Expecting at least one of  CLI/RESTCONF/XML");
60             }
61
62         } catch (Exception e) {
63             status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
64             status.put(CompareConstants.ERROR_CODE, "200");
65             status.put(CompareConstants.ERROR_MESSAGE, CompareConstants.ERROR_MESSAGE_DETAIL);
66             log.debug("Error in Comapre Node Execution", e);
67         }
68         createContextResponse(status, ctx, params.getRequestIdentifier());
69     }
70
71     private HashMap<String, String> getCompareResults(Parameters params) throws Exception {
72         HashMap<String, String> resultMap = new HashMap<>();
73         boolean cmpResult;
74         CompareDataInterface handler;
75
76         if (params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_JSON)) {
77             handler = new CompareJsonData(params.getPayloadX(), params.getPayloadY());
78         } else if ((params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_XML))
79             || (params.getCompareDataType().equalsIgnoreCase(CompareConstants.NETCONF_XML))
80             || (params.getCompareDataType().equalsIgnoreCase(CompareConstants.RESTCONF_XML))) {
81             handler = new CompareXmlData(params.getPayloadX(), params.getPayloadY());
82         } else if (params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_CLI)) {
83             handler = new CompareCliData(params.getPayloadX(), params.getPayloadY());
84         } else {
85             throw new SvcLogicException("Format " + params.getCompareDataType() + " not supported");
86         }
87         try {
88             log.debug("Received Format to compare : " + params.getCompareDataType());
89
90             cmpResult = handler.compare();
91             if (cmpResult) {
92                 resultMap.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_SUCCESS);
93
94             } else {
95                 resultMap.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
96                 resultMap.put(CompareConstants.ERROR_CODE, "500");
97                 resultMap.put(CompareConstants.ERROR_MESSAGE, CompareConstants.NO_MATCH_MESSAGE);
98             }
99         } catch (Exception e) {
100             throw e;
101         }
102         return resultMap;
103     }
104
105     private void createContextResponse(Map<String, String> status, SvcLogicContext ctx, String requestIdentifier) {
106         String requestId = requestIdentifier == null ? "" : requestIdentifier + ".";
107
108         ctx.setAttribute(requestId.concat(CompareConstants.RESPONSE_STATUS),
109             status.get(CompareConstants.RESPONSE_STATUS));
110         ctx.setAttribute(requestId.concat(CompareConstants.ERROR_CODE),
111             status.get(CompareConstants.ERROR_CODE));
112         ctx.setAttribute(requestId.concat(CompareConstants.ERROR_MESSAGE),
113             status.get(CompareConstants.ERROR_MESSAGE));
114     }
115 }