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