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.
23 vlog = ovs.vlog.Vlog("unixctl_client")
24 strtypes = types.StringTypes
27 class UnixctlClient(object):
28 def __init__(self, conn):
29 assert isinstance(conn, ovs.jsonrpc.Connection)
32 def transact(self, command, argv):
33 assert isinstance(command, strtypes)
34 assert isinstance(argv, list)
36 assert isinstance(arg, strtypes)
38 request = ovs.jsonrpc.Message.create_request(command, argv)
39 error, reply = self._conn.transact_block(request)
42 vlog.warn("error communicating with %s: %s"
43 % (self._conn.name, os.strerror(error)))
44 return error, None, None
46 if reply.error is not None:
47 return 0, str(reply.error), None
49 assert reply.result is not None
50 return 0, None, str(reply.result)
58 assert isinstance(path, str)
60 unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
61 error, stream = ovs.stream.Stream.open_block(
62 ovs.stream.Stream.open(unix))
65 vlog.warn("failed to connect to %s" % path)
68 return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))