1 # Copyright (c) 2011, 2012 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
25 vlog = ovs.vlog.Vlog("unixctl_client")
26 strtypes = types.StringTypes
29 class UnixctlClient(object):
30 def __init__(self, conn):
31 assert isinstance(conn, ovs.jsonrpc.Connection)
34 def transact(self, command, argv):
35 assert isinstance(command, strtypes)
36 assert isinstance(argv, list)
38 assert isinstance(arg, strtypes)
40 request = ovs.jsonrpc.Message.create_request(command, argv)
41 error, reply = self._conn.transact_block(request)
44 vlog.warn("error communicating with %s: %s"
45 % (self._conn.name, os.strerror(error)))
46 return error, None, None
48 if reply.error is not None:
49 return 0, str(reply.error), None
51 assert reply.result is not None
52 return 0, None, str(reply.result)
60 assert isinstance(path, str)
62 unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
63 error, stream = ovs.stream.Stream.open_block(
64 ovs.stream.Stream.open(unix))
67 vlog.warn("failed to connect to %s" % path)
70 return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))