6a6af0687a9046f45fa82d262246af8fbbc329f1
[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 junit.framework.Assert.assertNotNull;
11 import static junit.framework.Assert.assertTrue;
12 import static junit.framework.Assert.fail;
13
14 import java.io.InputStream;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
25 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
26 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
27 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
28 import org.opendaylight.controller.sal.common.util.Rpcs;
29 import org.opendaylight.controller.sal.core.api.RpcImplementation;
30 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
31 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
46 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
47
48 import com.google.common.collect.ImmutableSet;
49 import com.google.common.util.concurrent.Futures;
50 import com.google.common.util.concurrent.ListenableFuture;
51 import com.google.common.util.concurrent.MoreExecutors;
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 NodeId MOUNT_NODE = new NodeId("id");
68     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
69
70     private static final InstanceIdentifier<Node> BA_MOUNT_ID = createBANodeIdentifier(MOUNT_NODE);
71     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_MOUNT_ID = createBINodeIdentifier(MOUNT_NODE);
72
73     private BindingTestContext testContext;
74     private MountProvisionService 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         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         List<InputStream> rpcModels = Collections.singletonList(moduleStream);
100         Set<Module> modules = parser.parseYangModelsFromStreams(rpcModels);
101         schemaContext = parser.resolveSchemaContext(modules);
102     }
103
104     private static org.opendaylight.yangtools.yang.data.api.InstanceIdentifier createBINodeIdentifier(
105             NodeId mountNode) {
106         return org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
107                 .builder().node(Nodes.QNAME)
108                 .nodeWithKey(Node.QNAME, NODE_ID_QNAME, mountNode.getValue())
109                 .toInstance();
110     }
111
112     private static InstanceIdentifier<Node> createBANodeIdentifier(
113             NodeId mountNode) {
114         return InstanceIdentifier.builder(Nodes.class)
115                 .child(Node.class, new NodeKey(mountNode)).toInstance();
116     }
117
118     @Test
119     public void test() throws ExecutionException, InterruptedException {
120         testContext.getBindingDataBroker().readOperationalData(BA_MOUNT_ID);
121         final MountProvisionInstance mountPoint = domMountPointService
122                 .createMountPoint(BI_MOUNT_ID);
123         mountPoint.setSchemaContext(schemaContext);
124         assertNotNull(mountPoint);
125
126         mountPoint.addRpcImplementation(RPC_NAME, new RpcImplementation() {
127
128             @Override
129             public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(
130                     QName rpc, CompositeNode input) {
131
132                 return Futures.immediateFuture(Rpcs
133                         .<CompositeNode> getRpcResult(true));
134             }
135
136             @Override
137             public Set<QName> getSupportedRpcs() {
138                 return ImmutableSet.of(RPC_NAME);
139             }
140         });
141
142         final Set<QName> biSupportedRpcs = mountPoint.getSupportedRpcs();
143         assertNotNull(biSupportedRpcs);
144         assertTrue(!biSupportedRpcs.isEmpty());
145
146         MountProviderInstance mountInstance = bindingMountPointService
147                 .getMountPoint(BA_MOUNT_ID);
148         assertNotNull(mountInstance);
149         final OpendaylightTestRpcServiceService rpcService = mountInstance
150                 .getRpcService(OpendaylightTestRpcServiceService.class);
151         assertNotNull(rpcService);
152
153         try {
154             Future<RpcResult<Void>> result = rpcService
155                     .rockTheHouse(new RockTheHouseInputBuilder().build());
156             assertTrue(result.get().isSuccessful());
157         } catch (IllegalStateException ex) {
158             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
159         }
160     }
161
162     /**
163      * @throws java.lang.Exception
164      */
165     @After
166     public void teardown() throws Exception {
167         testContext.close();
168     }
169 }

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