backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / ovs / db_client.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2012 Isaku Yamahata <yamahata at private email ne jp>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import logging
18 import os
19
20 from ovs import jsonrpc
21 from ovs import stream
22 from ovs import util as ovs_util
23 from ovs.db import schema
24
25 LOG = logging.getLogger(__name__)
26
27
28 class DBClient(object):
29     def __init__(self, remote):
30         super(DBClient, self).__init__()
31         self.remote = remote
32
33     def run_command(self, args):
34         _COMMANDS = {
35             'list-dbs': self._list_dbs,
36             'get-schema': self._get_schema,
37             'get-schema-version': self._get_schema_version,
38             'list-tables': self._list_tables,
39             'list-columns': self._list_columns,
40             'transact': self._transact,
41             'monitor': self._monitor,
42             'dump': self._dump,
43         }
44
45         command = args[0]
46         args = args[1:]
47
48         error, stream_ = stream.Stream.open_block(
49             stream.Stream.open(self.remote))
50         if error:
51             raise RuntimeError('can not open socket to %s: %s' %
52                                (self.remote, os.strerror(error)))
53         rpc = jsonrpc.Connection(stream_)
54
55         ret = _COMMANDS[command](rpc, *args)
56         LOG.info('ret %s', ret)
57         rpc.close()
58
59     def _check_txn(self, error, reply):
60         if error:
61             ovs_util.ovs_fatal(error, os.strerror(error))
62         elif reply.error:
63             ovs_util.ovs_fatal(reply.error, 'error %s' % reply.error)
64
65     def _fetch_dbs(self, rpc):
66         request = jsonrpc.Message.create_request('list_dbs', [])
67         error, reply = rpc.transact_block(request)
68         self._check_txn(error, reply)
69
70         dbs = set()
71         for name in reply.result:
72             dbs.add(name)
73
74         return dbs
75
76     def _fetch_schema_json(self, rpc, database):
77         request = jsonrpc.Message.create_request('get_schema', [database])
78         error, reply = rpc.transact_block(request)
79         self._check_txn(error, reply)
80         return reply.result
81
82     def _fetch_schema(self, rpc, database):
83         return schema.DbSchema.from_json(self._fetch_schema_json(rpc,
84                                                                  database))
85
86     # commands
87     def _list_dbs(self, rpc, *_):
88         return self._fetch_dbs(rpc)
89
90     def _get_schema(self, rpc, *args):
91         database = args[0]
92         return self._fetch_schema(rpc, database).to_json()
93
94     def _get_schema_version(self, rpc, *_args):
95         database = _args[0]
96         schema_ = self._fetch_schema(rpc, database)
97         return schema_.version
98
99     def _list_tables(self, rpc, *args):
100         database = args[0]
101         schema_ = self._fetch_schema(rpc, database)
102         return [table.to_json() for table in schema_.tables.values()]
103
104     def _list_columns(self, rpc, *args):
105         database = args[0]
106         table_name = None
107         if len(args) > 1:
108             table_name = args[1]
109
110         schema_ = self._fetch_schema(rpc, database)
111         if table_name is None:
112             tables = [table for table in schema_.tables.values()]
113         else:
114             tables = [table for table in schema_.tables.values()
115                       if table.name == table_name]
116
117         columns = []
118         for table in tables:
119             columns.extend(table.columns.values())
120         return [column.to_json() for column in columns]
121
122     def _transact(self, rpc, *args):
123         raise NotImplementedError()
124
125     def _monitor(self, rpc, *args):
126         raise NotImplementedError()
127
128     def _dump(self, rpc, *args):
129         raise NotImplementedError()