Updates to config audit bundle
[appc.git] / appc-config / appc-config-audit / provider / src / main / java / org / onap / sdnc / config / audit / node / CompareXmlData.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 java.io.IOException;
29 import java.io.StringReader;
30
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34
35 import org.xmlunit.diff.ComparisonResult;
36 import org.xmlunit.diff.ComparisonType;
37 import org.xmlunit.diff.DefaultNodeMatcher;
38 import org.xmlunit.diff.ElementSelectors;
39 import org.xmlunit.util.Nodes;
40 import org.xmlunit.builder.DiffBuilder;
41 import org.w3c.dom.Attr;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48
49
50 public class CompareXmlData implements CompareDataInterface
51 {
52     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
53
54
55     String controlXml;
56     String testXml;
57
58     Document doc;
59
60     public CompareXmlData(String controlXml, String testXml) {
61         super();
62         this.controlXml = controlXml;
63         this.testXml = testXml;
64     }
65
66     @Override
67     public boolean compare() throws Exception
68     {
69
70         log.debug("controlXml : " + controlXml);
71         log.debug("testXml : " + testXml);
72         controlXml = controlXml.replace("junos:", "");
73         testXml = testXml.replace("junos:", "");
74         //doSetup();
75
76         try
77         {
78              //Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
79             final org.xmlunit.diff.Diff documentDiff = DiffBuilder
80                     .compare(controlXml)
81                     .withTest(testXml)
82                     .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
83                     .checkForSimilar()
84                     .withDifferenceEvaluator((comparison, outcome) -> {
85                             if (outcome != ComparisonResult.EQUAL && comparison.getType() == ComparisonType.ATTR_VALUE) {
86                                 Attr a = (Attr) comparison.getControlDetails().getTarget();
87                                 if ("commit-seconds".equals(Nodes.getQName(a).getLocalPart()) || "commit-localtime".equals(Nodes.getQName(a).getLocalPart())
88                                     && "configuration".equals(Nodes.getQName(a.getOwnerElement()).getLocalPart())) {
89                                     return ComparisonResult.EQUAL;
90                                 }
91                             }
92                             else
93                                 return ComparisonResult.SIMILAR;
94
95                             return outcome;
96                         })
97                     .ignoreComments()
98                     .ignoreWhitespace()
99                     .build();
100             if(documentDiff.hasDifferences())
101                 return false;
102             else
103                 return true;
104         }
105         catch(Exception e)
106         {
107             e.printStackTrace();
108             throw new Exception(e.getMessage());
109         }
110     }
111
112     /*private void doSetup() throws ParserConfigurationException, SAXException, IOException
113     {
114
115         XMLUnit.setIgnoreAttributeOrder(true);
116         XMLUnit.setIgnoreComments(true);
117         XMLUnit.setIgnoreWhitespace(true);
118     }*/
119
120
121     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
122     {
123         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
124         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
125         StringReader reader = new StringReader(inXml);
126         InputSource inputSource = new InputSource(reader);
127         Document doc = dBuilder.parse(inputSource);
128         doc.getDocumentElement().normalize();
129
130         return doc;
131     }
132 }