blob: ae914d05c77b4355cb9da5f565639743d950975f [file] [log] [blame]
Zsolt Harasztib71c2a02016-09-12 13:12:07 -07001#!/usr/bin/env python
2#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08003# Copyright 2017 the original author or authors.
Zsolt Harasztib71c2a02016-09-12 13:12:07 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070018"""Virtual OLT Hardware Abstraction main entry point"""
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070019
20import argparse
Zsolt Harasztib5d72f12017-01-15 20:44:02 -080021import arrow
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070022import os
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070023import time
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070024
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070025import yaml
Zsolt Haraszti89a27302016-12-08 16:53:06 -080026from simplejson import dumps
Zsolt Harasztie060a7d2016-09-16 11:08:24 -070027from twisted.internet.defer import inlineCallbacks
Zsolt Harasztib5d72f12017-01-15 20:44:02 -080028from twisted.internet.task import LoopingCall
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -080029from zope.interface import implementer
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070030
Zsolt Haraszti3bfff662016-12-14 23:41:49 -080031from common.event_bus import EventBusClient
32from common.manhole import Manhole
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070033from common.structlog_setup import setup_logging
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070034from common.utils.dockerhelpers import get_my_containers_name
35from common.utils.nethelpers import get_my_primary_interface, \
Khen Nursimulu441dedd2016-10-05 14:44:26 -070036 get_my_primary_local_ipv4
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080037from voltha.adapters.loader import AdapterLoader
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070038from voltha.coordinator import Coordinator
Zsolt Harasztidafefe12016-11-14 21:29:58 -080039from voltha.core.core import VolthaCore
Ryan Van Gilder5a0ab622017-03-01 16:39:25 -080040from voltha.core.config.config_backend import load_backend
Zsolt Haraszti99509d32016-12-10 16:41:45 -080041from voltha.northbound.diagnostics import Diagnostics
Zsolt Harasztieb435072016-09-23 17:10:49 -070042from voltha.northbound.grpc.grpc_server import VolthaGrpcServer
khenb95fe9a2016-10-05 11:15:25 -070043from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070044from voltha.northbound.rest.health_check import init_rest_service
Zsolt Haraszti66862032016-11-28 14:28:39 -080045from voltha.protos.common_pb2 import LogLevel
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -080046from voltha.registry import registry, IComponent
Zsolt Haraszti89a27302016-12-08 16:53:06 -080047from common.frameio.frameio import FrameIOManager
48
Zsolt Harasztidafefe12016-11-14 21:29:58 -080049VERSION = '0.9.0'
khenb95fe9a2016-10-05 11:15:25 -070050
khenaidoo032d3302017-06-09 14:50:04 -040051
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070052defs = dict(
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070053 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070054 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti109db832016-09-16 16:32:36 -070055 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
56 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070057 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070058 grpc_port=os.environ.get('GRPC_PORT', 50055),
59 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
60 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
61 get_my_primary_local_ipv4()),
62 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070063 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070064 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Haraszti3bfff662016-12-14 23:41:49 -080065 manhole_port=os.environ.get('MANHOLE_PORT', 12222),
Ryan Van Gilder5a0ab622017-03-01 16:39:25 -080066 backend=os.environ.get('BACKEND', 'none'),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070067)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070068
69
70def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070071 parser = argparse.ArgumentParser()
72
Zsolt Haraszti109db832016-09-16 16:32:36 -070073 _help = ('Path to voltha.yml config file (default: %s). '
74 'If relative, it is relative to main.py of voltha.'
75 % defs['config'])
76 parser.add_argument('-c', '--config',
77 dest='config',
78 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070079 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070080 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070081
Zsolt Haraszti109db832016-09-16 16:32:36 -070082 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
83 parser.add_argument(
84 '-C', '--consul', dest='consul', action='store',
85 default=defs['consul'],
86 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070087
Zsolt Haraszti109db832016-09-16 16:32:36 -070088 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
89 'the cluster (default: %s)' % defs['external_host_address'])
90 parser.add_argument('-E', '--external-host-address',
91 dest='external_host_address',
92 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070093 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070094 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070095
Zsolt Haraszti553826c2016-09-27 10:24:27 -070096 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
97 % defs['grpc_port'])
98 parser.add_argument('-g', '--grpc-port',
99 dest='grpc_port',
100 action='store',
101 default=defs['grpc_port'],
102 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700103
Zsolt Haraszti109db832016-09-16 16:32:36 -0700104 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
105 'specified (None), the address from the config file is used'
106 % defs['fluentd'])
107 parser.add_argument('-F', '--fluentd',
108 dest='fluentd',
109 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700110 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700111 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700112
Zsolt Haraszti109db832016-09-16 16:32:36 -0700113 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
114 'cluster (default: %s)' % defs['internal_host_address'])
115 parser.add_argument('-H', '--internal-host-address',
116 dest='internal_host_address',
117 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700118 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700119 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700120
Zsolt Haraszti109db832016-09-16 16:32:36 -0700121 _help = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700122 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700123 parser.add_argument('-i', '--instance-id',
124 dest='instance_id',
125 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700126 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700127 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700128
129 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700130 _help = 'ETH interface to send (default: %s)' % defs['interface']
131 parser.add_argument('-I', '--interface',
132 dest='interface',
133 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700134 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700135 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700136
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800137 _help = 'open ssh manhole at given port'
138 parser.add_argument('-m', '--manhole-port',
139 dest='manhole_port',
140 action='store',
141 type=int,
142 default=None,
143 help=_help)
144
Zsolt Haraszti109db832016-09-16 16:32:36 -0700145 _help = 'omit startup banner log lines'
146 parser.add_argument('-n', '--no-banner',
147 dest='no_banner',
148 action='store_true',
149 default=False,
150 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700151
Zsolt Haraszti109db832016-09-16 16:32:36 -0700152 _help = 'do not emit periodic heartbeat log messages'
153 parser.add_argument('-N', '--no-heartbeat',
154 dest='no_heartbeat',
155 action='store_true',
156 default=False,
157 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700158
Zsolt Haraszti109db832016-09-16 16:32:36 -0700159 _help = ('port number for the rest service (default: %d)'
160 % defs['rest_port'])
161 parser.add_argument('-R', '--rest-port',
162 dest='rest_port',
163 action='store',
164 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700165 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700166 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700167
Zsolt Haraszti109db832016-09-16 16:32:36 -0700168 _help = "suppress debug and info logs"
169 parser.add_argument('-q', '--quiet',
170 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700171 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700172 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700173
Zsolt Haraszti109db832016-09-16 16:32:36 -0700174 _help = 'enable verbose logging'
175 parser.add_argument('-v', '--verbose',
176 dest='verbose',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700177 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700178 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700179
Zsolt Haraszti109db832016-09-16 16:32:36 -0700180 _help = ('use docker container name as voltha instance id'
181 ' (overrides -i/--instance-id option)')
182 parser.add_argument('--instance-id-is-container-name',
183 dest='instance_id_is_container_name',
184 action='store_true',
185 default=False,
186 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700187
khenb95fe9a2016-10-05 11:15:25 -0700188 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
189 'specified (None), the address from the config file is used'
190 % defs['kafka'])
191 parser.add_argument('-K', '--kafka',
192 dest='kafka',
193 action='store',
194 default=defs['kafka'],
195 help=_help)
196
Ryan Van Gilder5a0ab622017-03-01 16:39:25 -0800197 _help = 'backend to use for config persitence'
198 parser.add_argument('-b', '--backend',
199 default=defs['backend'],
200 choices=['none', 'consul'],
201 help=_help)
202
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700203 args = parser.parse_args()
204
205 # post-processing
206
207 if args.instance_id_is_container_name:
208 args.instance_id = get_my_containers_name()
209
210 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700211
212
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700213def load_config(args):
214 path = args.config
215 if path.startswith('.'):
216 dir = os.path.dirname(os.path.abspath(__file__))
217 path = os.path.join(dir, path)
218 path = os.path.abspath(path)
219 with open(path) as fd:
220 config = yaml.load(fd)
221 return config
222
223
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700224def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700225 log.info(' _ ______ __ ________ _____ ')
226 log.info('| | / / __ \/ / /_ __/ / / / |')
227 log.info('| | / / / / / / / / / /_/ / /| |')
228 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
229 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700230 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700231
232
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800233@implementer(IComponent)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700234class Main(object):
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800235
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700236 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700237
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700238 self.args = args = parse_args()
239 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700240
241 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700242 self.log = setup_logging(self.config.get('logging', {}),
243 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700244 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700245 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700246
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400247 # configurable variables from voltha.yml file
248 #self.configurable_vars = self.config.get('Constants', {})
249
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700250 if not args.no_banner:
251 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700252
khenaidoo032d3302017-06-09 14:50:04 -0400253 # Create a unique instnce id using the passed-in instanceid and
254 # UTC timestamp
255 current_time = arrow.utcnow().timestamp
256 self.instance_id = self.args.instance_id + '_' + str(current_time)
257
258 # Every voltha instance is given a core_storage id where the
259 # instance data is stored
260 self.core_store_id = None
261
khenb95fe9a2016-10-05 11:15:25 -0700262 self.startup_components()
263
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700264 if not args.no_heartbeat:
265 self.start_heartbeat()
khenaidoo032d3302017-06-09 14:50:04 -0400266 self.start_kafka_heartbeat(self.instance_id)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700267
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800268 self.manhole = None
269
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700270 def start(self):
271 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700272
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800273 def stop(self):
274 pass
275
276 def get_args(self):
277 """Allow access to command line args"""
278 return self.args
279
280 def get_config(self):
281 """Allow access to content of config file"""
282 return self.config
283
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800284 @inlineCallbacks
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700285 def startup_components(self):
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800286 try:
287 self.log.info('starting-internal-components')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800288
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800289 registry.register('main', self)
290
Zsolt Haraszti66862032016-11-28 14:28:39 -0800291 yield registry.register(
292 'coordinator',
293 Coordinator(
294 internal_host_address=self.args.internal_host_address,
295 external_host_address=self.args.external_host_address,
296 rest_port=self.args.rest_port,
khenaidoo032d3302017-06-09 14:50:04 -0400297 instance_id=self.instance_id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800298 config=self.config,
299 consul=self.args.consul)
300 ).start()
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800301
khenaidoo032d3302017-06-09 14:50:04 -0400302 self.log.info('waiting-for-config-assignment')
303
304 # Wait until we get a config id before we proceed
305 self.core_store_id, store_prefix = \
306 yield registry('coordinator').get_core_store_id_and_prefix()
307
308 self.log.info('store-id', core_store_id=self.core_store_id)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700309
Zsolt Haraszti66862032016-11-28 14:28:39 -0800310 yield registry.register(
311 'grpc_server',
312 VolthaGrpcServer(self.args.grpc_port)
313 ).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700314
Zsolt Haraszti66862032016-11-28 14:28:39 -0800315 yield registry.register(
khenaidoo032d3302017-06-09 14:50:04 -0400316 'core',
317 VolthaCore(
318 instance_id=self.instance_id,
319 core_store_id = self.core_store_id,
320 version=VERSION,
321 log_level=LogLevel.INFO
322 )
323 ).start(config_backend=load_backend(store_id=self.core_store_id,
324 store_prefix=store_prefix,
325 args=self.args))
326
327 init_rest_service(self.args.rest_port)
328
329 yield registry.register(
Zsolt Haraszti66862032016-11-28 14:28:39 -0800330 'kafka_proxy',
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800331 KafkaProxy(
332 self.args.consul,
333 self.args.kafka,
334 config=self.config.get('kafka-proxy', {})
335 )
Zsolt Haraszti66862032016-11-28 14:28:39 -0800336 ).start()
337
338 yield registry.register(
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800339 'frameio',
340 FrameIOManager()
341 ).start()
342
343 yield registry.register(
Zsolt Haraszti66862032016-11-28 14:28:39 -0800344 'adapter_loader',
345 AdapterLoader(config=self.config.get('adapter_loader', {}))
346 ).start()
khenb95fe9a2016-10-05 11:15:25 -0700347
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800348 yield registry.register(
349 'diag',
350 Diagnostics(config=self.config.get('diagnostics', {}))
351 ).start()
352
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800353 if self.args.manhole_port is not None:
354 self.start_manhole(self.args.manhole_port)
355
khenaidoo032d3302017-06-09 14:50:04 -0400356 # Now that all components are loaded, in the scenario where this
357 # voltha instance is picking up an existing set of data (from a
358 # voltha instance that dies/stopped) then we need to setup this
359 # instance from where the previous one left
360
361 yield registry('core').reconcile_data()
362
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800363 self.log.info('started-internal-services')
364
365 except Exception as e:
khenaidoo032d3302017-06-09 14:50:04 -0400366 self.log.exception('Failure-to-start-all-components', e=e)
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800367
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800368 def start_manhole(self, port):
369 self.manhole = Manhole(
370 port,
371 pws=dict(admin='adminpw'),
372 eventbus = EventBusClient(),
373 **registry.components
374 )
375
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700376 @inlineCallbacks
377 def shutdown_components(self):
378 """Execute before the reactor is shut down"""
379 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800380 for component in reversed(registry.iterate()):
381 yield component.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700382
Zsolt Haraszti3300f742017-01-09 01:14:20 -0800383 import threading
384 self.log.info('THREADS:')
385 main_thread = threading.current_thread()
386 for t in threading.enumerate():
387 if t is main_thread:
388 continue
389 if not t.isDaemon():
390 continue
391 self.log.info('joining thread {} {}'.format(
392 t.getName(), "daemon" if t.isDaemon() else "not-daemon"))
393 t.join()
394
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700395 def start_reactor(self):
396 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700397 reactor.callWhenRunning(
398 lambda: self.log.info('twisted-reactor-started'))
399 reactor.addSystemEventTrigger('before', 'shutdown',
400 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700401 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700402
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700403 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700404
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700405 t0 = time.time()
406 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700407
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700408 def heartbeat():
409 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700410
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700411 lc = LoopingCall(heartbeat)
412 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700413
khenb95fe9a2016-10-05 11:15:25 -0700414 # Temporary function to send a heartbeat message to the external kafka
415 # broker
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800416 def start_kafka_heartbeat(self, instance_id):
khenb95fe9a2016-10-05 11:15:25 -0700417 # For heartbeat we will send a message to a specific "voltha-heartbeat"
418 # topic. The message is a protocol buf
419 # message
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800420 message = dict(
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800421 type='heartbeat',
422 voltha_instance=instance_id,
423 ip=get_my_primary_local_ipv4()
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800424 )
425 topic = "voltha.heartbeat"
khenb95fe9a2016-10-05 11:15:25 -0700426
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800427 def send_msg():
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400428 try:
429 kafka_proxy = get_kafka_proxy()
430 if kafka_proxy and not kafka_proxy.is_faulty():
431 self.log.debug('kafka-proxy-available')
432 message['ts'] = arrow.utcnow().timestamp
433 self.log.debug('start-kafka-heartbeat')
434 kafka_proxy.send_message(topic, dumps(message))
435 else:
436 self.log.error('kafka-proxy-unavailable')
437 except Exception, e:
438 self.log.exception('failed-sending-message-heartbeat', e=e)
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800439
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400440 try:
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800441 lc = LoopingCall(send_msg)
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800442 lc.start(10)
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400443 except Exception, e:
444 self.log.exception('failed-kafka-heartbeat', e=e)
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700445
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800446
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700447if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700448 Main().start()