Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / DOMRpcServiceTestBugfix560.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.test.connect.dom;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.io.InputStream;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
31 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
33 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
34 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
35 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.model.api.Module;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
51 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
52
53 /**
54  * Test case for reported bug 560
55  *
56  * @author Lukas Sedlak
57  * @see <a
58  *      href="https://bugs.opendaylight.org/show_bug.cgi?id=560">https://bugs.opendaylight.org/show_bug.cgi?id=560</a>
59  */
60 public class DOMRpcServiceTestBugfix560 {
61
62     private final static String RPC_SERVICE_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:bi:ba:rpcservice";
63     private final static String REVISION_DATE = "2014-07-01";
64     private final static QName RPC_NAME = QName.create(RPC_SERVICE_NAMESPACE,
65             REVISION_DATE, "rock-the-house");
66
67     private static final String TLL_NAME = "id";
68     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
69
70     private static final InstanceIdentifier<TopLevelList> BA_MOUNT_ID = createBATllIdentifier(TLL_NAME);
71     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_MOUNT_ID = createBITllIdentifier(TLL_NAME);
72
73     private BindingTestContext testContext;
74     private DOMMountPointService domMountPointService;
75     private MountProviderService bindingMountPointService;
76     private SchemaContext schemaContext;
77
78     /**
79      * @throws java.lang.Exception
80      */
81     @Before
82     public void setUp() throws Exception {
83         final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
84         testFactory.setExecutor(MoreExecutors.sameThreadExecutor());
85         testFactory.setStartWithParsedSchema(true);
86         testContext = testFactory.getTestContext();
87
88         testContext.start();
89         domMountPointService = testContext.getDomMountProviderService();
90         bindingMountPointService = testContext.getBindingMountProviderService();
91         assertNotNull(domMountPointService);
92
93         final YangContextParser parser = new YangParserImpl();
94         final InputStream moduleStream = BindingReflections.getModuleInfo(
95                 OpendaylightTestRpcServiceService.class)
96                 .getModuleSourceStream();
97
98         assertNotNull(moduleStream);
99         final List<InputStream> rpcModels = Collections.singletonList(moduleStream);
100         @SuppressWarnings("deprecation")
101         final
102         Set<Module> modules = parser.parseYangModelsFromStreams(rpcModels);
103         @SuppressWarnings("deprecation")
104         final
105         SchemaContext mountSchemaContext = parser.resolveSchemaContext(modules);
106         schemaContext = mountSchemaContext;
107     }
108
109     private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBITllIdentifier(
110             final String mount) {
111         return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
112                 .builder().node(Top.QNAME)
113                 .node(TopLevelList.QNAME)
114                 .nodeWithKey(TopLevelList.QNAME, TLL_NAME_QNAME, mount)
115                 .toInstance();
116     }
117
118     private static InstanceIdentifier<TopLevelList> createBATllIdentifier(
119             final String mount) {
120         return InstanceIdentifier.builder(Top.class)
121                 .child(TopLevelList.class, new TopLevelListKey(mount)).toInstance();
122     }
123
124     @SuppressWarnings("deprecation")
125     @Test
126     public void test() throws ExecutionException, InterruptedException {
127         // FIXME: This is made to only make sure instance identifier codec
128         // for path is instantiated.
129         domMountPointService
130                 .createMountPoint(BI_MOUNT_ID).addService(DOMRpcService.class, new DOMRpcService() {
131
132                     @Override
133                     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T arg0) {
134                         // TODO Auto-generated method stub
135                         return null;
136                     }
137
138                     @Override
139                     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath arg0, final NormalizedNode<?, ?> arg1) {
140                         final DOMRpcResult result = new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
141                         return Futures.immediateCheckedFuture(result);
142                     }
143                 }).register();
144         final MountProviderInstance mountInstance = bindingMountPointService
145                 .getMountPoint(BA_MOUNT_ID);
146         assertNotNull(mountInstance);
147         final OpendaylightTestRpcServiceService rpcService = mountInstance
148                 .getRpcService(OpendaylightTestRpcServiceService.class);
149         assertNotNull(rpcService);
150
151         try {
152             final Future<RpcResult<Void>> result = rpcService
153                     .rockTheHouse(new RockTheHouseInputBuilder().build());
154             assertTrue(result.get().isSuccessful());
155         } catch (final IllegalStateException ex) {
156             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
157         }
158     }
159
160     /**
161      * @throws java.lang.Exception
162      */
163     @After
164     public void teardown() throws Exception {
165         testContext.close();
166     }
167 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.