2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.restconf.client;
10 import com.google.common.base.Function;
11 import com.sun.jersey.api.client.ClientResponse;
12 import java.lang.reflect.InvocationHandler;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.Proxy;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
18 import org.opendaylight.yangtools.restconf.client.to.RestRpcError;
19 import org.opendaylight.yangtools.restconf.client.to.RestRpcResult;
20 import org.opendaylight.yangtools.restconf.common.ResourceUri;
21 import org.opendaylight.yangtools.yang.binding.BindingMapping;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
25 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
26 import org.opendaylight.yangtools.yang.common.RpcError;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
36 public class BindingToRestRpc implements InvocationHandler {
38 private final RestconfClientImpl client;
39 private static final Logger logger = LoggerFactory.getLogger(BindingToRestRpc.class);
40 private final BindingNormalizedNodeCodecRegistry mappingService;
41 private final SchemaContext schcemaContext;
42 private final Module module;
43 private static final int STATUS_OK = 200;
45 public BindingToRestRpc(final Class<?> proxiedInterface,final BindingNormalizedNodeCodecRegistry mappingService2,final RestconfClientImpl client,final SchemaContext schemaContext) throws Exception {
46 this.mappingService = mappingService2;
48 this.schcemaContext = schemaContext;
49 YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(proxiedInterface);
50 this.module = schemaContext.findModuleByName(moduleInfo.getName(),org.opendaylight.yangtools.yang.common.QName.parseRevision(moduleInfo.getRevision()));
53 @SuppressWarnings("unchecked")
55 public Object invoke(final Object o,final Method method, final Object[] objects) throws Exception {
56 for (RpcDefinition rpcDef:module.getRpcs()){
57 if (method.getName().equals(BindingMapping.getMethodName(rpcDef.getQName()))){
59 String moduleName = BindingReflections.getModuleInfo(method.getDeclaringClass()).getName();
60 String rpcMethodName = rpcDef.getQName().getLocalName();
61 Document rpcInputDoc = null;
62 for (Object component:objects){
63 ContainerNode rpcInput = mappingService.toNormalizedNodeRpcData((DataObject) component);
65 // FIXME: NormalizedNodeXmlStreamWriter
69 String payloadString = null;
70 final DataSchemaNode rpcOutputSchema = rpcDef.getOutput();
71 return client.post(ResourceUri.OPERATIONS.getPath() + "/" + moduleName + ":" + rpcMethodName,payloadString,new Function<ClientResponse, Object>() {
73 public Object apply(final ClientResponse clientResponse) {
74 if (clientResponse.getStatus() != STATUS_OK) {
75 throw new IllegalStateException("Can't get data from restconf. "+clientResponse.getClientResponseStatus());
77 List<RpcError> errors = new ArrayList<>();
79 ContainerNode output = null;
80 DataContainer rpcOutputDataObject = mappingService.fromNormalizedNodeRpcData(rpcOutputSchema.getPath(), output);
81 return new RestRpcResult(true,rpcOutputDataObject);
82 } catch (Exception e) {
83 logger.trace("Error while extracting rpc output in proxy method {}",e);
84 RestRpcError error = new RestRpcError(RpcError.ErrorSeverity.ERROR, RpcError.ErrorType.APPLICATION,"Error while extracting rpc output in proxy method.",e);
87 return new RestRpcResult(false,errors);
92 throw new IllegalStateException("Unexpected state of proxy method.");
95 @SuppressWarnings("unchecked")
96 public static<T> T getProxy(final Class<T> rpcService,
97 final BindingNormalizedNodeCodecRegistry mappingService2,
98 final RestconfClientImpl restconfClient,
99 final SchemaContext schemaContext) {
100 T proxiedType = null;
102 proxiedType = (T) Proxy.newProxyInstance
103 (BindingToRestRpc.class.getClassLoader(),
104 new Class[]{rpcService}, new BindingToRestRpc(rpcService, mappingService2, restconfClient, schemaContext));
105 } catch (Exception e) {
106 throw new IllegalStateException(e.getMessage());