6686a83a2e26ed55ca2a2cb7e9f40c3bce679f19
[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.core.api.RpcImplementation;
29 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
30 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInputBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
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.YangInstanceIdentifier 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         @SuppressWarnings("deprecation")
101         Set<Module> modules = parser.parseYangModelsFromStreams(rpcModels);
102         @SuppressWarnings("deprecation")
103         SchemaContext mountSchemaContext = parser.resolveSchemaContext(modules);
104         schemaContext = mountSchemaContext;
105     }
106
107     private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBINodeIdentifier(
108             final NodeId mountNode) {
109         return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
110                 .builder().node(Nodes.QNAME)
111                 .nodeWithKey(Node.QNAME, NODE_ID_QNAME, mountNode.getValue())
112                 .toInstance();
113     }
114
115     private static InstanceIdentifier<Node> createBANodeIdentifier(
116             final NodeId mountNode) {
117         return InstanceIdentifier.builder(Nodes.class)
118                 .child(Node.class, new NodeKey(mountNode)).toInstance();
119     }
120
121     @SuppressWarnings("deprecation")
122     @Test
123     public void test() throws ExecutionException, InterruptedException {
124         // FIXME: This is made to only make sure instance identifier codec
125         // for path is instantiated.
126         testContext.getBindingDataBroker().readOperationalData(BA_MOUNT_ID);
127         final MountProvisionInstance mountPoint = domMountPointService
128                 .createMountPoint(BI_MOUNT_ID);
129         mountPoint.setSchemaContext(schemaContext);
130         assertNotNull(mountPoint);
131
132         mountPoint.addRpcImplementation(RPC_NAME, new RpcImplementation() {
133
134             @Override
135             public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(
136                     final QName rpc, final CompositeNode input) {
137
138                 return Futures.immediateFuture(RpcResultBuilder
139                         .<CompositeNode> success().build());
140             }
141
142             @Override
143             public Set<QName> getSupportedRpcs() {
144                 return ImmutableSet.of(RPC_NAME);
145             }
146         });
147
148         final Set<QName> biSupportedRpcs = mountPoint.getSupportedRpcs();
149         assertNotNull(biSupportedRpcs);
150         assertTrue(!biSupportedRpcs.isEmpty());
151
152         MountProviderInstance mountInstance = bindingMountPointService
153                 .getMountPoint(BA_MOUNT_ID);
154         assertNotNull(mountInstance);
155         final OpendaylightTestRpcServiceService rpcService = mountInstance
156                 .getRpcService(OpendaylightTestRpcServiceService.class);
157         assertNotNull(rpcService);
158
159         try {
160             Future<RpcResult<Void>> result = rpcService
161                     .rockTheHouse(new RockTheHouseInputBuilder().build());
162             assertTrue(result.get().isSuccessful());
163         } catch (IllegalStateException ex) {
164             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
165         }
166     }
167
168     /**
169      * @throws java.lang.Exception
170      */
171     @After
172     public void teardown() throws Exception {
173         testContext.close();
174     }
175 }

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