BUG-4688: switch revisions from Date to Revision
[yangtools.git] / yang / yang-model-export / src / main / java / org / opendaylight / yangtools / yang / model / export / YinExportUtils.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.yang.model.export;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.BiMap;
12 import com.google.common.collect.HashBiMap;
13 import java.io.OutputStream;
14 import java.net.URI;
15 import java.util.Map;
16 import java.util.Optional;
17 import javax.xml.stream.XMLOutputFactory;
18 import javax.xml.stream.XMLStreamException;
19 import javax.xml.stream.XMLStreamWriter;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public final class YinExportUtils {
27
28     private YinExportUtils() {
29         throw new UnsupportedOperationException("Utility class");
30     }
31
32     /**
33      *
34      * Returns well-formed file name of YIN file as defined in RFC6020.
35      *
36      * @param name
37      *            Module or submodule name
38      * @param revision
39      *            Revision of module or submodule
40      * @return well-formed file name of YIN file as defined in RFC6020.
41      */
42     public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
43         return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
44     }
45
46     /**
47      *
48      * Returns well-formed file name of YIN file as defined in RFC6020.
49      *
50      * @param name
51      *            name Module or submodule name
52      * @param revision
53      *            Revision of module or submodule
54      * @return well-formed file name of YIN file as defined in RFC6020.
55      */
56     public static String wellFormedYinName(final String name, final String revision) {
57         if (revision == null) {
58             return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
59         }
60         return Preconditions.checkNotNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
61     }
62
63     /**
64      * Writes YIN representation of supplied module to specified output stream.
65      *
66      * @param ctx
67      *            Schema Context which contains module and extension definitions
68      *            to be used during export of model.
69      * @param module
70      *            Module to be exported.
71      * @param str
72      *            Output stream to which YIN representation of model will be
73      *            written.
74      * @throws XMLStreamException
75      */
76     public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str)
77             throws XMLStreamException {
78         writeModuleToOutputStream(ctx, module, str, false);
79     }
80
81     /**
82      * Writes YIN representation of supplied module to specified output stream.
83      *
84      * @param ctx
85      *            Schema Context which contains module and extension definitions
86      *            to be used during export of model.
87      * @param module
88      *            Module to be exported.
89      * @param str
90      *            Output stream to which YIN representation of model will be
91      *            written.
92      * @param emitInstantiated
93      *            Option to emit also instantiated statements (e.g. statements
94      *            added by uses or augment)
95      * @throws XMLStreamException
96      */
97     public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str,
98             final boolean emitInstantiated) throws XMLStreamException {
99         final XMLOutputFactory factory = XMLOutputFactory.newFactory();
100         final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(str);
101         writeModuleToOutputStream(ctx, module, xmlStreamWriter, emitInstantiated);
102         xmlStreamWriter.flush();
103     }
104
105     private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module,
106             final XMLStreamWriter xmlStreamWriter, final boolean emitInstantiated) {
107         final URI moduleNs = module.getNamespace();
108         final Map<String, URI> prefixToNs = prefixToNamespace(ctx, module);
109         final StatementTextWriter yinWriter = SingleModuleYinStatementWriter.create(xmlStreamWriter, moduleNs,
110                 prefixToNs);
111         SchemaContextEmitter.writeToStatementWriter(module, ctx, yinWriter, emitInstantiated);
112     }
113
114     private static Map<String, URI> prefixToNamespace(final SchemaContext ctx, final Module module) {
115         final BiMap<String, URI> prefixMap = HashBiMap.create(module.getImports().size() + 1);
116         prefixMap.put(module.getPrefix(), module.getNamespace());
117         for (final ModuleImport imp : module.getImports()) {
118             final String prefix = imp.getPrefix();
119             final URI namespace = getModuleNamespace(ctx, imp.getModuleName());
120             prefixMap.put(prefix, namespace);
121         }
122         return prefixMap;
123     }
124
125     private static URI getModuleNamespace(final SchemaContext ctx, final String moduleName) {
126         for (final Module module : ctx.getModules()) {
127             if (moduleName.equals(module.getName())) {
128                 return module.getNamespace();
129             }
130         }
131         throw new IllegalArgumentException("Module " + moduleName + "does not exists in provided schema context");
132     }
133
134 }