Simplify YangLibProvider
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / yanglib / impl / YangLibProvider.java
index 7f9873a5766d6378d1241e68ee2ea2ea8e957d64..5235d7df51281a0a424020e446b274f0557c3d37 100644 (file)
@@ -13,33 +13,31 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.Predicate;
 import com.google.common.base.Strings;
 import com.google.common.collect.Iterables;
-import com.google.common.io.ByteStreams;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.io.File;
 import java.io.IOException;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
 import org.opendaylight.yanglib.api.YangLibService;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -73,7 +71,7 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan
             final YangParserFactory parserFactory) {
         this.yanglibConfig = requireNonNull(yanglibConfig);
         this.dataBroker = requireNonNull(dataBroker);
-        this.schemaRepository = new SharedSchemaRepository("yang-library", parserFactory);
+        schemaRepository = new SharedSchemaRepository("yang-library", parserFactory);
     }
 
     @Override
@@ -112,11 +110,13 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan
         final Map<ModuleKey, Module> newModules = new HashMap<>();
 
         for (PotentialSchemaSource<?> potentialYangSource : Iterables.filter(sources, YANG_SCHEMA_SOURCE)) {
-            final YangIdentifier moduleName = new YangIdentifier(potentialYangSource.getSourceIdentifier().getName());
+            final YangIdentifier moduleName =
+                new YangIdentifier(potentialYangSource.getSourceIdentifier().name().getLocalName());
 
             final Module newModule = new ModuleBuilder()
                     .setName(moduleName)
-                    .setRevision(RevisionUtils.fromYangCommon(potentialYangSource.getSourceIdentifier().getRevision()))
+                    .setRevision(LegacyRevisionUtils.fromYangCommon(
+                        Optional.ofNullable(potentialYangSource.getSourceIdentifier().revision())))
                     .setSchema(getUrlForModule(potentialYangSource.getSourceIdentifier()))
                     .build();
 
@@ -155,8 +155,8 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan
 
         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ModulesState.class)
-            .child(Module.class, new ModuleKey(new YangIdentifier(source.getSourceIdentifier().getName()),
-                RevisionUtils.fromYangCommon(source.getSourceIdentifier().getRevision()))));
+            .child(Module.class, new ModuleKey(new YangIdentifier(source.getSourceIdentifier().name().getLocalName()),
+                LegacyRevisionUtils.fromYangCommon(Optional.ofNullable(source.getSourceIdentifier().revision())))));
 
         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
@@ -174,26 +174,32 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan
     @Override
     public String getSchema(final String name, final String revision) {
         LOG.debug("Attempting load for schema source {}:{}", name, revision);
-        final SourceIdentifier sourceId = RevisionSourceIdentifier.create(name,
-            revision.isEmpty() ? null : Revision.of(revision));
+        final SourceIdentifier sourceId = new SourceIdentifier(name, revision.isEmpty() ? null : revision);
 
         final ListenableFuture<YangTextSchemaSource> sourceFuture = schemaRepository.getSchemaSource(sourceId,
             YangTextSchemaSource.class);
 
+        final YangTextSchemaSource source;
         try {
-            final YangTextSchemaSource source = sourceFuture.get();
-            return new String(ByteStreams.toByteArray(source.openStream()), Charset.defaultCharset());
-        } catch (InterruptedException | ExecutionException | IOException e) {
+            source = sourceFuture.get();
+        } catch (InterruptedException | ExecutionException e) {
             throw new IllegalStateException("Unable to get schema " + sourceId, e);
         }
+
+        try {
+            return source.asCharSource(StandardCharsets.UTF_8).read();
+        } catch (IOException e) {
+            throw new IllegalStateException("Unable to read schema " + sourceId, e);
+        }
     }
 
     private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) {
         return new Uri("http://" + yanglibConfig.getBindingAddr() + ':' + yanglibConfig.getBindingPort()
-                + "/yanglib/schemas/" + sourceIdentifier.getName() + '/' + revString(sourceIdentifier));
+                + "/yanglib/schemas/" + sourceIdentifier.name().getLocalName() + '/' + revString(sourceIdentifier));
     }
 
     private static String revString(final SourceIdentifier id) {
-        return id.getRevision().map(Revision::toString).orElse("");
+        final var rev = id.revision();
+        return rev != null ? rev.toString() : "";
     }
 }