Merge "Bug 953 - Enable mounted RPC calls via RestConf"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / rpc / impl / MountPointRpcExecutor.java
1 /*
2 * Copyright (c) 2014 Brocade Communications 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.restconf.rpc.impl;
9
10 import java.util.concurrent.ExecutionException;
11
12 import javax.ws.rs.core.Response.Status;
13
14 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
15 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.ListenableFuture;
22
23 /**
24  * Provides an implementation which invokes rpc methods via a mounted yang data model.
25  * @author Devin Avery
26  *
27  */
28 public class MountPointRpcExecutor extends AbstractRpcExecutor {
29     private final MountInstance mountPoint;
30
31     public MountPointRpcExecutor(RpcDefinition rpcDef, MountInstance mountPoint) {
32         super( rpcDef );
33         this.mountPoint = mountPoint;
34         Preconditions.checkNotNull( mountPoint, "MountInstance can not be null." );
35     }
36
37     @Override
38     public RpcResult<CompositeNode> invokeRpc( CompositeNode rpcRequest ) throws ResponseException {
39         ListenableFuture<RpcResult<CompositeNode>> rpcFuture =
40                 mountPoint.rpc( getRpcDefinition().getQName(), rpcRequest);
41         try {
42             return rpcFuture.get();
43         } catch (InterruptedException | ExecutionException e) {
44             throw new ResponseException(Status.INTERNAL_SERVER_ERROR,
45                                         e.getCause().getMessage() );
46         }
47     }
48 }