Merge branch 'master' of ../controller
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.io.OutputStream;
15 import java.util.Optional;
16 import javax.xml.stream.XMLEventReader;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.transform.OutputKeys;
19 import javax.xml.transform.Transformer;
20 import javax.xml.transform.TransformerException;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.stax.StAXSource;
23 import javax.xml.transform.stream.StreamResult;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.YangConstants;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
29
30 public final class YinExportUtils {
31     private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
32
33     private YinExportUtils() {
34         throw new UnsupportedOperationException("Utility class");
35     }
36
37     /**
38      * Returns well-formed file name of YIN file as defined in RFC6020.
39      *
40      * @param name Module or submodule name
41      * @param revision Revision of module or submodule
42      * @return well-formed file name of YIN file as defined in RFC6020.
43      */
44     public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
45         return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
46     }
47
48     /**
49      * Returns well-formed file name of YIN file as defined in RFC6020.
50      *
51      * @param name name Module or submodule name
52      * @param revision Revision of module or submodule
53      * @return well-formed file name of YIN file as defined in RFC6020.
54      */
55     public static String wellFormedYinName(final String name, final String revision) {
56         if (revision == null) {
57             return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
58         }
59         return requireNonNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
60     }
61
62     /**
63      * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
64      * {@link ModuleEffectiveStatement} trait.
65      *
66      * @param module Module to be exported
67      * @throws IllegalArgumentException if the module is not an ModuleEffectiveStatement or if it declared
68      *                                  representation is not available.
69      * @throws NullPointerException if any of of the parameters is null
70      * @throws XMLStreamException if an input-output error occurs
71      */
72     @Beta
73     public static void writeModuleAsYinText(final Module module, final OutputStream output) throws XMLStreamException {
74         requireNonNull(module);
75         checkArgument(module instanceof ModuleEffectiveStatement, "Module %s is not a ModuleEffectiveStatement",
76             module);
77         final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
78         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(effective), output);
79     }
80
81     /**
82      * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
83      * {@link SubmoduleEffectiveStatement} trait.
84      *
85      * @param parentModule Parent module
86      * @param submodule Submodule to be exported
87      * @throws IllegalArgumentException if the parent module is not a ModuleEffectiveStatement, if the submodule is not
88      *                                  a SubmoduleEffectiveStatement or if its declared representation is not available
89      * @throws NullPointerException if any of of the parameters is null
90      * @throws XMLStreamException if an input-output error occurs
91      */
92     @Beta
93     public static void writeSubmoduleAsYinText(final Module parentModule, final Module submodule,
94             final OutputStream output) throws XMLStreamException {
95         requireNonNull(parentModule);
96         checkArgument(parentModule instanceof ModuleEffectiveStatement, "Parent %s is not a ModuleEffectiveStatement",
97             parentModule);
98         requireNonNull(submodule);
99         checkArgument(submodule instanceof SubmoduleEffectiveStatement,
100             "Submodule %s is not a SubmoduleEffectiveStatement", submodule);
101         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(
102             (ModuleEffectiveStatement) parentModule, (SubmoduleEffectiveStatement)submodule), output);
103     }
104
105     private static void writeReaderToOutput(final XMLEventReader reader, final OutputStream output)
106             throws XMLStreamException {
107         try {
108             final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
109             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
110             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
111             transformer.transform(new StAXSource(reader), new StreamResult(output));
112         } catch (TransformerException e) {
113             throw new XMLStreamException("Failed to stream XML events", e);
114         }
115     }
116 }