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