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