summaryrefslogtreecommitdiffstats
path: root/config-application-package/src/main/java/com/yahoo/config/model/application/provider/ApplicationPackageXmlFilesValidator.java
blob: 3f4a944cb44297f42561d91ac0e349f9314ea74c (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
// Copyright 2017 Yahoo Holdings. 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.component.Version;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.path.Path;
import com.yahoo.io.reader.NamedReader;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
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;
    
    /** The Vespa version this package tshould be validated against */
    private final Version vespaVersion;

    private static final FilenameFilter xmlFilter = (dir, name) -> name.endsWith(".xml");


    public ApplicationPackageXmlFilesValidator(AppSubDirs appDirs, Version vespaVersion) {
        this.appDirs = appDirs;
        this.vespaVersion = vespaVersion;
    }

    public static ApplicationPackageXmlFilesValidator createDefaultXMLValidator(File appDir, Version vespaVersion) {
        return new ApplicationPackageXmlFilesValidator(new AppSubDirs(appDir), vespaVersion);
    }

    public static ApplicationPackageXmlFilesValidator createTestXmlValidator(File appDir, Version vespaVersion) {
        return new ApplicationPackageXmlFilesValidator(new AppSubDirs(appDir), vespaVersion);
    }

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

        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, Version vespaVersion) 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", vespaVersion).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 validateDeploymentFile(String deploymentXmlSchemaName) throws IOException {
        if (appDirs.file(FilesApplicationPackage.DEPLOYMENT_FILE.getName()).exists()) {
            validate(deploymentXmlSchemaName, FilesApplicationPackage.DEPLOYMENT_FILE.getName());
        }
    }

    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, Version vespaVersion) {
        return new SchemaValidator(schemaFile, new BaseDeployLogger(), vespaVersion);
    }

}