Adjust to yangtools-2.0.0 changes
[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.base.Optional;
15 import com.google.common.io.ByteSource;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.io.IOException;
20 import java.io.InputStream;
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.binding.api.MountPoint;
27 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
33 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
34 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
35 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
36 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
45 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.common.Revision;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
52 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
53 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
54 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
55 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
56
57 /**
58  * Test case for reported bug 560
59  *
60  * @author Lukas Sedlak
61  * @see <a
62  *      href="https://bugs.opendaylight.org/show_bug.cgi?id=560">https://bugs.opendaylight.org/show_bug.cgi?id=560</a>
63  */
64 public class DOMRpcServiceTestBugfix560 {
65
66     private final static String RPC_SERVICE_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:bi:ba:rpcservice";
67     private final static String REVISION_DATE = "2014-07-01";
68     private final static QName RPC_NAME = QName.create(RPC_SERVICE_NAMESPACE,
69             REVISION_DATE, "rock-the-house");
70
71     private static final String TLL_NAME = "id";
72     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
73
74     private static final InstanceIdentifier<TopLevelList> BA_MOUNT_ID = createBATllIdentifier(TLL_NAME);
75     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_MOUNT_ID = createBITllIdentifier(TLL_NAME);
76
77     private BindingTestContext testContext;
78     private DOMMountPointService domMountPointService;
79     private MountPointService bindingMountPointService;
80     private SchemaContext schemaContext;
81
82     /**
83      * @throws java.lang.Exception
84      */
85     @Before
86     public void setUp() throws Exception {
87         final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
88         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
89         testFactory.setStartWithParsedSchema(true);
90         testContext = testFactory.getTestContext();
91
92         testContext.start();
93         domMountPointService = testContext.getDomMountProviderService();
94         bindingMountPointService = testContext.getBindingMountPointService();
95         assertNotNull(domMountPointService);
96
97         final YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(OpendaylightTestRpcServiceService.class);
98         assertNotNull(moduleInfo);
99
100         schemaContext = YangParserTestUtils.parseYangSources(StatementParserMode.DEFAULT_MODE, null,
101             YangTextSchemaSource.delegateForByteSource(RevisionSourceIdentifier.create(moduleInfo.getName(),
102                 Revision.ofNullable(moduleInfo.getRevision())), new ByteSource() {
103
104                 @Override
105                 public InputStream openStream() throws IOException {
106                     return moduleInfo.getModuleSourceStream();
107                 }
108             }));
109     }
110
111     private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBITllIdentifier(
112             final String mount) {
113         return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
114                 .builder().node(Top.QNAME)
115                 .node(TopLevelList.QNAME)
116                 .nodeWithKey(TopLevelList.QNAME, TLL_NAME_QNAME, mount)
117                 .build();
118     }
119
120     private static InstanceIdentifier<TopLevelList> createBATllIdentifier(
121             final String mount) {
122         return InstanceIdentifier.builder(Top.class)
123                 .child(TopLevelList.class, new TopLevelListKey(mount)).build();
124     }
125
126     @Test
127     public void test() throws ExecutionException, InterruptedException {
128         // FIXME: This is made to only make sure instance identifier codec 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
145         final Optional<MountPoint> mountInstance = bindingMountPointService.getMountPoint(BA_MOUNT_ID);
146         assertTrue(mountInstance.isPresent());
147
148         final Optional<RpcConsumerRegistry> rpcRegistry = mountInstance.get().getService(RpcConsumerRegistry.class);
149         assertTrue(rpcRegistry.isPresent());
150         final OpendaylightTestRpcServiceService rpcService = rpcRegistry.get()
151                 .getRpcService(OpendaylightTestRpcServiceService.class);
152         assertNotNull(rpcService);
153
154         try {
155             final Future<RpcResult<Void>> result = rpcService
156                     .rockTheHouse(new RockTheHouseInputBuilder().build());
157             assertTrue(result.get().isSuccessful());
158         } catch (final IllegalStateException ex) {
159             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
160         }
161     }
162
163     /**
164      * @throws java.lang.Exception
165      */
166     @After
167     public void teardown() throws Exception {
168         testContext.close();
169     }
170 }

©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.