blob: 68218adf1d886ecc649980b8387945eff3a00f86 [file] [log] [blame]
Sithara Punnassery9d464a32017-07-23 16:14:02 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sithara Punnassery9d464a32017-07-23 16:14:02 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.config;
17
18import org.onosproject.yang.model.RpcInput;
19import org.onosproject.yang.model.RpcOutput;
20import org.onosproject.yang.model.RpcService;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.lang.reflect.InvocationTargetException;
25import java.util.function.Supplier;
26
27/**
28 * Rpc Executor.
29 */
30public final class RpcExecutor implements Supplier<RpcOutput> {
31 private final Logger log = LoggerFactory.getLogger(getClass());
32 private RpcService handler;
33 private String msgId;
34 private RpcInput input;
35 private String rpcName;
36 int svcId;
37
38 /**
39 * Creates an RpcExecutor.
40 *
41 * @param handler rpc handler
42 * @param svcId svcId
43 * @param rpcName rpcName
44 * @param msgId rpc msgId
45 * @param input rpc input
46 */
47 public RpcExecutor(RpcService handler, int svcId, String rpcName, String msgId, RpcInput input) {
48 this.handler = handler;
49 this.rpcName = rpcName;
50 this.svcId = svcId;
51 this.msgId = msgId;
52 this.input = input;
53 }
54
55 @Override
56 public RpcOutput get() {
57 RpcOutput ret;
58 try {
59 ret = (RpcOutput) handler.getClass().getInterfaces()[svcId].
60 getMethod(rpcName, RpcInput.class).invoke(handler, input);
61 } catch (NoSuchMethodException | IllegalAccessException |
62 InvocationTargetException | IllegalArgumentException e) {
63 throw new FailedException(e.getMessage() + ", request:" + msgId);
64 }
65 ret.messageId(msgId);
66 return ret;
67 }
68}