Merge "Bump to odlparent 3.1.0 and yangtools 2.0.3"
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / yanglib / impl / YangLibServiceImpl.java
index f867971aea5051b9ba984025c48e687b2c6fe434..949d8f577393c35baaf747ae4a1b51266d3aa0fb 100644 (file)
@@ -8,52 +8,53 @@
 
 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;
 import org.slf4j.LoggerFactory;
 
 /**
- * Provides schema sources from yang library
+ * Provides schema sources from yang library.
  */
 public class YangLibServiceImpl implements YangLibService {
     private static final Logger LOG = LoggerFactory.getLogger(YangLibServiceImpl.class);
 
-    private SchemaRepository schemaRepository;
+    private static volatile SchemaRepository schemaRepository;
 
     public YangLibServiceImpl() {
 
     }
 
-    public void setSchemaRepository(final SchemaRepository schemaRepository) {
+    public static void setSchemaRepository(final SchemaRepository schemaRepository) {
         LOG.debug("Setting schema repository {}", schemaRepository);
-        this.schemaRepository = schemaRepository;
+        YangLibServiceImpl.schemaRepository = schemaRepository;
     }
 
     @Override
     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);
         }
     }
 }