Use instanceof pattern in netconf-impl
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / osgi / NetconfOperationRouterImpl.java
index 8a9acf07ee2fe98c406f857beb031a19090e5d56..b1ab4b26de966af71f94fdeb8019d8eed1ebf04e 100644 (file)
@@ -31,6 +31,8 @@ import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -68,17 +70,31 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
             final String messageAsString = XmlUtil.toString(message);
             LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
 
-            final DocumentedException.ErrorTag tag;
-            if (e instanceof IllegalArgumentException) {
-                tag = DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED;
-            } else {
-                tag = DocumentedException.ErrorTag.OPERATION_FAILED;
-            }
+            final ErrorTag tag = e instanceof IllegalArgumentException ? ErrorTag.OPERATION_NOT_SUPPORTED
+                : ErrorTag.OPERATION_FAILED;
 
             throw new DocumentedException(
-                    String.format("Unable to handle rpc %s on session %s", messageAsString, session),
-                    e, DocumentedException.ErrorType.APPLICATION, tag, ErrorSeverity.ERROR,
-                    Map.of(tag.toString(), e.getMessage()));
+                    String.format("Unable to handle rpc %s on session %s", messageAsString, session), e,
+                    ErrorType.APPLICATION, tag, ErrorSeverity.ERROR,
+                    // FIXME: i.e. in what namespace are we providing these tags? why is this not just:
+                    //
+                    // <java-throwable xmlns="org.opendaylight.something">
+                    //   <message>e.getMessage()</message>
+                    // </java-throwable>
+                    //
+                    // for each place where we are mapping Exception.getMessage() ? We probably do not want to propagate
+                    // stack traces out, but suppressed exceptions and causal list might be interesting:
+                    //
+                    // <java-throwable xmlns="org.opendaylight.something">
+                    //   <message>reported exception</message>
+                    // </java-throwable>
+                    // <java-throwable xmlns="org.opendaylight.something">
+                    //   <message>cause of reported exception</message>
+                    // </java-throwable>
+                    // <java-throwable xmlns="org.opendaylight.something">
+                    //   <message>cause of cause of reported exception</message>
+                    // </java-throwable>
+                    Map.of(tag.elementBody(), e.getMessage()));
         } catch (final RuntimeException e) {
             throw handleUnexpectedEx("sort", e);
         }
@@ -98,10 +114,9 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
     private static DocumentedException handleUnexpectedEx(final String op, final Exception exception) {
         LOG.error("Unexpected exception during netconf operation {}", op, exception);
         return new DocumentedException("Unexpected error",
-                DocumentedException.ErrorType.APPLICATION,
-                DocumentedException.ErrorTag.OPERATION_FAILED,
-                ErrorSeverity.ERROR,
-                Map.of(ErrorSeverity.ERROR.toString(), exception.toString()));
+                ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
+                // FIXME: i.e. <error>exception.toString()</error>? That looks wrong on a few levels.
+                Map.of(ErrorSeverity.ERROR.elementBody(), exception.toString()));
     }
 
     private static Document executeOperationWithHighestPriority(final Document message,
@@ -135,11 +150,11 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
 
         for (final NetconfOperation netconfOperation : allNetconfOperations) {
             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
-            if (netconfOperation instanceof DefaultNetconfOperation) {
-                ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
+            if (netconfOperation instanceof DefaultNetconfOperation defaultOperation) {
+                defaultOperation.setNetconfSession(session);
             }
-            if (netconfOperation instanceof SessionAwareNetconfOperation) {
-                ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
+            if (netconfOperation instanceof SessionAwareNetconfOperation sessionAwareOperation) {
+                sessionAwareOperation.setSession(session);
             }
             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {