summaryrefslogtreecommitdiffstats
path: root/config-application-package/src/main/java/com/yahoo/config/model/application/provider/ApplicationPackageXmlFilesValidator.java
blob: 393bd1c2de735cfda90cbffd0456a5053a339b83 (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;

import com.yahoo.collections.Tuple2;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.provision.Version;
import com.yahoo.path.Path;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.log.LogLevel;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * Validation of xml files in application package against RELAX NG schemas.
 *
 * @author hmusum
 */
public class ApplicationPackageXmlFilesValidator {

    private final AppSubDirs appDirs;
    private final DeployLogger logger;
    private final Optional<Version> vespaVersion;

    private static final FilenameFilter xmlFilter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".xml");
        }
    };

    public ApplicationPackageXmlFilesValidator(AppSubDirs appDirs, DeployLogger logger, Optional<Version> vespaVersion) {
        this.appDirs = appDirs;
        this.logger = logger;
        this.vespaVersion = vespaVersion;
    }

    public static ApplicationPackageXmlFilesValidator createDefaultXMLValidator(File appDir, DeployLogger logger, Optional<Version> vespaVersion) {
        return new ApplicationPackageXmlFilesValidator(new AppSubDirs(appDir), logger, vespaVersion);
    }

    public static ApplicationPackageXmlFilesValidator createTestXmlValidator(File appDir) {
        return new ApplicationPackageXmlFilesValidator(new AppSubDirs(appDir), new BaseDeployLogger(), Optional.<Version>empty());
    }

    // Verify that files a and b does not coexist.
    private void checkConflicts(String a, String b) throws IllegalArgumentException {
 	   if (appDirs.file(a).exists() && appDirs.file(b).exists())
 		   throw new IllegalArgumentException("Application package in " + appDirs.root() + " contains both " + a + " and " + b +
                                              ", please use just one of them");
    }

    @SuppressWarnings("deprecation")
    public void checkApplication() throws IOException {
        validateHostsFile(SchemaValidator.hostsXmlSchemaName);
        validateServicesFile(SchemaValidator.servicesXmlSchemaName);

        if (appDirs.searchdefinitions().exists()) {
            if (FilesApplicationPackage.getSearchDefinitionFiles(appDirs.root()).isEmpty()) {
                throw new IllegalArgumentException("Application package in " + appDirs.root() +
                        " must contain at least one search definition (.sd) file when directory searchdefinitions/ exists.");
            }
        }

        validate(appDirs.routingtables, "routing-standalone.rnc");
    }

    // For testing
    public static void checkIncludedDirs(ApplicationPackage app) throws IOException {
        for (String includedDir : app.getUserIncludeDirs()) {
            List<NamedReader> includedFiles = app.getFiles(Path.fromString(includedDir), ".xml", true);
            for (NamedReader file : includedFiles) {
                createSchemaValidator("container-include.rnc", Optional.empty()).validate(file);
            }
        }
    }

    @SuppressWarnings("deprecation")
    private void validateHostsFile(String hostsXmlSchemaName) throws IOException {
        if (appDirs.file(FilesApplicationPackage.HOSTS).exists()) {
            validate(hostsXmlSchemaName, FilesApplicationPackage.HOSTS);
        }

    }

    private void validateServicesFile(String servicesXmlSchemaName) throws IOException {
        // vespa-services.xml or services.xml. Fallback to vespa-services.xml
        validate(servicesXmlSchemaName, servicesFileName());
    }

    private void validate(String schemaName, String xmlFileName) throws IOException {
        createSchemaValidator(schemaName, vespaVersion).validate(appDirs.file(xmlFileName));
    }

    @SuppressWarnings("deprecation")
    private String servicesFileName() {
        String servicesFile = FilesApplicationPackage.SERVICES;
        if (!appDirs.file(servicesFile).exists()) {
            throw new IllegalArgumentException("Application package in " + appDirs.root() +
                                               " must contain " + FilesApplicationPackage.SERVICES);
        }
        return servicesFile;
    }

    private void validate(Tuple2<File, String> directory, String schemaFile) throws IOException {
        if ( ! directory.first.isDirectory()) return;
        validate(directory, createSchemaValidator(schemaFile, vespaVersion));
    }

    private void validate(Tuple2<File, String> directory, SchemaValidator validator) throws IOException {
        File dir = directory.first;
        if ( ! dir.isDirectory()) return;

        String directoryName = directory.second;
        for (File f : dir.listFiles(xmlFilter)) {
            if (f.isDirectory())
                validate(new Tuple2<>(f, directoryName + File.separator + f.getName()),validator);
            else
                validator.validate(f, directoryName + File.separator + f.getName());
        }
    }

    private static SchemaValidator createSchemaValidator(String schemaFile, Optional<Version> vespaVersion) {
        return new SchemaValidator(schemaFile, new BaseDeployLogger(), vespaVersion);
    }

}