1 package org.opendaylight.ovsdb.lib.jsonrpc;
3 import io.netty.channel.Channel;
5 import java.lang.reflect.InvocationHandler;
6 import java.lang.reflect.Method;
11 import org.opendaylight.controller.sal.core.Node;
12 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import com.fasterxml.jackson.core.JsonProcessingException;
17 import com.fasterxml.jackson.databind.JavaType;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.type.TypeFactory;
21 import com.google.common.collect.Maps;
22 import com.google.common.reflect.Reflection;
23 import com.google.common.reflect.TypeToken;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import com.google.common.util.concurrent.SettableFuture;
27 public class JsonRpcEndpoint {
29 protected static final Logger logger = LoggerFactory.getLogger(JsonRpcEndpoint.class);
31 public class CallContext {
33 JsonRpc10Request request;
34 SettableFuture<Object> future;
36 public CallContext(JsonRpc10Request request, Method method, SettableFuture<Object> future) {
38 this.request = request;
42 public Method getMethod() {
46 public JsonRpc10Request getRequest() {
50 public SettableFuture<Object> getFuture() {
55 ObjectMapper objectMapper;
57 Map<String, CallContext> methodContext = Maps.newHashMap();
58 Map<Node, OvsdbRPC.Callback> requestCallbacks = Maps.newHashMap();
60 public JsonRpcEndpoint(ObjectMapper objectMapper, Channel channel) {
61 this.objectMapper = objectMapper;
62 this.nettyChannel = channel;
65 public <T> T getClient(final Node node, Class<T> klazz) {
67 return Reflection.newProxy(klazz, new InvocationHandler() {
69 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
70 if (method.getName().equals(OvsdbRPC.REGISTER_CALLBACK_METHOD)) {
71 if ((args == null) || args.length != 1 || !(args[0] instanceof OvsdbRPC.Callback)) return false;
72 requestCallbacks.put(node, (OvsdbRPC.Callback)args[0]);
76 JsonRpc10Request request = new JsonRpc10Request(UUID.randomUUID().toString());
77 request.setMethod(method.getName());
79 if (args != null && args.length != 0) {
80 List<Object> params = null;
82 if (args.length == 1) {
83 if (args[0] instanceof Params) {
84 params = ((Params) args[0]).params();
85 } else if (args[0] instanceof List) {
86 params = (List<Object>) args[0];
90 throw new RuntimeException("do not understand this argument yet");
92 request.setParams(params);
96 String s = objectMapper.writeValueAsString(request);
97 logger.trace("{}", s);
99 SettableFuture<Object> sf = SettableFuture.create();
100 methodContext.put(request.getId(), new CallContext(request, method, sf));
102 nettyChannel.writeAndFlush(s);
110 public void processResult(JsonNode response) throws NoSuchMethodException {
112 CallContext returnCtxt = methodContext.get(response.get("id").asText());
113 if (returnCtxt == null) return;
115 if (ListenableFuture.class == returnCtxt.getMethod().getReturnType()) {
116 TypeToken<?> retType = TypeToken.of(
117 returnCtxt.getMethod().getGenericReturnType())
118 .resolveType(ListenableFuture.class.getMethod("get").getGenericReturnType());
119 JavaType javaType = TypeFactory.defaultInstance().constructType (retType.getType());
121 JsonNode result = response.get("result");
122 logger.trace("Response : {}", result.toString());
124 Object result1 = objectMapper.convertValue(result, javaType);
125 JsonNode error = response.get("error");
127 logger.error("Error : {}", error.toString());
130 returnCtxt.getFuture().set(result1);
133 throw new RuntimeException("donno how to deal with this");
137 public void processRequest(Node node, JsonNode requestJson) {
138 JsonRpc10Request request = new JsonRpc10Request(requestJson.get("id").asText());
139 request.setMethod(requestJson.get("method").asText());
140 logger.trace("Request : {} {}", requestJson.get("method"), requestJson.get("params"));
141 OvsdbRPC.Callback callback = requestCallbacks.get(node);
142 if (callback != null) {
143 Method[] methods = callback.getClass().getDeclaredMethods();
144 for (Method m : methods) {
145 if (m.getName().equals(request.getMethod())) {
146 Class<?>[] parameters = m.getParameterTypes();
147 JsonNode params = requestJson.get("params");
148 Object param = objectMapper.convertValue(params, parameters[1]);
150 m.invoke(callback, node, param);
151 } catch (Exception e) {
159 // Echo dont need any special processing. hence handling it internally.
161 if (request.getMethod().equals("echo")) {
162 JsonRpc10Response response = new JsonRpc10Response(request.getId());
163 response.setError(null);
165 String s = objectMapper.writeValueAsString(response);
166 nettyChannel.writeAndFlush(s);
167 } catch (JsonProcessingException e) {
173 logger.error("No handler for Request : {} on {}",requestJson.toString(), node);
176 public Map<String, CallContext> getMethodContext() {
177 return methodContext;