Merge "Release 1.14.0 maven artifact"
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / nodes / DefaultDuplicateNodeDefinitionValidationModule.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.onap.aai.validation.nodes;
22
23 import com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.Multimap;
25
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Collection;
30 import java.util.List;
31
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36
37 import org.onap.aai.setup.SchemaVersion;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
41
42 /**
43  * Default duplicate rules for A&AI -
44  * node types may never have a duplicate definition
45  * within the same Version's file set.
46  *
47  * Finds all duplicates and what files they're in.
48  *
49  */
50 public class DefaultDuplicateNodeDefinitionValidationModule implements DuplicateNodeDefinitionValidationModule {
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.onap.aai.nodes.validation.DuplicateNodeDefinitionValidationModule#findDuplicates(java.util.List)
56      */
57     @Override
58     public String findDuplicates(List<String> files, SchemaVersion v) {
59         try {
60             final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
61             docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
62             docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
63             docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
64             final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
65
66             Multimap<String, String> types = ArrayListMultimap.create();
67             boolean foundDups = false;
68             for (String file : files) {
69                 InputStream inputStream = new FileInputStream(file);
70                 final Document doc = docBuilder.parse(inputStream);
71                 final NodeList list = doc.getElementsByTagName("java-type");
72
73                 for (int i = 0; i < list.getLength(); i++) {
74                     String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
75                     if (types.containsKey(type)) {
76                         foundDups = true;
77                     }
78                     types.put(type, file);
79                 }
80             }
81
82             if (foundDups) {
83                 return buildErrorMsg(types, v);
84             } else {
85                 return "";
86             }
87         } catch (ParserConfigurationException | SAXException | IOException e) {
88             // TODO something useful with this information
89             return e.getMessage();
90         }
91     }
92
93     private String buildErrorMsg(Multimap<String, String> types, SchemaVersion v) {
94         StringBuilder errorMsg =
95                 new StringBuilder().append("Duplicates found in version ").append(v.toString()).append(". ");
96         for (String nodeType : types.keySet()) {
97             Collection<String> files = types.get(nodeType);
98             if (files.size() == 1) {
99                 continue; // only record the duplicated ones
100             }
101             errorMsg.append(nodeType).append(" has definitions in ");
102             for (String file : files) {
103                 errorMsg.append(file).append(" ");
104             }
105         }
106         return errorMsg.toString();
107     }
108
109 }