BUG-7850: YangLibProvider needs to accept null revisions 47/52247/1
authorRobert Varga <rovarga@cisco.com>
Fri, 24 Feb 2017 12:48:50 +0000 (13:48 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 24 Feb 2017 12:48:50 +0000 (13:48 +0100)
The API contract indicates that SourceIdentifier.getRevision()
may legally return null. The implementation is currently broken,
so make sure we account for both possibilities.

Also share empty revision object for efficiency, as it is constant.

Change-Id: Iacfe5009eea8d5d9531340da27cbb25d3c6c763f
Signed-off-by: Robert Varga <rovarga@cisco.com>
netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java

index 1dcc9bf9a974bb8d1b9ad71eacd87ff483106557..14231dbc0bdfaded2a80621500d156cb56867ae2 100644 (file)
@@ -54,13 +54,9 @@ import org.slf4j.LoggerFactory;
 public class YangLibProvider implements BindingAwareProvider, AutoCloseable, SchemaSourceListener{
     private static final Logger LOG = LoggerFactory.getLogger(YangLibProvider.class);
 
-    private static final Predicate<PotentialSchemaSource<?>> YANG_SCHEMA_SOURCE = new Predicate<PotentialSchemaSource<?>>() {
-        @Override
-        public boolean apply(final PotentialSchemaSource<?> input) {
-            // filter out non yang sources
-            return YangTextSchemaSource.class.isAssignableFrom(input.getRepresentation());
-        }
-    };
+    private static final OptionalRevision NO_REVISION = new OptionalRevision("");
+    private static final Predicate<PotentialSchemaSource<?>> YANG_SCHEMA_SOURCE =
+            input -> YangTextSchemaSource.class.isAssignableFrom(input.getRepresentation());
 
     protected DataBroker dataBroker;
     protected SchemaListenerRegistration schemaListenerRegistration;
@@ -123,12 +119,12 @@ public class YangLibProvider implements BindingAwareProvider, AutoCloseable, Sch
 
         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
             @Override
-            public void onSuccess(@Nullable Void result) {
+            public void onSuccess(@Nullable final Void result) {
                 LOG.debug("Modules state successfully populated with new modules");
             }
 
             @Override
-            public void onFailure(Throwable t) {
+            public void onFailure(final Throwable t) {
                 LOG.warn("Unable to update modules state", t);
             }
         });
@@ -152,28 +148,33 @@ public class YangLibProvider implements BindingAwareProvider, AutoCloseable, Sch
 
         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
             @Override
-            public void onSuccess(@Nullable Void result) {
+            public void onSuccess(@Nullable final Void result) {
                 LOG.debug("Modules state successfully updated.");
             }
 
             @Override
-            public void onFailure(Throwable t) {
+            public void onFailure(final Throwable t) {
                 LOG.warn("Unable to update modules state", t);
             }
         });
     }
 
+
+
     private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) {
-        final String revision = sourceIdentifier.getRevision().equals(SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION)
-                ? "" : sourceIdentifier.getRevision();
-        return new Uri("http://" + bindingAddress + ":" + bindingPort
-                + "/yanglib/schemas/" + sourceIdentifier.getName() + "/" + revision);
+        return new Uri("http://" + bindingAddress + ':' + bindingPort + "/yanglib/schemas/"
+                + sourceIdentifier.getName() + '/' + revString(sourceIdentifier));
+    }
+
+    private static String revString(final SourceIdentifier id) {
+        final String rev = id.getRevision();
+        return rev == null || SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(rev) ? "" : rev;
     }
 
-    private OptionalRevision getRevisionForModule(final SourceIdentifier sourceIdentifier) {
-        return sourceIdentifier.getRevision().equals(SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION)
-                ? new OptionalRevision("")
-                : new OptionalRevision(new RevisionIdentifier(sourceIdentifier.getRevision()));
+    private static OptionalRevision getRevisionForModule(final SourceIdentifier sourceIdentifier) {
+        final String rev = sourceIdentifier.getRevision();
+        return rev == null || SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(rev) ? NO_REVISION
+                : new OptionalRevision(new RevisionIdentifier(rev));
     }
 
     private <T> T getObjectFromBundleContext(final Class<T> type, final String serviceRefName) {