aboutsummaryrefslogtreecommitdiffstats
path: root/aai-schema-ingest/src/main/java/org/onap/aai/validation/nodes/DefaultDuplicateNodeDefinitionValidationModule.java
blob: 6181c52d8b266f9d7a2dfd85e274e13c50ff82df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.validation.nodes;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.onap.aai.setup.SchemaVersion;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Default duplicate rules for A&AI -
 * node types may never have a duplicate definition
 * within the same Version's file set.
 *
 * Finds all duplicates and what files they're in.
 *
 */
public class DefaultDuplicateNodeDefinitionValidationModule implements DuplicateNodeDefinitionValidationModule {

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.nodes.validation.DuplicateNodeDefinitionValidationModule#findDuplicates(java.util.List)
     */
    @Override
    public String findDuplicates(List<String> files, SchemaVersion v) {
        try {
            final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            Multimap<String, String> types = ArrayListMultimap.create();
            boolean foundDups = false;
            for (String file : files) {
                InputStream inputStream = new FileInputStream(file);
                final Document doc = docBuilder.parse(inputStream);
                final NodeList list = doc.getElementsByTagName("java-type");

                for (int i = 0; i < list.getLength(); i++) {
                    String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
                    if (types.containsKey(type)) {
                        foundDups = true;
                    }
                    types.put(type, file);
                }
            }

            if (foundDups) {
                return buildErrorMsg(types, v);
            } else {
                return "";
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            // TODO something useful with this information
            return e.getMessage();
        }
    }

    private String buildErrorMsg(Multimap<String, String> types, SchemaVersion v) {
        StringBuilder errorMsg =
                new StringBuilder().append("Duplicates found in version ").append(v.toString()).append(". ");
        for (String nodeType : types.keySet()) {
            Collection<String> files = types.get(nodeType);
            if (files.size() == 1) {
                continue; // only record the duplicated ones
            }
            errorMsg.append(nodeType).append(" has definitions in ");
            for (String file : files) {
                errorMsg.append(file).append(" ");
            }
        }
        return errorMsg.toString();
    }

}