Bug 8072: Fix decoding of URLs with external identityref 88/53788/3
authorMarek Gradzki <mgradzki@cisco.com>
Fri, 24 Mar 2017 09:49:52 +0000 (10:49 +0100)
committerMarek Gradzki <mgradzki@cisco.com>
Fri, 24 Mar 2017 14:13:36 +0000 (14:13 +0000)
Mount point schema (rather than global) should be used
to decode identitirefs defined in external modules.

Change-Id: I51f135fe0cd5ceee39d5a2e6f0fc62631cf4437b
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/ControllerContext.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java [new file with mode: 0644]

index 3e3b924cab98cfbb2aea99f3ef88fafe122982e5..5ea8a7468ee2c561ffa83437bdafffcb59b29b20 100644 (file)
@@ -749,7 +749,8 @@ public class ControllerContext implements SchemaContextListener {
         String additionalInfo = "";
         if (decoded == null) {
             if ((typedef instanceof IdentityrefTypeDefinition)) {
-                decoded = toQName(urlDecoded, null);
+                final SchemaContext schemaContext = mountPoint == null ? this.globalSchema : mountPoint.getSchemaContext();
+                decoded = toQName(schemaContext, urlDecoded, null);
                 additionalInfo =
                         "For key which is of type identityref it should be in format module_name:identity_name.";
             }
@@ -791,11 +792,11 @@ public class ControllerContext implements SchemaContextListener {
         return str.substring(idx + 1);
     }
 
-    private QName toQName(final String name, final Date revisionDate) {
+    private QName toQName(final SchemaContext schemaContext, final String name, final Date revisionDate) {
         checkPreconditions();
         final String module = toModuleName(name);
         final String node = toNodeName(name);
-        final Module m = this.globalSchema.findModuleByName(module, revisionDate);
+        final Module m = schemaContext.findModuleByName(module, revisionDate);
         return m == null ? null : QName.create(m.getQNameModule(), node);
     }
 
@@ -804,7 +805,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public RpcDefinition getRpcDefinition(final String name, final Date revisionDate) {
-        final QName validName = toQName(name, revisionDate);
+        final QName validName = toQName(this.globalSchema, name, revisionDate);
         return validName == null ? null : this.qnameToRpc.get().get(validName);
     }
 
diff --git a/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java b/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java
new file mode 100644 (file)
index 0000000..d465a3f
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.common.base.Optional;
+import java.io.FileNotFoundException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
+import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
+import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+
+public class Bug8072Test {
+    private static final String EXTERNAL_MODULE_NAME = "test-module";
+    private static final QName MODULES_QNAME = QName.create("test:module", "2014-01-09", "modules");
+    private static final QName MODULE_QNAME = QName.create("test:module", "2014-01-09", "module");
+    private static final QName NAME_QNAME = QName.create("test:module", "2014-01-09", "name");
+    private static final QName TYPE_QNAME = QName.create("test:module", "2014-01-09", "type");
+    private static final QName MODULE_TYPE_QNAME = QName.create("test:module", "2014-01-09", "module-type");
+
+    private static final ControllerContext controllerContext = ControllerContext.getInstance();
+
+    @BeforeClass
+    public static void init() throws FileNotFoundException, ReactorException {
+        final SchemaContext globalContext = TestUtils.loadSchemaContext("/full-versions/yangs");
+        assertNull(globalContext.findModuleByName(EXTERNAL_MODULE_NAME, null));
+        final Set<Module> allModules = globalContext.getModules();
+        assertNotNull(allModules);
+        controllerContext.setSchemas(globalContext);
+    }
+
+    @Test
+    public void testIdentityRefFromExternalModule() throws FileNotFoundException, ReactorException {
+        initMountService();
+        final InstanceIdentifierContext<?> ctx = controllerContext
+            .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-module:modules/module/test-module:module-type/name");
+
+        final Map<QName, Object> keyValues = new HashMap<>();
+        keyValues.put(NAME_QNAME, "name");
+        keyValues.put(TYPE_QNAME, MODULE_TYPE_QNAME);
+        final YangInstanceIdentifier expectedYII = YangInstanceIdentifier.of(MODULES_QNAME).node(MODULE_QNAME)
+            .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(MODULE_QNAME, keyValues));
+
+        assertEquals(expectedYII, ctx.getInstanceIdentifier());
+    }
+
+    private void initMountService() throws FileNotFoundException, ReactorException {
+        final DOMMountPointService mountService = mock(DOMMountPointService.class);
+        controllerContext.setMountService(mountService);
+        final BrokerFacade brokerFacade = mock(BrokerFacade.class);
+        final RestconfImpl restconfImpl = RestconfImpl.getInstance();
+        restconfImpl.setBroker(brokerFacade);
+        restconfImpl.setControllerContext(controllerContext);
+        final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/full-versions/test-module");
+        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+        when(mountInstance.getSchemaContext()).thenReturn(mountPointContext);
+        when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+    }
+}