Add generalized FileGenerator interface
[yangtools.git] / yang / yang-maven-plugin-spi / src / main / java / org / opendaylight / yangtools / yang2sources / spi / BasicCodeGenerator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.spi;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.Set;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19
20 /**
21  * Maven 3.1.x and newer uses SLF4J internally, which means we do not need to pass a logger instance around.
22  */
23 public interface BasicCodeGenerator {
24     enum ImportResolutionMode {
25         /**
26          * Standard, RFC6020 and RFC7950 compliant mode. Imports are satisfied by exact revision match (if specified),
27          * or by latest available revision.
28          */
29         REVISION_EXACT_OR_LATEST(FileGenerator.ImportResolutionMode.REVISION_EXACT_OR_LATEST),
30         /**
31          * Semantic version based mode. Imports which specify a semantic version (via the OpenConfig extension) will
32          * be satisfied by module which exports the latest compatible revision. Imports which do not specify semantic
33          * version will be resolved just as they would be via {@link #REVISION_EXACT_OR_LATEST}.
34          */
35         SEMVER_LATEST(FileGenerator.ImportResolutionMode.SEMVER_LATEST);
36
37         private final FileGenerator.@NonNull ImportResolutionMode fileGeneratorMode;
38
39         ImportResolutionMode(final FileGenerator.@NonNull ImportResolutionMode fileGeneratorMode) {
40             this.fileGeneratorMode = fileGeneratorMode;
41         }
42
43         /**
44          * Return {@link FileGenerator.ImportResolutionMode} equivalent of this mode.
45          *
46          * @return {@link FileGenerator.ImportResolutionMode} equivalent of this mode
47          */
48         public final FileGenerator.@NonNull ImportResolutionMode toFileGeneratorMode() {
49             return fileGeneratorMode;
50         }
51     }
52
53     /**
54      * Generate sources from provided {@link EffectiveModelContext}.
55      *
56      * @param context
57      *            parsed from YANG files
58      * @param outputBaseDir
59      *            expected output directory for generated sources configured by
60      *            user
61      * @param currentModules
62      *            YANG modules parsed from yangFilesRootDir
63      * @param moduleResourcePathResolver
64      *            Function converting a local module to the packaged resource path
65      * @return collection of files that were generated from schema context
66      */
67     Collection<File> generateSources(EffectiveModelContext context, File outputBaseDir, Set<Module> currentModules,
68             ModuleResourceResolver moduleResourcePathResolver) throws IOException;
69
70     /**
71      * Provided map contains all configuration that was set in pom for code
72      * generator in additionalConfiguration tag.
73      */
74     void setAdditionalConfig(Map<String, String> additionalConfiguration);
75
76     /**
77      * Provided folder is marked as resources and its content will be packaged
78      * in resulting jar. Feel free to add necessary resources.
79      */
80     void setResourceBaseDir(File resourceBaseDir);
81
82     /**
83      * Indicate import resolution mode this code generator requires. Default implementation indicates
84      * {@link ImportResolutionMode#REVISION_EXACT_OR_LATEST}.
85      *
86      * @return Required import resolution mode, null if the code generator does not care.
87      */
88     // FIXME: This is not really extensible, we should be returning a collection of acceptable modes, or have some sort
89     //        of two-step negotiation protocol:
90     //        - Optional<ImportResolutionMode> suggestImportResolutionMode();
91     //        - boolean isImportResolutionModeAcceptable(ImportResolutionMode);
92     //        Let's go with something hacky until we figure out exact requirements.
93     default ImportResolutionMode getImportResolutionMode() {
94         return ImportResolutionMode.REVISION_EXACT_OR_LATEST;
95     }
96 }