Clean up mdsal.monitoring.GetSchema 96/105696/4
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Apr 2023 03:06:13 +0000 (05:06 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Apr 2023 14:57:47 +0000 (16:57 +0200)
Handle non-null prerequisites as such, not allowing being null.

Change-Id: I250d7e1a85c70dd85ec15a93873eec34f5941442
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-monitoring/src/main/java/org/opendaylight/controller/config/yang/netconf/mdsal/monitoring/GetSchema.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlElement.java

index 0db18566061af1c54f9858275f202506933f7f14..c3c9bbf1d60bbdc52f8908d0df1fe74385f4b114 100644 (file)
@@ -7,9 +7,12 @@
  */
 package org.opendaylight.controller.config.yang.netconf.mdsal.monitoring;
 
-import java.util.HashMap;
+import static java.util.Objects.requireNonNull;
+
 import java.util.Map;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.api.xml.XmlElement;
@@ -30,11 +33,11 @@ public final class GetSchema extends AbstractSingletonNetconfOperation {
     private static final String VERSION = "version";
 
     private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
-    private final NetconfMonitoringService cap;
+    private final NetconfMonitoringService monitoring;
 
-    public GetSchema(final String netconfSessionIdForReporting, final NetconfMonitoringService cap) {
+    public GetSchema(final String netconfSessionIdForReporting, final NetconfMonitoringService monitoring) {
         super(netconfSessionIdForReporting);
-        this.cap = cap;
+        this.monitoring = requireNonNull(monitoring);
     }
 
     @Override
@@ -50,33 +53,28 @@ public final class GetSchema extends AbstractSingletonNetconfOperation {
     @Override
     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
             throws DocumentedException {
-        final GetSchemaEntry entry;
-
-        entry = new GetSchemaEntry(xml);
+        final var entry = new GetSchemaEntry(xml);
 
         final String schema;
         try {
-            schema = cap.getSchemaForCapability(entry.identifier, entry.version);
+            schema = monitoring.getSchemaForCapability(entry.identifier, Optional.ofNullable(entry.version));
         } catch (final IllegalStateException e) {
-            final Map<String, String> errorInfo = new HashMap<>();
-            // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
-            errorInfo.put(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage());
             LOG.warn("Rpc error: {}", ErrorTag.OPERATION_FAILED, e);
-            throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION,
-                    ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR, errorInfo);
+            throw new DocumentedException(e.getMessage(), e,
+                ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
+                // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
+                Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
         }
 
-        final Element getSchemaResult;
-        getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
+        final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
         LOG.trace("{} operation successful", GET_SCHEMA);
-
         return getSchemaResult;
     }
 
     private static final class GetSchemaEntry {
-        private final String identifier;
-        private final Optional<String> version;
+        private final @NonNull String identifier;
+        private final @Nullable String version;
 
         GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
             getSchemaElement.checkName(GET_SCHEMA);
@@ -90,12 +88,11 @@ public final class GetSchema extends AbstractSingletonNetconfOperation {
                 throw DocumentedException.wrap(e);
             }
             identifier = identifierElement.getTextContent();
-            final Optional<XmlElement> versionElement = getSchemaElement
-                    .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
+            final var versionElement = getSchemaElement.getOnlyChildElementWithSameNamespaceOptionally(VERSION);
             if (versionElement.isPresent()) {
-                version = Optional.of(versionElement.orElseThrow().getTextContent());
+                version = versionElement.orElseThrow().getTextContent();
             } else {
-                version = Optional.empty();
+                version = null;
             }
         }
     }
index 5fe23bc8260f087bc280d0e0ac7e9a322ad4a38b..a236cc259f4944fd9cda29a325abba3c04fa481d 100644 (file)
@@ -257,7 +257,7 @@ public final class XmlElement {
         return children.get(0);
     }
 
-    public String getTextContent() throws DocumentedException {
+    public @NonNull String getTextContent() throws DocumentedException {
         final var children = element.getChildNodes();
         final var length = children.getLength();
         if (length == 0) {
@@ -268,8 +268,8 @@ public final class XmlElement {
                 return textChild.getTextContent().trim();
             }
         }
-        throw new DocumentedException(getName() + " should contain text.",
-                ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
+        throw new DocumentedException(getName() + " should contain text",
+            ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
     }
 
     public Optional<String> getOnlyTextContentOptionally() {