Convert yanglib to new web API
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / yanglib / impl / YangLibServiceImpl.java
index 5f66daea8212fdddc63ddb3284957e4f03274f3f..d856ac7bdb5f62b083602d4a449d8cbbea4a46f6 100644 (file)
@@ -8,15 +8,16 @@
 
 package org.opendaylight.yanglib.impl;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.io.ByteStreams;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.concurrent.ExecutionException;
 import org.opendaylight.yanglib.api.YangLibService;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
-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.slf4j.Logger;
@@ -28,14 +29,9 @@ import org.slf4j.LoggerFactory;
 public class YangLibServiceImpl implements YangLibService {
     private static final Logger LOG = LoggerFactory.getLogger(YangLibServiceImpl.class);
 
-    private volatile SchemaRepository schemaRepository;
+    private final SchemaRepository schemaRepository;
 
-    public YangLibServiceImpl() {
-
-    }
-
-    public void setSchemaRepository(final SchemaRepository schemaRepository) {
-        LOG.debug("Setting schema repository {}", schemaRepository);
+    public YangLibServiceImpl(final SchemaRepository schemaRepository) {
         this.schemaRepository = schemaRepository;
     }
 
@@ -43,17 +39,17 @@ public class YangLibServiceImpl implements YangLibService {
     public String getSchema(final String name, final String revision) {
         Preconditions.checkNotNull(schemaRepository, "Schema repository is not initialized");
         LOG.debug("Attempting load for schema source {}:{}", name, revision);
-        final SourceIdentifier sourceId =
-                RevisionSourceIdentifier.create(name, Optional.fromNullable(revision.equals("") ? null : revision));
+        final SourceIdentifier sourceId = RevisionSourceIdentifier.create(name,
+            revision.isEmpty() ? null : Revision.of(revision));
 
-        final CheckedFuture<YangTextSchemaSource, SchemaSourceException> sourceFuture =
-                schemaRepository.getSchemaSource(sourceId, YangTextSchemaSource.class);
+        final ListenableFuture<YangTextSchemaSource> sourceFuture = schemaRepository.getSchemaSource(sourceId,
+            YangTextSchemaSource.class);
 
         try {
-            final YangTextSchemaSource source = sourceFuture.checkedGet();
-            return new String(ByteStreams.toByteArray(source.openStream()));
-        } catch (SchemaSourceException | IOException e) {
-            throw new IllegalStateException("Unable to get schema" + sourceId, e);
+            final YangTextSchemaSource source = sourceFuture.get();
+            return new String(ByteStreams.toByteArray(source.openStream()), Charset.defaultCharset());
+        } catch (InterruptedException | ExecutionException | IOException e) {
+            throw new IllegalStateException("Unable to get schema " + sourceId, e);
         }
     }
 }