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