Integrate aai-schema-ingest library into aai-core
[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 java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.List;
28
29 import javax.xml.XMLConstants;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33
34 import org.onap.aai.setup.SchemaVersion;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.NodeList;
37 import org.xml.sax.SAXException;
38
39 import com.google.common.collect.ArrayListMultimap;
40 import com.google.common.collect.Multimap;
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         /* (non-Javadoc)
53          * @see org.onap.aai.nodes.validation.DuplicateNodeDefinitionValidationModule#findDuplicates(java.util.List)
54          */
55         @Override
56         public String findDuplicates(List<String> files, SchemaVersion v) {
57                 try {
58                         final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
59                         docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
60                         final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
61                                 
62                         Multimap<String, String> types = ArrayListMultimap.create();
63                         boolean foundDups = false;
64                         for (String file : files) {
65                                 InputStream inputStream = new FileInputStream(file);
66                                 final Document doc = docBuilder.parse(inputStream);
67                                 final NodeList list = doc.getElementsByTagName("java-type");
68         
69                                 for (int i = 0; i < list.getLength(); i++) {
70                                         String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
71                                         if (types.containsKey(type)) {
72                                                 foundDups = true;
73                                         }
74                                         types.put(type, file);
75                                 }
76                         }
77                         
78                         if (foundDups) {
79                                 return buildErrorMsg(types, v);
80                         } else {
81                                 return "";
82                         }
83                 } catch (ParserConfigurationException | SAXException | IOException e) {
84                         // TODO something useful with this information
85                         return e.getMessage();
86                 }
87         }
88         
89         private String buildErrorMsg(Multimap<String, String> types, SchemaVersion v) {
90                 StringBuilder errorMsg = new StringBuilder().append("Duplicates found in version ").append(v.toString()).append(". ");
91                 for (String nodeType : types.keySet()) {
92                         Collection<String> files = types.get(nodeType);
93                         if (files.size() == 1) {
94                                 continue; //only record the duplicated ones
95                         }
96                         errorMsg.append(nodeType).append(" has definitions in ");
97                         for (String file : files) {
98                                 errorMsg.append(file).append(" ");
99                         }
100                 }
101                 return errorMsg.toString();
102         }
103
104 }