970df9e3a07c9a64a6efa6e97512138810b987b7
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YinTextSchemaSource.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.repo.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.io.ByteSource;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.Map.Entry;
19 import org.opendaylight.yangtools.concepts.Delegator;
20 import org.opendaylight.yangtools.yang.common.YangConstants;
21 import org.opendaylight.yangtools.yang.common.YangNames;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
27  */
28 @Beta
29 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
30     private static final Logger LOG = LoggerFactory.getLogger(YinTextSchemaSource.class);
31     private static final String XML_EXTENSION = ".xml";
32
33     private final SourceIdentifier identifier;
34
35     protected YinTextSchemaSource(final SourceIdentifier identifier) {
36         this.identifier = Preconditions.checkNotNull(identifier);
37     }
38
39     public static SourceIdentifier identifierFromFilename(final String name) {
40         final String baseName;
41         if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
42             baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
43         } else if (name.endsWith(XML_EXTENSION)) {
44             // FIXME: BUG-7061: remove this once we do not need it
45             LOG.warn("XML file {} being loaded as YIN", name);
46             baseName = name.substring(0, name.length() - XML_EXTENSION.length());
47         } else {
48             throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
49         }
50
51         final Entry<String, String> parsed = YangNames.parseFilename(baseName);
52         return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Override
59     public final SourceIdentifier getIdentifier() {
60         return identifier;
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public Class<? extends YinTextSchemaSource> getType() {
68         return YinTextSchemaSource.class;
69     }
70
71     @Override
72     public final String toString() {
73         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
74     }
75
76     /**
77      * Add subclass-specific attributes to the output {@link #toString()} output. Since
78      * subclasses are prevented from overriding {@link #toString()} for consistency
79      * reasons, they can add their specific attributes to the resulting string by attaching
80      * attributes to the supplied {@link ToStringHelper}.
81      *
82      * @param toStringHelper ToStringHelper onto the attributes can be added
83      * @return ToStringHelper supplied as input argument.
84      */
85     protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
86
87     /**
88      * Create a new YinTextSchemaSource with a specific source identifier and backed
89      * by ByteSource, which provides the actual InputStreams.
90      *
91      * @param identifier SourceIdentifier of the resulting schema source
92      * @param delegate Backing ByteSource instance
93      * @return A new YinTextSchemaSource
94      */
95     public static YinTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
96         return new DelegatedYinTextSchemaSource(identifier, delegate);
97     }
98
99     private static final class DelegatedYinTextSchemaSource extends YinTextSchemaSource implements Delegator<ByteSource> {
100         private final ByteSource delegate;
101
102         private DelegatedYinTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
103             super(identifier);
104             this.delegate = Preconditions.checkNotNull(delegate);
105         }
106
107         @Override
108         public ByteSource getDelegate() {
109             return delegate;
110         }
111
112         @Override
113         public InputStream openStream() throws IOException {
114             return delegate.openStream();
115         }
116
117         @Override
118         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
119             return toStringHelper.add("delegate", delegate);
120         }
121     }
122 }