1 package org.opendaylight.ovsdb.internal;
2 import java.io.IOException;
3 import java.net.Socket;
6 import org.opendaylight.controller.sal.core.Node;
7 import org.opendaylight.controller.sal.utils.Status;
8 import org.opendaylight.controller.sal.utils.StatusCode;
9 import org.opendaylight.ovsdb.database.Uuid;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 import com.fasterxml.jackson.core.JsonParser;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import com.fasterxml.jackson.databind.node.ObjectNode;
16 import com.googlecode.jsonrpc4j.JsonRpcClient;
17 import com.googlecode.jsonrpc4j.JsonRpcClient.RequestListener;
19 public class Connection implements RequestListener {
21 private String identifier;
22 private Socket socket;
23 private JsonRpcClient rpcClient;
24 private static final Logger logger = LoggerFactory.getLogger(Connection.class);
26 public Connection(String identifier, Socket socket, JsonRpcClient rpcClient) {
28 this.identifier = identifier;
30 this.rpcClient = rpcClient;
31 rpcClient.setRequestListener(this);
33 node = new Node("OVS", identifier);
34 } catch (Exception e) {
36 logger.error("Error creating Node {}", e.getMessage());
40 public String getIdentifier() {
44 public void setIdentifier(String identifier) {
45 this.identifier = identifier;
48 public Socket getSocket() {
52 public void setSocket(Socket socket) {
56 public JsonRpcClient getRpcClient() {
59 public void setRpcClient(JsonRpcClient rpcClient) {
60 this.rpcClient = rpcClient;
63 public Node getNode() {
67 public void setNode(Node node) {
72 public void onBeforeRequestSent(JsonRpcClient client, ObjectNode request) {
73 request.remove("jsonrpc"); //ovsdb-server expects JSON-RPC v1.0
77 public void onBeforeResponseProcessed(JsonRpcClient client,
78 ObjectNode response) {
82 public void sendMessage(OvsdbMessage message) throws IOException{
84 rpcClient.invoke(message.methodName, message.argument, socket.getOutputStream(), message.id);
86 logger.warn("Could not send RPC for {} ({})", message.methodName, e.getMessage());
90 public Object readResponse(Class<?> clazz) throws Throwable{
92 if(clazz.equals(String[].class)){
93 String[] result = this.rpcClient.readResponse(String[].class, socket.getInputStream());
94 for (String res : result) logger.info(res);
97 else if(clazz.equals(Uuid[].class)){
98 Uuid[] result = this.rpcClient.readResponse(Uuid[].class, socket.getInputStream());
101 else if(clazz.equals(Map.class)){
102 Map result = this.rpcClient.readResponse(Map.class, socket.getInputStream());
106 ObjectNode jsonObject = this.rpcClient.readResponse(ObjectNode.class, socket.getInputStream());
107 ObjectMapper mapper = new ObjectMapper();
108 JsonParser parser = mapper.treeAsTokens(jsonObject);
109 Object result = mapper.readValue(parser, clazz);
114 logger.warn("Could not receive RPC response: {}", e.getMessage());
119 public Status disconnect() {
122 } catch (IOException e) {
124 return new Status(StatusCode.INTERNALERROR, e.getMessage());
127 return new Status(StatusCode.SUCCESS);
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
139 public boolean equals(Object obj) {
144 if (getClass() != obj.getClass())
146 Connection other = (Connection) obj;
147 if (identifier == null) {
148 if (other.identifier != null)
150 } else if (!identifier.equals(other.identifier))