Merge "Bug 2731: Discard changes only when transaction exist."
[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 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.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
45 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
46
47 import com.google.common.collect.ImmutableSet;
48 import com.google.common.util.concurrent.Futures;
49 import com.google.common.util.concurrent.ListenableFuture;
50 import com.google.common.util.concurrent.MoreExecutors;
51
52 /**
53  * Test case for reported bug 560
54  *
55  * @author Lukas Sedlak
56  * @see <a
57  *      href="https://bugs.opendaylight.org/show_bug.cgi?id=560">https://bugs.opendaylight.org/show_bug.cgi?id=560</a>
58  */
59 public class DOMRpcServiceTestBugfix560 {
60
61     private final static String RPC_SERVICE_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:bi:ba:rpcservice";
62     private final static String REVISION_DATE = "2014-07-01";
63     private final static QName RPC_NAME = QName.create(RPC_SERVICE_NAMESPACE,
64             REVISION_DATE, "rock-the-house");
65
66     private static final String TLL_NAME = "id";
67     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
68
69     private static final InstanceIdentifier<TopLevelList> BA_MOUNT_ID = createBATllIdentifier(TLL_NAME);
70     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_MOUNT_ID = createBITllIdentifier(TLL_NAME);
71
72     private BindingTestContext testContext;
73     private MountProvisionService domMountPointService;
74     private MountProviderService bindingMountPointService;
75     private SchemaContext schemaContext;
76
77     /**
78      * @throws java.lang.Exception
79      */
80     @Before
81     public void setUp() throws Exception {
82         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
83         testFactory.setExecutor(MoreExecutors.sameThreadExecutor());
84         testFactory.setStartWithParsedSchema(true);
85         testContext = testFactory.getTestContext();
86
87         testContext.start();
88         domMountPointService = testContext.getDomMountProviderService();
89         bindingMountPointService = testContext.getBindingMountProviderService();
90         assertNotNull(domMountPointService);
91
92         final YangContextParser parser = new YangParserImpl();
93         final InputStream moduleStream = BindingReflections.getModuleInfo(
94                 OpendaylightTestRpcServiceService.class)
95                 .getModuleSourceStream();
96
97         assertNotNull(moduleStream);
98         List<InputStream> rpcModels = Collections.singletonList(moduleStream);
99         @SuppressWarnings("deprecation")
100         Set<Module> modules = parser.parseYangModelsFromStreams(rpcModels);
101         @SuppressWarnings("deprecation")
102         SchemaContext mountSchemaContext = parser.resolveSchemaContext(modules);
103         schemaContext = mountSchemaContext;
104     }
105
106     private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBITllIdentifier(
107             final String mount) {
108         return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
109                 .builder().node(Top.QNAME)
110                 .nodeWithKey(TopLevelList.QNAME, TLL_NAME_QNAME, mount)
111                 .toInstance();
112     }
113
114     private static InstanceIdentifier<TopLevelList> createBATllIdentifier(
115             final String mount) {
116         return InstanceIdentifier.builder(Top.class)
117                 .child(TopLevelList.class, new TopLevelListKey(mount)).toInstance();
118     }
119
120     @SuppressWarnings("deprecation")
121     @Test
122     public void test() throws ExecutionException, InterruptedException {
123         // FIXME: This is made to only make sure instance identifier codec
124         // for path is instantiated.
125         testContext.getBindingDataBroker().readOperationalData(BA_MOUNT_ID);
126         final MountProvisionInstance mountPoint = domMountPointService
127                 .createMountPoint(BI_MOUNT_ID);
128         mountPoint.setSchemaContext(schemaContext);
129         assertNotNull(mountPoint);
130
131         mountPoint.addRpcImplementation(RPC_NAME, new RpcImplementation() {
132
133             @Override
134             public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(
135                     final QName rpc, final CompositeNode input) {
136
137                 return Futures.immediateFuture(RpcResultBuilder
138                         .<CompositeNode> success().build());
139             }
140
141             @Override
142             public Set<QName> getSupportedRpcs() {
143                 return ImmutableSet.of(RPC_NAME);
144             }
145         });
146
147         final Set<QName> biSupportedRpcs = mountPoint.getSupportedRpcs();
148         assertNotNull(biSupportedRpcs);
149         assertTrue(!biSupportedRpcs.isEmpty());
150
151         MountProviderInstance mountInstance = bindingMountPointService
152                 .getMountPoint(BA_MOUNT_ID);
153         assertNotNull(mountInstance);
154         final OpendaylightTestRpcServiceService rpcService = mountInstance
155                 .getRpcService(OpendaylightTestRpcServiceService.class);
156         assertNotNull(rpcService);
157
158         try {
159             Future<RpcResult<Void>> result = rpcService
160                     .rockTheHouse(new RockTheHouseInputBuilder().build());
161             assertTrue(result.get().isSuccessful());
162         } catch (IllegalStateException ex) {
163             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
164         }
165     }
166
167     /**
168      * @throws java.lang.Exception
169      */
170     @After
171     public void teardown() throws Exception {
172         testContext.close();
173     }
174 }