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