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