Implemented ovs manager insert for client access to ovs tables
[netvirt.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / internal / Connection.java
1 package org.opendaylight.ovsdb.internal;
2 import java.io.IOException;
3 import java.net.Socket;
4 import java.util.Map;
5
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;
12
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;
18
19 public class Connection implements RequestListener {
20     private Node node;
21     private String identifier;
22     private Socket socket;
23     private JsonRpcClient rpcClient;
24     private static final Logger logger = LoggerFactory.getLogger(Connection.class);
25
26     public Connection(String identifier, Socket socket, JsonRpcClient rpcClient) {
27         super();
28         this.identifier = identifier;
29         this.socket = socket;
30         this.rpcClient = rpcClient;
31         rpcClient.setRequestListener(this);
32         try {
33         node = new Node("OVS", identifier);
34         } catch (Exception e) {
35             e.printStackTrace();
36             logger.error("Error creating Node {}", e.getMessage());
37         }
38     }
39
40     public String getIdentifier() {
41         return identifier;
42     }
43
44     public void setIdentifier(String identifier) {
45         this.identifier = identifier;
46     }
47
48     public Socket getSocket() {
49         return socket;
50     }
51
52     public void setSocket(Socket socket) {
53         this.socket = socket;
54     }
55
56     public JsonRpcClient getRpcClient() {
57         return rpcClient;
58     }
59     public void setRpcClient(JsonRpcClient rpcClient) {
60         this.rpcClient = rpcClient;
61     }
62
63     public Node getNode() {
64         return node;
65     }
66
67     public void setNode(Node node) {
68         this.node = node;
69     }
70
71     @Override
72     public void onBeforeRequestSent(JsonRpcClient client, ObjectNode request) {
73         request.remove("jsonrpc"); //ovsdb-server expects JSON-RPC v1.0
74     }
75
76     @Override
77     public void onBeforeResponseProcessed(JsonRpcClient client,
78             ObjectNode response) {
79
80     }
81
82     public void sendMessage(OvsdbMessage message) throws IOException{
83         try{
84             rpcClient.invoke(message.methodName, message.argument, socket.getOutputStream(), message.id);
85         }catch(Exception e){
86             logger.warn("Could not send RPC for {} ({})", message.methodName, e.getMessage());
87         }
88     }
89
90     public Object readResponse(Class<?> clazz) throws Throwable{
91         try{
92             if(clazz.equals(String[].class)){
93                 String[] result = this.rpcClient.readResponse(String[].class, socket.getInputStream());
94                 for (String res : result) logger.info(res);
95                 return result;
96             }
97             else if(clazz.equals(Uuid[].class)){
98                 Uuid[] result = this.rpcClient.readResponse(Uuid[].class, socket.getInputStream());
99                 return result;
100             }
101             else if(clazz.equals(Map.class)){
102                 Map result = this.rpcClient.readResponse(Map.class, socket.getInputStream());
103                 return result;
104             }
105             else{
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);
110                 return result;
111             }
112
113         }catch(Exception e){
114             logger.warn("Could not receive RPC response: {}", e.getMessage());
115         }
116         return null;
117     }
118
119     public Status disconnect() {
120         try {
121             socket.close();
122         } catch (IOException e) {
123             e.printStackTrace();
124             return new Status(StatusCode.INTERNALERROR, e.getMessage());
125         }
126
127         return new Status(StatusCode.SUCCESS);
128     }
129
130     @Override
131     public int hashCode() {
132         final int prime = 31;
133         int result = 1;
134         result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
135         return result;
136     }
137
138     @Override
139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         if (obj == null)
143             return false;
144         if (getClass() != obj.getClass())
145             return false;
146         Connection other = (Connection) obj;
147         if (identifier == null) {
148             if (other.identifier != null)
149                 return false;
150         } else if (!identifier.equals(other.identifier))
151             return false;
152         return true;
153     }
154 }