Remote yang text source provider implementation
authorJakub Morvay <jmorvay@cisco.com>
Mon, 2 Nov 2015 08:00:20 +0000 (09:00 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 3 Dec 2015 08:38:32 +0000 (08:38 +0000)
Change-Id: Ie2495b1561ff95508b9a31ffa0aad79ddb59a8c5
Signed-off-by: Jakub Morvay <jmorvay@cisco.com>
java/org/opendaylight/controller/cluster/schema/repository/RemoteYangTextSourceProvider.java [new file with mode: 0644]
java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteSchemaProvider.java [new file with mode: 0644]
java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteYangTextSourceImpl.java [new file with mode: 0644]
java/org/opendaylight/controller/cluster/schema/repository/impl/YangTextSchemaSourceSerializationProxy.java [new file with mode: 0644]

diff --git a/java/org/opendaylight/controller/cluster/schema/repository/RemoteYangTextSourceProvider.java b/java/org/opendaylight/controller/cluster/schema/repository/RemoteYangTextSourceProvider.java
new file mode 100644 (file)
index 0000000..2b5f969
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2015 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/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.schema.repository;
+
+import com.google.common.annotations.Beta;
+import java.util.Set;
+import javax.annotation.Nonnull;
+import org.opendaylight.controller.cluster.schema.repository.impl.YangTextSchemaSourceSerializationProxy;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import scala.concurrent.Future;
+
+/**
+ * A remote yang text source provider provides serializable yang text sources.
+ */
+@Beta
+public interface RemoteYangTextSourceProvider {
+
+    Future<Set<SourceIdentifier>> getProvidedSources();
+
+    Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(@Nonnull SourceIdentifier identifier);
+}
diff --git a/java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteSchemaProvider.java b/java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteSchemaProvider.java
new file mode 100644 (file)
index 0000000..6d6fed7
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2015 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/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.schema.repository.impl;
+
+import akka.dispatch.OnComplete;
+import com.google.common.annotations.Beta;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.controller.cluster.schema.repository.RemoteYangTextSourceProvider;
+import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import scala.concurrent.ExecutionContext;
+import scala.concurrent.Future;
+
+/**
+ * Provides schema sources from {@link RemoteYangTextSourceProvider}.
+ */
+@Beta
+public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchemaSource> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);
+
+    private final RemoteYangTextSourceProvider remoteRepo;
+    private final ExecutionContext executionContext;
+
+    private static final ExceptionMapper<SchemaSourceException> MAPPER = new ExceptionMapper<SchemaSourceException>(
+            "schemaDownload", SchemaSourceException.class) {
+        @Override
+        protected SchemaSourceException newWithCause(final String s, final Throwable throwable) {
+            return new SchemaSourceException(s, throwable);
+        }
+    };
+
+    public RemoteSchemaProvider(RemoteYangTextSourceProvider remoteRepo, ExecutionContext executionContext) {
+        this.remoteRepo = remoteRepo;
+        this.executionContext = executionContext;
+    }
+
+    @Override
+    public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
+        LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());
+
+        Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);
+
+        final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
+        result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
+            @Override
+            public void onComplete(Throwable throwable, YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
+                if(yangTextSchemaSourceSerializationProxy != null) {
+                    res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
+                }
+                if(throwable != null) {
+                    res.setException(throwable);
+                }
+            }
+
+        }, executionContext);
+
+        return Futures.makeChecked(res, MAPPER);
+    }
+}
diff --git a/java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteYangTextSourceImpl.java b/java/org/opendaylight/controller/cluster/schema/repository/impl/RemoteYangTextSourceImpl.java
new file mode 100644 (file)
index 0000000..273aa38
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2015 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/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.schema.repository.impl;
+
+import com.google.common.annotations.Beta;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import java.io.IOException;
+import java.util.Set;
+import org.opendaylight.controller.cluster.schema.repository.RemoteYangTextSourceProvider;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import scala.concurrent.Future;
+import scala.concurrent.Promise;
+
+/**
+ *  Remote schema repository implementation backed by local schema repository.
+ */
+@Beta
+public class RemoteYangTextSourceImpl implements RemoteYangTextSourceProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(RemoteYangTextSourceImpl.class);
+
+    private final SchemaRepository repository;
+    private final Set<SourceIdentifier> providedSources;
+
+    public RemoteYangTextSourceImpl(SchemaRepository repository, Set<SourceIdentifier> providedSources) {
+        this.repository = repository;
+        this.providedSources = providedSources;
+    }
+
+    @Override
+    public Future<Set<SourceIdentifier>> getProvidedSources() {
+        return akka.dispatch.Futures.successful(providedSources);
+    }
+
+    @Override
+    public Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(SourceIdentifier identifier) {
+        LOG.trace("Sending yang schema source for {}", identifier);
+
+        final Promise<YangTextSchemaSourceSerializationProxy> promise = akka.dispatch.Futures.promise();
+        CheckedFuture future = repository.getSchemaSource(identifier, YangTextSchemaSource.class);
+
+        Futures.addCallback(future, new FutureCallback<YangTextSchemaSource>() {
+            @Override
+            public void onSuccess(YangTextSchemaSource result) {
+                try {
+                    promise.success(new YangTextSchemaSourceSerializationProxy(result));
+                } catch (IOException e) {
+                    LOG.warn("Unable to read schema source for {}", result.getIdentifier(), e);
+                    promise.failure(e);
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.warn("Unable to retrieve schema source from repository", t);
+                promise.failure(t);
+            }
+        });
+
+        return promise.future();
+    }
+}
diff --git a/java/org/opendaylight/controller/cluster/schema/repository/impl/YangTextSchemaSourceSerializationProxy.java b/java/org/opendaylight/controller/cluster/schema/repository/impl/YangTextSchemaSourceSerializationProxy.java
new file mode 100644 (file)
index 0000000..8a42837
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2015 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/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.schema.repository.impl;
+
+import com.google.common.annotations.Beta;
+import com.google.common.io.ByteSource;
+import java.io.IOException;
+import java.io.Serializable;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+
+/**
+ * {@link org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource} serialization proxy.
+ */
+@Beta
+public class YangTextSchemaSourceSerializationProxy implements Serializable {
+    private static long serialVersionUID = 1L;
+
+    private final byte[] schemaSource;
+    private final String revision;
+    private final String name;
+
+    public YangTextSchemaSourceSerializationProxy(final YangTextSchemaSource source) throws IOException {
+        this.revision = source.getIdentifier().getRevision();
+        this.name = source.getIdentifier().getName();
+        this.schemaSource = source.read();
+    }
+
+    public YangTextSchemaSource getRepresentation() {
+        return YangTextSchemaSource.delegateForByteSource(new SourceIdentifier(name, revision), ByteSource.wrap(schemaSource));
+    }
+}