ed96d9d7185936d825aad1071a8eec6d647354a2
[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 javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.concepts.Delegator;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.common.YangNames;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
28  */
29 @Beta
30 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
31     private static final Logger LOG = LoggerFactory.getLogger(YinTextSchemaSource.class);
32     private static final String XML_EXTENSION = ".xml";
33
34     private final SourceIdentifier identifier;
35
36     protected YinTextSchemaSource(final SourceIdentifier identifier) {
37         this.identifier = Preconditions.checkNotNull(identifier);
38     }
39
40     public static SourceIdentifier identifierFromFilename(final String name) {
41         final String baseName;
42         if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
43             baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
44         } else if (name.endsWith(XML_EXTENSION)) {
45             // FIXME: BUG-7061: remove this once we do not need it
46             LOG.warn("XML file {} being loaded as YIN", name);
47             baseName = name.substring(0, name.length() - XML_EXTENSION.length());
48         } else {
49             throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
50         }
51
52         final Entry<String, String> parsed = YangNames.parseFilename(baseName);
53         return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
54     }
55
56     /**
57      * {@inheritDoc}
58      */
59     @Override
60     public final SourceIdentifier getIdentifier() {
61         return identifier;
62     }
63
64     /**
65      * {@inheritDoc}
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(final 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, final ByteSource delegate) {
98         return new DelegatedYinTextSchemaSource(identifier, delegate);
99     }
100
101     private static final class DelegatedYinTextSchemaSource extends YinTextSchemaSource implements Delegator<ByteSource> {
102         private final ByteSource delegate;
103
104         private DelegatedYinTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
105             super(identifier);
106             this.delegate = Preconditions.checkNotNull(delegate);
107         }
108
109         @Override
110         public ByteSource getDelegate() {
111             return delegate;
112         }
113
114         @Override
115         public InputStream openStream() throws IOException {
116             return delegate.openStream();
117         }
118
119         @Override
120         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
121             return toStringHelper.add("delegate", delegate);
122         }
123     }
124 }