forked from osrg/ops-gobgp
/
connection.py
161 lines (134 loc) · 4.77 KB
/
connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import socket
import threading
import traceback
import logging
from ovs.db import idl
from ovs.unixctl.client import *
from ovs import poller
from grpc.beta import implementations
import handle
from lib import utils
from lib import transaction
from api import gobgp_pb2 as api
log = logging.getLogger('connection')
class Connection(object):
def __init__(self):
self.timeout = 5
self.retry_limit = 5
self.wait_time = 3
self.hdr = None
self.th = None
self.conn_f = None
def get_handler(self):
return self.hdr
def connect(self):
connected = False
retry = 0
while not connected:
if retry >= self.retry_limit:
os._exit(1)
try:
self.conn_f()
self.th = threading.Thread(target=self.run)
self.th.setDaemon(True)
log.info('Connected')
connected = True
except Exception as e:
log.error('Faild to connect: {0}'.format(e))
time.sleep(self.wait_time)
retry += 1
self.th = threading.Thread(target=self.run)
self.th.setDaemon(True)
def start(self):
self.th.start()
def run(self):
raise NotImplementedError
class OpsConnection(Connection):
def __init__(self, ovsdb):
self.idl = None
self.poller = None
self.ovsdb = ovsdb
self.txns = transaction.TransactionQueue(1)
self.schema_name = "OpenSwitch"
super(OpsConnection, self).__init__()
def connect(self):
if self.idl is not None:
return
def conn():
log.info('Connecting to OpenSwitch...')
helper = utils.get_schema_helper(self.ovsdb, self.schema_name)
helper.register_all()
self.idl = idl.Idl(self.ovsdb, helper)
utils.wait_for_change(self.idl, self.timeout)
self.poller = poller.Poller()
self.hdr = handle.OpsHandler(self.idl, self)
self.conn_f = conn
super(OpsConnection, self).connect()
def start(self):
log.info('Run run_ops_to_gogbp thread...')
super(OpsConnection, self).start()
return self.th
def run(self):
first_time = True
while True:
self.idl.txn = None
self.idl.wait(self.poller)
self.poller.fd_wait(self.txns.alert_fileno, poller.POLLIN)
if not first_time:
self.poller.block()
self.idl.run()
self.hdr.handle_update()
txn = self.txns.get_nowait()
if txn is not None:
try:
txn.results.put(txn.do_commit())
except Exception as ex:
er = utils.ExceptionResult(ex=ex, tb=traceback.format_exc())
txn.results.put(er)
self.txns.task_done()
first_time = False
log.info('run_ops_to_gogbp thread is end')
def queue_txn(self, txn):
self.txns.put(txn)
class GobgpConnection(Connection):
def __init__(self, gobgp_url, gobgp_port):
self.gobgp_url = gobgp_url
self.gobgp_port = gobgp_port
self.channel = implementations.insecure_channel(gobgp_url, gobgp_port)
super(GobgpConnection, self).__init__()
def connect(self):
def conn():
log.info('Connecting to Gobgp...')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.gobgp_url, self.gobgp_port))
s.close()
g_conn = api.beta_create_GobgpApi_stub(self.channel)
self.hdr = handle.GobgpHandler(g_conn)
self.conn_f = conn
super(GobgpConnection, self).connect()
def start(self):
log.info('Run run_gogbp_to_ops thread...')
super(GobgpConnection, self).start()
return self.th
def run(self):
while True:
log.info('Wait for a change the bestpath from gobgp...')
monitor_argument = {'family': utils.RF_IPv4_UC}
self.hdr.monitor_bestpath_chenged(monitor_argument)
time.sleep(3)
log.info('run_ops_to_gogbp thread is end')