Debian Codegraph @

vif_plug_ovs/ovsdb/impl_idl.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import functools
14 import socket
15
16 from ovs.db import idl
17 from ovs import socket_util
18 from ovs import stream
19 from ovsdbapp.backend.ovs_idl import connection
20 from ovsdbapp.backend.ovs_idl import idlutils
21 from ovsdbapp.backend.ovs_idl import vlog
22 from ovsdbapp.schema.open_vswitch import impl_idl
23
24 from vif_plug_ovs.ovsdb import api
25
26 REQUIRED_TABLES = ('Interface', 'Port', 'Bridge', 'Open_vSwitch', 'QoS')
27
28
29 def idl_factory(config):
30 conn = config.connection
31 schema_name = 'Open_vSwitch'
32 helper = idlutils.get_schema_helper(conn, schema_name)
33 for table in REQUIRED_TABLES:
34 helper.register_table(table)
35 return idl.Idl(conn, helper)
36
37
38 def api_factory(config):
39 conn = connection.Connection(
40 idl=idl_factory(config),
41 timeout=config.timeout)
42 return NeutronOvsdbIdl(conn)
43
44
45 class NeutronOvsdbIdl(impl_idl.OvsdbIdl, api.ImplAPI):
46 """IDL interface for OVS database back-end
47
48 This class provides an OVSDB IDL (Open vSwitch Database Interface
49 Definition Language) interface to the OVS back-end.
50 """
51
52 def __init__(self, conn):
53 vlog.use_python_logger()
54 super(NeutronOvsdbIdl, self).__init__(conn)
55
56 def _get_table_columns(self, table):
57 return list(self.tables[table].columns)
58
59 def has_table_column(self, table, column):
60 return column in self._get_table_columns(table)
61
62
63 # this is derived form https://review.opendev.org/c/openstack/neutron/+/794892
64 def add_keepalives(fn):
65 @functools.wraps(fn)
66 def _open(*args, **kwargs):
67 error, sock = fn(*args, **kwargs)
68 try:
69 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
70 except socket.error as e:
71 sock.close()
72 return socket_util.get_exception_errno(e), None
73 return error, sock
74 return _open
75
76
77 class NoProbesMixin:
78 @staticmethod
79 def needs_probes():
80 # If we are using keepalives, we can force probe_interval=0
81 return False
82
83
84 class TCPStream(stream.TCPStream, NoProbesMixin):
85 @classmethod
86 @add_keepalives
87 def _open(cls, suffix, dscp):
88 return super()._open(suffix, dscp)
89
90
91 class SSLStream(stream.SSLStream, NoProbesMixin):
92 @classmethod
93 @add_keepalives
94 def _open(cls, suffix, dscp):
95 return super()._open(suffix, dscp)
96
97
98 # Overwriting globals in a library is clearly a good idea
99 stream.Stream.register_method("tcp", TCPStream)
100 stream.Stream.register_method("ssl", SSLStream)