Lower codec instantiation scope 30/97230/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Aug 2021 19:59:42 +0000 (21:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 17 Aug 2021 09:50:43 +0000 (11:50 +0200)
Do not instantiate codec until we really need it. This also
short-circuits some of the other setup code.

Change-Id: I1b38951699395f18fe961e3c752a8bd8dde5eac5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit d6785007a5ed83553aef2b01d8585047e0102671)

restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlToPatchBodyReader.java

index 9351411b5e6d5ed1513e5ad8e06d96b2958e1498..4b3a60440d6830f58dfb1755c9613e364657e98f 100644 (file)
@@ -108,17 +108,6 @@ public class XmlToPatchBodyReader extends AbstractToPatchBodyReader {
             final List<Element> values = readValueNodes(element, oper);
             final Element firstValueElement = values != null ? values.get(0) : null;
 
-            // get namespace according to schema node from path context or value
-            final String namespace = firstValueElement == null
-                    ? schemaNode.getQName().getNamespace().toString() : firstValueElement.getNamespaceURI();
-
-            // find module according to namespace
-            final Module module = pathContext.getSchemaContext().findModules(URI.create(namespace)).iterator().next();
-
-            // initialize codec + set default prefix derived from module name
-            final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
-                    pathContext.getSchemaContext(), module.getName());
-
             // find complete path to target and target schema node
             // target can be also empty (only slash)
             YangInstanceIdentifier targetII;
@@ -127,6 +116,18 @@ public class XmlToPatchBodyReader extends AbstractToPatchBodyReader {
                 targetII = pathContext.getInstanceIdentifier();
                 targetNode = pathContext.getSchemaContext();
             } else {
+                // get namespace according to schema node from path context or value
+                final String namespace = firstValueElement == null
+                        ? schemaNode.getQName().getNamespace().toString() : firstValueElement.getNamespaceURI();
+
+                // find module according to namespace
+                final Module module = pathContext.getSchemaContext().findModules(URI.create(namespace)).iterator()
+                    .next();
+
+                // initialize codec + set default prefix derived from module name
+                final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
+                        pathContext.getSchemaContext(), module.getName());
+
                 targetII = codec.deserialize(codec.serialize(pathContext.getInstanceIdentifier())
                         .concat(prepareNonCondXpath(schemaNode, target.replaceFirst("/", ""), firstValueElement,
                                 namespace,