2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.restconf.impl.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import com.google.common.base.Optional;
19 import java.io.FileNotFoundException;
20 import java.util.HashMap;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 * Checks if restconf is able to deserialize external leafref.
40 * @see <a href="https://jira.opendaylight.org/browse/NETCONF-505">NETCONF-505</a>
42 public class Netconf505Test {
43 private static final String EXTERNAL_MODULE_NAME = "leafref-module";
44 private static final QName CONT_QNAME = QName.create("leafref:module", "2014-04-17", "cont");
45 private static final QName LIST_QNAME = QName.create("leafref:module", "2014-04-17", "lst-with-lfref-key");
46 private static final QName KEY_QNAME = QName.create("leafref:module", "2014-04-17", "lfref-key");
48 private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
51 public static void init() throws FileNotFoundException, ReactorException {
52 final SchemaContext globalContext = TestUtils.loadSchemaContext("/full-versions/yangs");
53 assertNull(globalContext.findModuleByName(EXTERNAL_MODULE_NAME, null));
54 final Set<Module> allModules = globalContext.getModules();
55 assertNotNull(allModules);
56 CONTROLLER_CONTEXT.setSchemas(globalContext);
60 public void testLeafRefFromExternalModule() throws FileNotFoundException, ReactorException {
62 final InstanceIdentifierContext<?> ctx = CONTROLLER_CONTEXT.toInstanceIdentifier(
63 "simple-nodes:users/yang-ext:mount/leafref-module:cont/lst-with-lfref-key/id-string");
65 final Map<QName, Object> keyValues = new HashMap<>();
66 keyValues.put(KEY_QNAME, "id-string");
67 final YangInstanceIdentifier expectedYII = YangInstanceIdentifier.of(CONT_QNAME).node(LIST_QNAME)
68 .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(LIST_QNAME, keyValues));
70 assertEquals(expectedYII, ctx.getInstanceIdentifier());
73 private void initMountService() throws FileNotFoundException, ReactorException {
74 final DOMMountPointService mountService = mock(DOMMountPointService.class);
75 CONTROLLER_CONTEXT.setMountService(mountService);
76 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
77 final RestconfImpl restconfImpl = RestconfImpl.getInstance();
78 restconfImpl.setBroker(brokerFacade);
79 restconfImpl.setControllerContext(CONTROLLER_CONTEXT);
80 final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/leafref/yang");
81 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
82 when(mountInstance.getSchemaContext()).thenReturn(mountPointContext);
83 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));