blob: 9eb12d48487ba9b650d09811baf6ea70b91d4246 [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'),
khenaidooccc42252017-07-06 23:00:49 -040055 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', None),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070056 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070057 grpc_port=os.environ.get('GRPC_PORT', 50055),
58 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
khenaidooccc42252017-07-06 23:00:49 -040059 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070060 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070061 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070062 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Haraszti3bfff662016-12-14 23:41:49 -080063 manhole_port=os.environ.get('MANHOLE_PORT', 12222),
Ryan Van Gilder5a0ab622017-03-01 16:39:25 -080064 backend=os.environ.get('BACKEND', 'none'),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070065)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070066
67
68def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070069 parser = argparse.ArgumentParser()
70
Zsolt Haraszti109db832016-09-16 16:32:36 -070071 _help = ('Path to voltha.yml config file (default: %s). '
72 'If relative, it is relative to main.py of voltha.'
73 % defs['config'])
74 parser.add_argument('-c', '--config',
75 dest='config',
76 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070077 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070078 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070079
Zsolt Haraszti109db832016-09-16 16:32:36 -070080 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
81 parser.add_argument(
82 '-C', '--consul', dest='consul', action='store',
83 default=defs['consul'],
84 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070085
Zsolt Haraszti109db832016-09-16 16:32:36 -070086 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
87 'the cluster (default: %s)' % defs['external_host_address'])
88 parser.add_argument('-E', '--external-host-address',
89 dest='external_host_address',
90 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070091 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070092 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070093
Zsolt Haraszti553826c2016-09-27 10:24:27 -070094 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
95 % defs['grpc_port'])
96 parser.add_argument('-g', '--grpc-port',
97 dest='grpc_port',
98 action='store',
99 default=defs['grpc_port'],
100 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700101
Zsolt Haraszti109db832016-09-16 16:32:36 -0700102 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
103 'specified (None), the address from the config file is used'
104 % defs['fluentd'])
105 parser.add_argument('-F', '--fluentd',
106 dest='fluentd',
107 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700108 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700109 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700110
Zsolt Haraszti109db832016-09-16 16:32:36 -0700111 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
112 'cluster (default: %s)' % defs['internal_host_address'])
113 parser.add_argument('-H', '--internal-host-address',
114 dest='internal_host_address',
115 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700116 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700117 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700118
Zsolt Haraszti109db832016-09-16 16:32:36 -0700119 _help = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700120 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700121 parser.add_argument('-i', '--instance-id',
122 dest='instance_id',
123 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700124 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700125 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700126
khenaidooccc42252017-07-06 23:00:49 -0400127 _help = 'ETH interface to recieve (default: %s)' % defs['interface']
Zsolt Haraszti109db832016-09-16 16:32:36 -0700128 parser.add_argument('-I', '--interface',
129 dest='interface',
130 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700131 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700132 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700133
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800134 _help = 'open ssh manhole at given port'
135 parser.add_argument('-m', '--manhole-port',
136 dest='manhole_port',
137 action='store',
138 type=int,
139 default=None,
140 help=_help)
141
Zsolt Haraszti109db832016-09-16 16:32:36 -0700142 _help = 'omit startup banner log lines'
143 parser.add_argument('-n', '--no-banner',
144 dest='no_banner',
145 action='store_true',
146 default=False,
147 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700148
Zsolt Haraszti109db832016-09-16 16:32:36 -0700149 _help = 'do not emit periodic heartbeat log messages'
150 parser.add_argument('-N', '--no-heartbeat',
151 dest='no_heartbeat',
152 action='store_true',
153 default=False,
154 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700155
Zsolt Haraszti109db832016-09-16 16:32:36 -0700156 _help = ('port number for the rest service (default: %d)'
157 % defs['rest_port'])
158 parser.add_argument('-R', '--rest-port',
159 dest='rest_port',
160 action='store',
161 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700162 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700163 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700164
Zsolt Haraszti109db832016-09-16 16:32:36 -0700165 _help = "suppress debug and info logs"
166 parser.add_argument('-q', '--quiet',
167 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700168 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700169 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700170
Zsolt Haraszti109db832016-09-16 16:32:36 -0700171 _help = 'enable verbose logging'
172 parser.add_argument('-v', '--verbose',
173 dest='verbose',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700174 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700175 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700176
Zsolt Haraszti109db832016-09-16 16:32:36 -0700177 _help = ('use docker container name as voltha instance id'
178 ' (overrides -i/--instance-id option)')
179 parser.add_argument('--instance-id-is-container-name',
180 dest='instance_id_is_container_name',
181 action='store_true',
182 default=False,
183 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700184
khenb95fe9a2016-10-05 11:15:25 -0700185 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
186 'specified (None), the address from the config file is used'
187 % defs['kafka'])
188 parser.add_argument('-K', '--kafka',
189 dest='kafka',
190 action='store',
191 default=defs['kafka'],
192 help=_help)
193
Ryan Van Gilder5a0ab622017-03-01 16:39:25 -0800194 _help = 'backend to use for config persitence'
195 parser.add_argument('-b', '--backend',
196 default=defs['backend'],
197 choices=['none', 'consul'],
198 help=_help)
199
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700200 args = parser.parse_args()
201
202 # post-processing
203
204 if args.instance_id_is_container_name:
205 args.instance_id = get_my_containers_name()
206
khenaidooccc42252017-07-06 23:00:49 -0400207 m_ip = get_my_primary_local_ipv4(args.interface)
208 if not m_ip:
209 m_ip = get_my_primary_local_ipv4()
210 if not args.external_host_address:
211 args.external_host_address = m_ip
212 if not args.internal_host_address:
213 args.internal_host_address = m_ip
214
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700215 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700216
217
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700218def load_config(args):
219 path = args.config
220 if path.startswith('.'):
221 dir = os.path.dirname(os.path.abspath(__file__))
222 path = os.path.join(dir, path)
223 path = os.path.abspath(path)
224 with open(path) as fd:
225 config = yaml.load(fd)
226 return config
227
228
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700229def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700230 log.info(' _ ______ __ ________ _____ ')
231 log.info('| | / / __ \/ / /_ __/ / / / |')
232 log.info('| | / / / / / / / / / /_/ / /| |')
233 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
234 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700235 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700236
237
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800238@implementer(IComponent)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700239class Main(object):
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800240
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700241 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700242
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700243 self.args = args = parse_args()
244 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700245
246 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700247 self.log = setup_logging(self.config.get('logging', {}),
248 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700249 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700250 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700251
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400252 # configurable variables from voltha.yml file
253 #self.configurable_vars = self.config.get('Constants', {})
254
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700255 if not args.no_banner:
256 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700257
khenaidoo032d3302017-06-09 14:50:04 -0400258 # Create a unique instnce id using the passed-in instanceid and
259 # UTC timestamp
260 current_time = arrow.utcnow().timestamp
261 self.instance_id = self.args.instance_id + '_' + str(current_time)
262
263 # Every voltha instance is given a core_storage id where the
264 # instance data is stored
265 self.core_store_id = None
266
khenb95fe9a2016-10-05 11:15:25 -0700267 self.startup_components()
268
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700269 if not args.no_heartbeat:
270 self.start_heartbeat()
khenaidoo032d3302017-06-09 14:50:04 -0400271 self.start_kafka_heartbeat(self.instance_id)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700272
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800273 self.manhole = None
274
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700275 def start(self):
276 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700277
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800278 def stop(self):
279 pass
280
281 def get_args(self):
282 """Allow access to command line args"""
283 return self.args
284
285 def get_config(self):
286 """Allow access to content of config file"""
287 return self.config
288
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800289 @inlineCallbacks
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700290 def startup_components(self):
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800291 try:
khenaidooccc42252017-07-06 23:00:49 -0400292 self.log.info('starting-internal-components',
293 internal_host=self.args.internal_host_address,
294 external_host=self.args.external_host_address)
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800295
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800296 registry.register('main', self)
297
Zsolt Haraszti66862032016-11-28 14:28:39 -0800298 yield registry.register(
299 'coordinator',
300 Coordinator(
301 internal_host_address=self.args.internal_host_address,
302 external_host_address=self.args.external_host_address,
303 rest_port=self.args.rest_port,
khenaidoo032d3302017-06-09 14:50:04 -0400304 instance_id=self.instance_id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800305 config=self.config,
306 consul=self.args.consul)
307 ).start()
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800308
khenaidoo032d3302017-06-09 14:50:04 -0400309 self.log.info('waiting-for-config-assignment')
310
311 # Wait until we get a config id before we proceed
312 self.core_store_id, store_prefix = \
313 yield registry('coordinator').get_core_store_id_and_prefix()
314
315 self.log.info('store-id', core_store_id=self.core_store_id)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700316
Zsolt Haraszti66862032016-11-28 14:28:39 -0800317 yield registry.register(
318 'grpc_server',
319 VolthaGrpcServer(self.args.grpc_port)
320 ).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700321
Zsolt Haraszti66862032016-11-28 14:28:39 -0800322 yield registry.register(
khenaidoo032d3302017-06-09 14:50:04 -0400323 'core',
324 VolthaCore(
325 instance_id=self.instance_id,
326 core_store_id = self.core_store_id,
khenaidoo08d48d22017-06-29 19:42:49 -0400327 grpc_port=self.args.grpc_port,
khenaidoo032d3302017-06-09 14:50:04 -0400328 version=VERSION,
329 log_level=LogLevel.INFO
330 )
331 ).start(config_backend=load_backend(store_id=self.core_store_id,
332 store_prefix=store_prefix,
333 args=self.args))
334
335 init_rest_service(self.args.rest_port)
336
337 yield registry.register(
Zsolt Haraszti66862032016-11-28 14:28:39 -0800338 'kafka_proxy',
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800339 KafkaProxy(
340 self.args.consul,
341 self.args.kafka,
342 config=self.config.get('kafka-proxy', {})
343 )
Zsolt Haraszti66862032016-11-28 14:28:39 -0800344 ).start()
345
346 yield registry.register(
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800347 'frameio',
348 FrameIOManager()
349 ).start()
350
351 yield registry.register(
Zsolt Haraszti66862032016-11-28 14:28:39 -0800352 'adapter_loader',
353 AdapterLoader(config=self.config.get('adapter_loader', {}))
354 ).start()
khenb95fe9a2016-10-05 11:15:25 -0700355
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800356 yield registry.register(
357 'diag',
358 Diagnostics(config=self.config.get('diagnostics', {}))
359 ).start()
360
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800361 if self.args.manhole_port is not None:
362 self.start_manhole(self.args.manhole_port)
363
khenaidoo032d3302017-06-09 14:50:04 -0400364 # Now that all components are loaded, in the scenario where this
365 # voltha instance is picking up an existing set of data (from a
366 # voltha instance that dies/stopped) then we need to setup this
367 # instance from where the previous one left
368
369 yield registry('core').reconcile_data()
370
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800371 self.log.info('started-internal-services')
372
373 except Exception as e:
khenaidoo032d3302017-06-09 14:50:04 -0400374 self.log.exception('Failure-to-start-all-components', e=e)
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800375
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800376 def start_manhole(self, port):
377 self.manhole = Manhole(
378 port,
379 pws=dict(admin='adminpw'),
380 eventbus = EventBusClient(),
381 **registry.components
382 )
383
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700384 @inlineCallbacks
385 def shutdown_components(self):
386 """Execute before the reactor is shut down"""
387 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800388 for component in reversed(registry.iterate()):
389 yield component.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700390
Zsolt Haraszti3300f742017-01-09 01:14:20 -0800391 import threading
392 self.log.info('THREADS:')
393 main_thread = threading.current_thread()
394 for t in threading.enumerate():
395 if t is main_thread:
396 continue
397 if not t.isDaemon():
398 continue
399 self.log.info('joining thread {} {}'.format(
400 t.getName(), "daemon" if t.isDaemon() else "not-daemon"))
401 t.join()
402
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700403 def start_reactor(self):
404 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700405 reactor.callWhenRunning(
406 lambda: self.log.info('twisted-reactor-started'))
407 reactor.addSystemEventTrigger('before', 'shutdown',
408 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700409 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700410
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700411 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700412
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700413 t0 = time.time()
414 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700415
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700416 def heartbeat():
417 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700418
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700419 lc = LoopingCall(heartbeat)
420 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700421
khenb95fe9a2016-10-05 11:15:25 -0700422 # Temporary function to send a heartbeat message to the external kafka
423 # broker
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800424 def start_kafka_heartbeat(self, instance_id):
khenb95fe9a2016-10-05 11:15:25 -0700425 # For heartbeat we will send a message to a specific "voltha-heartbeat"
426 # topic. The message is a protocol buf
427 # message
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800428 message = dict(
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800429 type='heartbeat',
430 voltha_instance=instance_id,
431 ip=get_my_primary_local_ipv4()
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800432 )
433 topic = "voltha.heartbeat"
khenb95fe9a2016-10-05 11:15:25 -0700434
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800435 def send_msg():
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400436 try:
437 kafka_proxy = get_kafka_proxy()
438 if kafka_proxy and not kafka_proxy.is_faulty():
439 self.log.debug('kafka-proxy-available')
440 message['ts'] = arrow.utcnow().timestamp
441 self.log.debug('start-kafka-heartbeat')
442 kafka_proxy.send_message(topic, dumps(message))
443 else:
444 self.log.error('kafka-proxy-unavailable')
445 except Exception, e:
446 self.log.exception('failed-sending-message-heartbeat', e=e)
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800447
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400448 try:
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800449 lc = LoopingCall(send_msg)
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800450 lc.start(10)
Rouzbahan Rashidi-Tabrizi380dcb32017-03-23 18:01:02 -0400451 except Exception, e:
452 self.log.exception('failed-kafka-heartbeat', e=e)
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700453
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800454
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700455if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700456 Main().start()