710b5ffd18cfa556e4e25578bf525c2305feaa79
[appc.git] / appc-config / appc-config-audit / provider / src / main / java / org / openecomp / sdnc / config / audit / node / CompareNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.config.audit.node;
22
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
33
34 public class CompareNode implements SvcLogicJavaPlugin
35 {
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     {
41         log.debug("Starting Compare Node Analysis");
42
43         HashMap<String, String> status = new HashMap<String, String>();
44         Parameters params = new Parameters(inParams);
45         try
46         {
47             if(params.getCompareDataType() != null)
48             {
49                 if(params.getPayloadX() !=null && params.getPayloadY() !=null)
50                 {
51                     status = getCompareResults(params);
52                     log.debug("Compare Result : " + status);
53                 }
54                 else
55                 {
56                     status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
57                     status.put(CompareConstants.ERROR_CODE, "200");
58                     status.put(CompareConstants.ERROR_MESSAGE, "One of the Data Received by CompareNode is Empty");
59                 }
60             }
61             else
62             {
63                 status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
64                 status.put(CompareConstants.ERROR_CODE, "200");
65                 status.put(CompareConstants.ERROR_MESSAGE, "Missing compareDataType value in input request: Expecting at least one of  CLI/RESTCONF/XML");
66             }
67
68         }
69         catch(Exception e)
70         {
71             status.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
72             status.put(CompareConstants.ERROR_CODE, "200");
73             status.put(CompareConstants.ERROR_MESSAGE, CompareConstants.ERROR_MESSAGE_DEATIL);
74             log.debug("Error in Comapre Node Execution " + e.getMessage());
75
76         }
77
78         createContextReposne(status, ctx, params.getRequestIdentifier());
79     }
80
81     private HashMap<String, String> getCompareResults(Parameters params) throws Exception
82     {
83         HashMap<String, String> resultMap = new HashMap<String, String>();
84         boolean cmpResult = false;
85         CompareDataInterface handler;
86
87
88
89         if(params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_JSON))
90             handler =  new CompareJsonData(params.getPayloadX(), params.getPayloadY());
91         else if((params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_XML))
92                 || (params.getCompareDataType().equalsIgnoreCase(CompareConstants.NETCONF_XML))
93                         || (params.getCompareDataType().equalsIgnoreCase(CompareConstants.RESTCONF_XML)))
94             handler =  new CompareXmlData(params.getPayloadX(), params.getPayloadY());
95         else if (params.getCompareDataType().equalsIgnoreCase(CompareConstants.FORMAT_CLI))
96             handler =  new CompareCliData(params.getPayloadX(), params.getPayloadY());
97         else
98         {
99             throw new Exception("Format " + params.getCompareDataType() + " not supported");
100         }
101         try
102         {
103             log.debug("Received Format to compare : " + params.getCompareDataType());
104
105             cmpResult = handler.compare();
106             if(cmpResult)
107             {
108                 resultMap.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_SUCCESS);
109
110             }
111             else
112             {
113                 resultMap.put(CompareConstants.RESPONSE_STATUS, CompareConstants.STATUS_FAILURE);
114                 resultMap.put(CompareConstants.ERROR_CODE, "500");
115                 resultMap.put(CompareConstants.ERROR_MESSAGE, CompareConstants.NO_MATCH_MESSAGE);
116             }
117         }
118         catch (Exception e)
119         {
120             throw e;
121         }
122
123     return resultMap;
124     }
125
126     private void createContextReposne(HashMap status, SvcLogicContext ctx, String requestIdentifier )
127     {
128         if(requestIdentifier == null)
129             requestIdentifier = "";
130         else
131             requestIdentifier = requestIdentifier + ".";
132
133         ctx.setAttribute(requestIdentifier.concat( CompareConstants.RESPONSE_STATUS), (String) status.get(CompareConstants.RESPONSE_STATUS));
134         ctx.setAttribute(requestIdentifier.concat(CompareConstants.ERROR_CODE), (String) status.get(CompareConstants.ERROR_CODE));
135         ctx.setAttribute(requestIdentifier.concat(CompareConstants.ERROR_MESSAGE), (String) status.get(CompareConstants.ERROR_MESSAGE));
136     }
137
138 }