Lower codec instantiation scope 08/97208/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Aug 2021 19:59:42 +0000 (21:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Aug 2021 20:02:57 +0000 (22:02 +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>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java

index e8baaf492bb1b6d31d111b748101d2745f85a432..927ca67d06df51485e6b6af646a7d4238af6de14 100644 (file)
@@ -108,18 +108,6 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader {
             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(XMLNamespace.of(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;
@@ -128,6 +116,18 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader {
                 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(XMLNamespace.of(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,