BUG-997: Introduce SchemaSource respository and related APIs 67/9367/2
authorRobert Varga <rovarga@cisco.com>
Sat, 19 Jul 2014 09:05:53 +0000 (11:05 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 28 Jul 2014 07:06:36 +0000 (09:06 +0200)
Change-Id: I9cddf1ca0d1df4360b6110663c0c5eb89bd8f51c
Signed-off-by: Robert Varga <rovarga@cisco.com>
15 files changed:
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/AcceptingSchemaSourceFilter.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaContextFactory.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaRepository.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaResolutionException.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceFilter.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceRepresentation.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SourceIdentifier.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YinSchemaSource.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceProvider.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformationException.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaTransformerRegistration.java [new file with mode: 0644]

diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/AcceptingSchemaSourceFilter.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/AcceptingSchemaSourceFilter.java
new file mode 100644 (file)
index 0000000..e101d9d
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+
+/**
+ * A {@link SchemaSourceFilter} which accepts any schema source it is presented with.
+ */
+public final class AcceptingSchemaSourceFilter implements SchemaSourceFilter {
+    private static final AcceptingSchemaSourceFilter INSTANCE = new AcceptingSchemaSourceFilter();
+
+    private final Iterable<Class<? extends SchemaSourceRepresentation>> representations;
+
+    private AcceptingSchemaSourceFilter() {
+        final Builder<Class<? extends SchemaSourceRepresentation>> b = ImmutableList.builder();
+        b.add(SchemaSourceRepresentation.class);
+        representations = b.build();
+    }
+
+    /**
+     * Return the singleton instance of this filter.
+     *
+     * @return Singleton shared instance.
+     */
+    public static final AcceptingSchemaSourceFilter getSingletonInstance() {
+        return INSTANCE;
+    }
+
+    @Override
+    public Iterable<Class<? extends SchemaSourceRepresentation>> supportedRepresentations() {
+        return representations;
+    }
+
+    @Override
+    public ListenableFuture<Boolean> apply(final SchemaSourceRepresentation schemaSource) {
+        return Futures.immediateFuture(Boolean.TRUE);
+    }
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaContextFactory.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaContextFactory.java
new file mode 100644 (file)
index 0000000..8869c9e
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.util.concurrent.CheckedFuture;
+
+import java.util.Collection;
+
+import javax.annotation.Nonnull;
+
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * An asynchronous factory for building {@link SchemaContext} instances
+ * based on a specification of what {@link SourceIdentifier}s are required
+ * and dynamic recursive resolution.
+ */
+public interface SchemaContextFactory {
+    /**
+     * Create a new schema context containing specified sources, pulling in
+     * any dependencies they may have.
+     *
+     * @param requiredSources a collection of sources which are required to
+     *                        be present
+     * @return A checked future, which will produce a schema context, or
+     *         fail with an explanation why the creation of the schema context
+     *         failed.
+     */
+    CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(@Nonnull Collection<SourceIdentifier> requiredSources);
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaRepository.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaRepository.java
new file mode 100644 (file)
index 0000000..807bbf0
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import javax.annotation.Nonnull;
+
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Interface exposed by repository implementations. A schema repository is a logically
+ * centralized place for model storage and creation of {@link SchemaContext} instances.
+ */
+public interface SchemaRepository {
+    /**
+     * Instantiate a new {@link SchemaContextFactory}, which will filter available schema
+     * sources using the provided filter.
+     *
+     * @param filter Filter which acts as the gating function before a schema source is
+     *               considered by the factory for inclusion in the {@link SchemaContext}
+     *               it produces.
+     * @return A new schema context factory.
+     */
+    SchemaContextFactory createSchemaContextFactory(@Nonnull SchemaSourceFilter filter);
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaResolutionException.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaResolutionException.java
new file mode 100644 (file)
index 0000000..b47e366
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+
+/**
+ * Exception thrown when a Schema Source fails to resolve.
+ */
+public class SchemaResolutionException extends Exception {
+    private static final long serialVersionUID = 1L;
+    private final Map<SourceIdentifier, Throwable> unresolvedSources;
+
+    public SchemaResolutionException(final @Nonnull String message) {
+        this(message, (Throwable)null);
+    }
+
+    public SchemaResolutionException(final @Nonnull String message, final Throwable cause) {
+        this(message, cause, ImmutableMap.<SourceIdentifier, Exception>of());
+    }
+
+    public SchemaResolutionException(final @Nonnull String message, final @Nonnull Map<SourceIdentifier, ? extends Throwable> unresolvedSources) {
+        super(Preconditions.checkNotNull(message));
+        this.unresolvedSources = ImmutableMap.copyOf(unresolvedSources);
+    }
+
+    public SchemaResolutionException(final @Nonnull String message, final Throwable cause, @Nonnull final Map<SourceIdentifier, ? extends Throwable> unresolvedSources) {
+        super(message, cause);
+        this.unresolvedSources = ImmutableMap.copyOf(unresolvedSources);
+    }
+
+    /**
+     * Return the list of sources which failed to resolve along with reasons
+     * why they were not resolved.
+     *
+     * @return Source/reason map.
+     */
+    public final Map<SourceIdentifier, Throwable> getUnresolvedSources() {
+        return unresolvedSources;
+    }
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(Objects.toStringHelper(this).add("unresolvedSources", unresolvedSources)).toString();
+    }
+
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return toStringHelper;
+    }
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceFilter.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceFilter.java
new file mode 100644 (file)
index 0000000..188f83c
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.util.concurrent.ListenableFuture;
+
+public interface SchemaSourceFilter {
+    Iterable<Class<? extends SchemaSourceRepresentation>> supportedRepresentations();
+    ListenableFuture<Boolean> apply(SchemaSourceRepresentation schemaSource);
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceRepresentation.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaSourceRepresentation.java
new file mode 100644 (file)
index 0000000..b1261a9
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.concepts.Immutable;
+
+/**
+ * Common interface for schema source representations.
+ *
+ * A schema source is an atomic piece of the overall schema context. In YANG terms,
+ * a schema source is semantically equivalent to a single YANG text file, be it a
+ * module or a submodule.
+ *
+ * A schema source can exist in various forms, which we call representations. Again,
+ * in YANG terms, each representation is semantically equivalent, but from
+ * implementation perspective certain operations on a schema source may require it
+ * to be first transformed into a particular representation before they can be
+ * applied. Such transformations are affected via instances of
+ * {@link SchemaSourceTransformation}.
+ *
+ * Typical examples of a schema source representation include:
+ * <ul>
+ * <li>a {@link java.lang.String} - textual representation of source code
+ * <li>a {@link java.io.InputStream} - input stream containing source code
+ * <li>a {@link com.google.common.io.ByteSource} - source for input streams
+ * containing source code
+ * <li>Parsed abstract syntax tree (AST), which is the result of a syntactic parser
+ *
+ * Implementations of this interface expected to comply with the {@link Immutable}
+ * contract.
+ */
+public interface SchemaSourceRepresentation extends Identifiable<SourceIdentifier>, Immutable {
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    SourceIdentifier getIdentifier();
+
+    /**
+     * Return the concrete representation type.
+     *
+     * @return The type of representation.
+     */
+    Class<? extends SchemaSourceRepresentation> getType();
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SourceIdentifier.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SourceIdentifier.java
new file mode 100644 (file)
index 0000000..cd3a0fb
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
+import org.opendaylight.yangtools.concepts.Identifier;
+import org.opendaylight.yangtools.concepts.Immutable;
+
+/**
+ *
+ * YANG Schema source identifier
+ *
+ * Simple transfer object represents identifier of source for YANG schema (module or submodule),
+ * which consists of
+ * <ul>
+ * <li>YANG schema name ({@link #getName()}
+ * <li>Module revision (optional) ({link {@link #getRevision()})
+ * </ul>
+ *
+ * Source identifier is designated to be carry only necessary information
+ * to look-up YANG model source and to be used by {@link AdvancedSchemaSourceProvider}
+ * and similar.
+ *
+ * <b>Note:</b>On source retrieval layer it is impossible to distinguish
+ * between YANG module and/or submodule unless source is present.
+ *
+ * <p>
+ * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2 and
+ * http://tools.ietf.org/html/rfc6022#section-3.1 ).
+ *
+ *
+ */
+public final class SourceIdentifier implements Identifier, Immutable {
+    private static final long serialVersionUID = 1L;
+    private final String revision;
+    private final String name;
+
+    /**
+     *
+     * Creates new YANG Schema source identifier.
+     *
+     * @param name Name of schema
+     * @param formattedRevision Revision of source in format YYYY-mm-dd
+     */
+    public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
+        super();
+        this.name = Preconditions.checkNotNull(name);
+        this.revision = formattedRevision.orNull();
+    }
+
+    /**
+     * Returns model name
+     *
+     * @return model name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Returns revision of source or null if revision was not supplied.
+     *
+     * @return revision of source or null if revision was not supplied.
+     */
+    public String getRevision() {
+        return revision;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        SourceIdentifier other = (SourceIdentifier) obj;
+        if (name == null) {
+            if (other.name != null) {
+                return false;
+            }
+        } else if (!name.equals(other.name)) {
+            return false;
+        }
+        if (revision == null) {
+            if (other.revision != null) {
+                return false;
+            }
+        } else if (!revision.equals(other.revision)) {
+            return false;
+        }
+        return true;
+    }
+
+    public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
+        return new SourceIdentifier(moduleName, revision);
+    }
+
+    /**
+     * Returns filename for this YANG module as specified in RFC 6020.
+     *
+     * Returns filename in format
+     * <code>name ['@' revision] '.yang'</code>
+     * <p>
+     * Where revision is  date in format YYYY-mm-dd.
+     * <p>
+     *
+     * @see http://tools.ietf.org/html/rfc6020#section-5.2
+     *
+     * @return Filename for this source identifier.
+     */
+    public String toYangFilename() {
+        return toYangFileName(name, Optional.fromNullable(revision));
+    }
+
+    @Override
+    public String toString() {
+        return "SourceIdentifier [name=" + name + "@" + revision + "]";
+    }
+
+    /**
+     * Returns filename for this YANG module as specified in RFC 6020.
+     *
+     * Returns filename in format
+     * <code>moduleName ['@' revision] '.yang'</code>
+     *
+     * Where Where revision-date is in format YYYY-mm-dd.
+     *
+     * <p>
+     * See
+     * http://tools.ietf.org/html/rfc6020#section-5.2
+     *
+     * @return Filename for this source identifier.
+     */
+    public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
+        StringBuilder filename = new StringBuilder(moduleName);
+        if (revision.isPresent()) {
+            filename.append('@');
+            filename.append(revision.get());
+        }
+        filename.append(".yang");
+        return filename.toString();
+    }
+
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java
new file mode 100644 (file)
index 0000000..be52a90
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.Preconditions;
+import com.google.common.io.ByteSource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.opendaylight.yangtools.concepts.Delegator;
+
+/**
+ * YANG text schema source representation. Exposes an RFC6020 text representation
+ * as an {@link InputStream}.
+ */
+public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
+    private final SourceIdentifier identifier;
+
+    protected YangTextSchemaSource(final SourceIdentifier identifier) {
+        this.identifier = Preconditions.checkNotNull(identifier);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public final SourceIdentifier getIdentifier() {
+        return identifier;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Class<? extends YangTextSchemaSource> getType() {
+        return YangTextSchemaSource.class;
+    }
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(Objects.toStringHelper(this).add("identifier", identifier)).toString();
+    }
+
+    /**
+     * Add subclass-specific attributes to the output {@link #toString()} output. Since
+     * subclasses are prevented from overriding {@link #toString()} for consistency
+     * reasons, they can add their specific attributes to the resulting string by attaching
+     * attributes to the supplied {@link ToStringHelper}.
+     *
+     * @param toStringHelper ToStringHelper onto the attributes can be added
+     * @return ToStringHelper supplied as input argument.
+     */
+    protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
+
+    /**
+     * Create a new YangTextSchemaSource with a specific source identifier and backed
+     * by ByteSource, which provides the actual InputStreams.
+     *
+     * @param identifier SourceIdentifier of the resulting schema source
+     * @param delegate Backing ByteSource instance
+     * @return A new YangTextSchemaSource
+     */
+    public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
+        return new DelegatedYangTextSchemaSource(identifier, delegate);
+    }
+
+    private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
+        private final ByteSource delegate;
+
+        private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
+            super(identifier);
+            this.delegate = Preconditions.checkNotNull(delegate);
+        }
+
+        @Override
+        public final ByteSource getDelegate() {
+            return delegate;
+        }
+
+        @Override
+        public InputStream openStream() throws IOException {
+            return delegate.openStream();
+        }
+
+        @Override
+        protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+            return toStringHelper.add("delegate", delegate);
+        }
+    }
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YinSchemaSource.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YinSchemaSource.java
new file mode 100644 (file)
index 0000000..b67aae6
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.api;
+
+import org.w3c.dom.Document;
+
+/**
+ * Yin schema source representation. Exposes an RFC6020 YIN XML representation
+ * as an W3C {@link Document}.
+ */
+public interface YinSchemaSource extends SchemaSourceRepresentation {
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    SourceIdentifier getIdentifier();
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    Class<? extends YinSchemaSource> getType();
+
+    /**
+     * Return schema source as a Yin-compliant Document.
+     *
+     * @return W3C DOM document.
+     */
+    Document getYinDocument();
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceProvider.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceProvider.java
new file mode 100644 (file)
index 0000000..a0a141b
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
+
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+
+/**
+ * Schema source provider implementations take care of resolving a {@link SourceIdentifier}
+ * into a particular representation of the schema source. Examples of resolution include
+ * fetching the source from an external source, opening a classpath resource, or similar.
+ *
+ * @param <T> Schema source representation type provided by this implementation
+ */
+@Beta
+public interface SchemaSourceProvider<T extends SchemaSourceRepresentation> {
+    /**
+     * Returns a representation a for supplied YANG source identifier. The resolution
+     * criteria are as follows:
+     *
+     * <ul>
+     * <li> If the source identifier specifies a revision, this method returns either
+     * a representation of that particular revision, or report the identifier as absent
+     * by returning {@link Optional#absent()}.
+     * <li> If the source identifier does not specify a revision, this method returns
+     * the newest available revision, or {@link Optional#absent()}.
+     *
+     * In either case the returned representation is required to report a non-null
+     * revision in the {@link SourceIdentifier} returned from
+     * {@link SchemaSourceRepresentation#getIdentifier()}.
+     *
+     * Implementations are not required to provide constant behavior in time, notably
+     * this different invocation of this method may produce different results.
+     *
+     * @param sourceIdentifier source identifier
+     * @return source representation if supplied YANG module is available
+     *         {@link Optional#absent()} otherwise.
+     */
+    ListenableFuture<Optional<T>> getSource(SourceIdentifier sourceIdentifier);
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java
new file mode 100644 (file)
index 0000000..6cdb6a3
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+
+public interface SchemaSourceRegistration extends Registration<SourceIdentifier> {
+    @Override
+    void close();
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java
new file mode 100644 (file)
index 0000000..f9acf3d
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+
+/**
+ * Registry of all potentially available schema sources. Processes capable of
+ * dynamic schema discovery, such as OSGi registry scanners, NETCONF clients
+ * (with NETCONF monitoring extension) and similar can register
+ * {@link SchemaSourceProvider} instances which would then acquire the schema
+ * source.
+ */
+public interface SchemaSourceRegistry {
+    /**
+     * Register a new schema source which is potentially available from a provider.
+     * A registration does not guarantee that a subsequent call to
+     * {@link SchemaSourceProvider#getSource(SourceIdentifier)} will succeed.
+     *
+     * @param identifier Schema source identifier
+     * @param provider Resolver which can potentially resolve the identifier
+     * @param representation Schema source representation which the source may
+     *                       be available.
+     * @return A registration handle. Invoking {@link SchemaSourceRegistration#close()}
+     *         will cancel the registration.
+     */
+    <T extends SchemaSourceRepresentation> SchemaSourceRegistration registerSchemaSource(
+            SourceIdentifier identifier, SchemaSourceProvider<? super T> provider, Class<T> representation);
+
+    /**
+     * Register a schema transformer. The registry can invoke it to transform between
+     * the various schema source formats.
+     *
+     * @param transformer Schema source transformer
+     * @return A registration handle. Invoking {@link SchemaTransformerRegistration#close()}
+     *         will cancel the registration.
+     */
+    SchemaTransformerRegistration registerSchemaSourceTransformer(SchemaSourceTransformer<?, ?> transformer);
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformationException.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformationException.java
new file mode 100644 (file)
index 0000000..b5c08de
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+/**
+ * Exception thrown when a failure to translate a schema source between
+ * representations.
+ */
+public class SchemaSourceTransformationException extends Exception {
+    private static final long serialVersionUID = 1L;
+
+    public SchemaSourceTransformationException(final String message) {
+        super(message);
+    }
+
+    public SchemaSourceTransformationException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java
new file mode 100644 (file)
index 0000000..13d309e
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+import com.google.common.util.concurrent.CheckedFuture;
+
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+
+/**
+ * An schema source representation transformation service. An instance can create
+ * some output schema source representation based on some input source representation.
+ *
+ * @param <I> Input {@link SchemaSourceRepresentation}
+ * @param <O> Output {@link SchemaSourceRepresentation}
+ */
+public interface SchemaSourceTransformer<I extends SchemaSourceRepresentation, O extends SchemaSourceRepresentation> {
+    /**
+     * Return the {@link SchemaSourceRepresentation} which this transformer
+     * accepts on its input.
+     *
+     * @return The input source representation type.
+     */
+    Class<I> getInputRepresentation();
+
+    /**
+     * Return the {@link SchemeSourceRepresentation} which this transformer
+     * produces on its output.
+     *
+     * @return The output source representation type.
+     */
+    Class<O> getOutputRepresentation();
+
+    /**
+     * Transform a schema source representation from its input form to
+     * the transformers output form.
+     *
+     * @param source Schema source in its source representation
+     * @return A future which produces the output schema source representation.
+     */
+    CheckedFuture<O, SchemaSourceTransformationException> transformSchemaSource(I source);
+
+    /**
+     * Return the relative cost of performing the transformation. When in doubt,
+     * return 1.
+     *
+     * @return Relative cost.
+     */
+    int getCost();
+}
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaTransformerRegistration.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaTransformerRegistration.java
new file mode 100644 (file)
index 0000000..e64deee
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
+package org.opendaylight.yangtools.yang.model.repo.spi;
+
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
+
+public interface SchemaTransformerRegistration extends ObjectRegistration<SchemaSourceTransformer<?, ?>> {
+    @Override
+    void close();
+}