BUG-7850: SourceIdentifier.getRevision() can return null 48/52248/1
authorRobert Varga <rovarga@cisco.com>
Fri, 24 Feb 2017 13:04:43 +0000 (14:04 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 24 Feb 2017 13:04:43 +0000 (14:04 +0100)
Fix assumptions which are not in-line with API documentation,
i.e. that getRevision() will return a magic string and never null.

Once the users are fixed, getRevision() will return null as per
its defined contract.

Change-Id: Ia7d45325d9f45ff6dc237461fc4ea4d45901675a
Signed-off-by: Robert Varga <rovarga@cisco.com>
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/NetconfRemoteSchemaYangSourceProvider.java

index 04953133a3506b18e1783d545ba736c3a9d74b33..520ec3dd27d1bb889ea1df64908d1fc1c8235623 100644 (file)
@@ -79,13 +79,9 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfDevice.class);
 
-    public static final Function<QName, SourceIdentifier> QNAME_TO_SOURCE_ID_FUNCTION = new Function<QName, SourceIdentifier>() {
-        @Override
-        public SourceIdentifier apply(final QName input) {
-            return RevisionSourceIdentifier
-                    .create(input.getLocalName(), Optional.fromNullable(input.getFormattedRevision()));
-        }
-    };
+    public static final Function<QName, SourceIdentifier> QNAME_TO_SOURCE_ID_FUNCTION =
+            input -> RevisionSourceIdentifier.create(input.getLocalName(),
+                Optional.fromNullable(input.getFormattedRevision()));
 
     protected final RemoteDeviceId id;
     private final boolean reconnectOnSchemasChange;
@@ -168,8 +164,9 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
     private void registerToBaseNetconfStream(final NetconfDeviceRpc deviceRpc, final NetconfDeviceCommunicator listener) {
        // TODO check whether the model describing create subscription is present in schema
         // Perhaps add a default schema context to support create-subscription if the model was not provided (same as what we do for base netconf operations in transformer)
-        final CheckedFuture<DOMRpcResult, DOMRpcException> rpcResultListenableFuture =
-                deviceRpc.invokeRpc(NetconfMessageTransformUtil.toPath(NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME), NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT);
+        final CheckedFuture<DOMRpcResult, DOMRpcException> rpcResultListenableFuture = deviceRpc.invokeRpc(
+            NetconfMessageTransformUtil.toPath(NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME),
+            NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT);
 
         final NotificationHandler.NotificationFilter filter = new NotificationHandler.NotificationFilter() {
             @Override
@@ -534,12 +531,7 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
         }
 
         private Collection<QName> getQNameFromSourceIdentifiers(final Collection<SourceIdentifier> identifiers) {
-            final Collection<QName> qNames = Collections2.transform(identifiers, new Function<SourceIdentifier, QName>() {
-                @Override
-                public QName apply(final SourceIdentifier sourceIdentifier) {
-                    return getQNameFromSourceIdentifier(sourceIdentifier);
-                }
-            });
+            final Collection<QName> qNames = Collections2.transform(identifiers, this::getQNameFromSourceIdentifier);
 
             if (qNames.isEmpty()) {
                 LOG.debug("{}: Unable to map any source identifiers to a capability reported by device : {}", id, identifiers);
@@ -550,16 +542,16 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
         private QName getQNameFromSourceIdentifier(final SourceIdentifier identifier) {
             // Required sources are all required and provided merged in DeviceSourcesResolver
             for (final QName qname : deviceSources.getRequiredSourcesQName()) {
-                if(qname.getLocalName().equals(identifier.getName()) == false) {
+                if (!qname.getLocalName().equals(identifier.getName())) {
                     continue;
                 }
 
-                if(identifier.getRevision().equals(SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION) &&
-                        qname.getRevision() == null) {
-                    return qname;
-                }
-
-                if (qname.getFormattedRevision().equals(identifier.getRevision())) {
+                final String rev = getNullableRev(identifier);
+                if (rev == null) {
+                    if (qname.getRevision() == null) {
+                        return qname;
+                    }
+                } else if (qname.getFormattedRevision().equals(rev)) {
                     return qname;
                 }
             }
@@ -567,5 +559,10 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
             // return null since we cannot find the QName, this capability will be removed from required sources and not reported as unresolved-capability
             return null;
         }
+
+        private String getNullableRev(final SourceIdentifier identifier) {
+            final String rev = identifier.getRevision();
+            return rev == null || SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(rev) ? null : rev;
+        }
     }
 }
index 8f2728b9e1c55c26b19c7e2550351d91999bc15e..9146ff76af8cba2001fdd5873d80647719d32ce4 100644 (file)
@@ -113,7 +113,8 @@ public final class NetconfRemoteSchemaYangSourceProvider implements SchemaSource
         final String moduleName = sourceIdentifier.getName();
 
         // If formatted revision is SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION, we have to omit it from request
-        final String formattedRevision = sourceIdentifier.getRevision().equals(SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION) ? null : sourceIdentifier.getRevision();
+        final String badRev = sourceIdentifier.getRevision();
+        final String formattedRevision = SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(badRev) ? null : badRev;
         final Optional<String> revision = Optional.fromNullable(formattedRevision);
         final NormalizedNode<?, ?> getSchemaRequest = createGetSchemaRequest(moduleName, revision);