blob: 2262c946f5e013592b461c97de18f28e60ac67a0 [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 Harasztid7c7c482016-09-13 00:45:38 -070021import os
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070022import time
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070023
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070024import yaml
Zsolt Haraszti89a27302016-12-08 16:53:06 -080025from simplejson import dumps
Zsolt Harasztie060a7d2016-09-16 11:08:24 -070026from twisted.internet.defer import inlineCallbacks
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -080027from zope.interface import implementer
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070028
Zsolt Haraszti3bfff662016-12-14 23:41:49 -080029from common.event_bus import EventBusClient
30from common.manhole import Manhole
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070031from common.structlog_setup import setup_logging
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070032from common.utils.dockerhelpers import get_my_containers_name
33from common.utils.nethelpers import get_my_primary_interface, \
Khen Nursimulu441dedd2016-10-05 14:44:26 -070034 get_my_primary_local_ipv4
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080035from voltha.adapters.loader import AdapterLoader
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070036from voltha.coordinator import Coordinator
Zsolt Harasztidafefe12016-11-14 21:29:58 -080037from voltha.core.core import VolthaCore
Zsolt Haraszti99509d32016-12-10 16:41:45 -080038from voltha.northbound.diagnostics import Diagnostics
Zsolt Harasztieb435072016-09-23 17:10:49 -070039from voltha.northbound.grpc.grpc_server import VolthaGrpcServer
khenb95fe9a2016-10-05 11:15:25 -070040from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070041from voltha.northbound.rest.health_check import init_rest_service
Zsolt Haraszti66862032016-11-28 14:28:39 -080042from voltha.protos.common_pb2 import LogLevel
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -080043from voltha.registry import registry, IComponent
Zsolt Haraszti89a27302016-12-08 16:53:06 -080044from common.frameio.frameio import FrameIOManager
45
Zsolt Harasztidafefe12016-11-14 21:29:58 -080046
47VERSION = '0.9.0'
khenb95fe9a2016-10-05 11:15:25 -070048
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070049defs = dict(
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070050 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070051 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti109db832016-09-16 16:32:36 -070052 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
53 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070054 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070055 grpc_port=os.environ.get('GRPC_PORT', 50055),
56 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
57 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
58 get_my_primary_local_ipv4()),
59 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070060 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070061 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Haraszti3bfff662016-12-14 23:41:49 -080062 manhole_port=os.environ.get('MANHOLE_PORT', 12222),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070063)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070064
65
66def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070067 parser = argparse.ArgumentParser()
68
Zsolt Haraszti109db832016-09-16 16:32:36 -070069 _help = ('Path to voltha.yml config file (default: %s). '
70 'If relative, it is relative to main.py of voltha.'
71 % defs['config'])
72 parser.add_argument('-c', '--config',
73 dest='config',
74 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070075 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070076 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070077
Zsolt Haraszti109db832016-09-16 16:32:36 -070078 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
79 parser.add_argument(
80 '-C', '--consul', dest='consul', action='store',
81 default=defs['consul'],
82 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070083
Zsolt Haraszti109db832016-09-16 16:32:36 -070084 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
85 'the cluster (default: %s)' % defs['external_host_address'])
86 parser.add_argument('-E', '--external-host-address',
87 dest='external_host_address',
88 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070089 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070090 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070091
Zsolt Haraszti553826c2016-09-27 10:24:27 -070092 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
93 % defs['grpc_port'])
94 parser.add_argument('-g', '--grpc-port',
95 dest='grpc_port',
96 action='store',
97 default=defs['grpc_port'],
98 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -070099
Zsolt Haraszti109db832016-09-16 16:32:36 -0700100 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
101 'specified (None), the address from the config file is used'
102 % defs['fluentd'])
103 parser.add_argument('-F', '--fluentd',
104 dest='fluentd',
105 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700106 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700107 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700108
Zsolt Haraszti109db832016-09-16 16:32:36 -0700109 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
110 'cluster (default: %s)' % defs['internal_host_address'])
111 parser.add_argument('-H', '--internal-host-address',
112 dest='internal_host_address',
113 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700114 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700115 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700116
Zsolt Haraszti109db832016-09-16 16:32:36 -0700117 _help = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700118 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700119 parser.add_argument('-i', '--instance-id',
120 dest='instance_id',
121 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700122 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700123 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700124
125 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700126 _help = 'ETH interface to send (default: %s)' % defs['interface']
127 parser.add_argument('-I', '--interface',
128 dest='interface',
129 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700130 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700131 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700132
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800133 _help = 'open ssh manhole at given port'
134 parser.add_argument('-m', '--manhole-port',
135 dest='manhole_port',
136 action='store',
137 type=int,
138 default=None,
139 help=_help)
140
Zsolt Haraszti109db832016-09-16 16:32:36 -0700141 _help = 'omit startup banner log lines'
142 parser.add_argument('-n', '--no-banner',
143 dest='no_banner',
144 action='store_true',
145 default=False,
146 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700147
Zsolt Haraszti109db832016-09-16 16:32:36 -0700148 _help = 'do not emit periodic heartbeat log messages'
149 parser.add_argument('-N', '--no-heartbeat',
150 dest='no_heartbeat',
151 action='store_true',
152 default=False,
153 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700154
Zsolt Haraszti109db832016-09-16 16:32:36 -0700155 _help = ('port number for the rest service (default: %d)'
156 % defs['rest_port'])
157 parser.add_argument('-R', '--rest-port',
158 dest='rest_port',
159 action='store',
160 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700161 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700162 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700163
Zsolt Haraszti109db832016-09-16 16:32:36 -0700164 _help = "suppress debug and info logs"
165 parser.add_argument('-q', '--quiet',
166 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700167 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700168 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700169
Zsolt Haraszti109db832016-09-16 16:32:36 -0700170 _help = 'enable verbose logging'
171 parser.add_argument('-v', '--verbose',
172 dest='verbose',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700173 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700174 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700175
Zsolt Haraszti109db832016-09-16 16:32:36 -0700176 _help = ('use docker container name as voltha instance id'
177 ' (overrides -i/--instance-id option)')
178 parser.add_argument('--instance-id-is-container-name',
179 dest='instance_id_is_container_name',
180 action='store_true',
181 default=False,
182 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700183
khenb95fe9a2016-10-05 11:15:25 -0700184 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
185 'specified (None), the address from the config file is used'
186 % defs['kafka'])
187 parser.add_argument('-K', '--kafka',
188 dest='kafka',
189 action='store',
190 default=defs['kafka'],
191 help=_help)
192
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700193 args = parser.parse_args()
194
195 # post-processing
196
197 if args.instance_id_is_container_name:
198 args.instance_id = get_my_containers_name()
199
200 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700201
202
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700203def load_config(args):
204 path = args.config
205 if path.startswith('.'):
206 dir = os.path.dirname(os.path.abspath(__file__))
207 path = os.path.join(dir, path)
208 path = os.path.abspath(path)
209 with open(path) as fd:
210 config = yaml.load(fd)
211 return config
212
213
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700214def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700215 log.info(' _ ______ __ ________ _____ ')
216 log.info('| | / / __ \/ / /_ __/ / / / |')
217 log.info('| | / / / / / / / / / /_/ / /| |')
218 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
219 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700220 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700221
222
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800223@implementer(IComponent)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700224class Main(object):
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800225
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700226 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700227
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700228 self.args = args = parse_args()
229 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700230
231 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700232 self.log = setup_logging(self.config.get('logging', {}),
233 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700234 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700235 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700236
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400237 # configurable variables from voltha.yml file
238 #self.configurable_vars = self.config.get('Constants', {})
239
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700240 if not args.no_banner:
241 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700242
khenb95fe9a2016-10-05 11:15:25 -0700243 self.startup_components()
244
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700245 if not args.no_heartbeat:
246 self.start_heartbeat()
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800247 self.start_kafka_heartbeat(args.instance_id)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700248
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800249 self.manhole = None
250
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700251 def start(self):
252 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700253
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800254 def stop(self):
255 pass
256
257 def get_args(self):
258 """Allow access to command line args"""
259 return self.args
260
261 def get_config(self):
262 """Allow access to content of config file"""
263 return self.config
264
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800265 @inlineCallbacks
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700266 def startup_components(self):
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800267 try:
268 self.log.info('starting-internal-components')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800269
Zsolt Harasztia17f3ec2016-12-08 14:55:49 -0800270 registry.register('main', self)
271
Zsolt Haraszti66862032016-11-28 14:28:39 -0800272 yield registry.register(
273 'coordinator',
274 Coordinator(
275 internal_host_address=self.args.internal_host_address,
276 external_host_address=self.args.external_host_address,
277 rest_port=self.args.rest_port,
278 instance_id=self.args.instance_id,
279 config=self.config,
280 consul=self.args.consul)
281 ).start()
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800282
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800283 init_rest_service(self.args.rest_port)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700284
Zsolt Haraszti66862032016-11-28 14:28:39 -0800285 yield registry.register(
286 'grpc_server',
287 VolthaGrpcServer(self.args.grpc_port)
288 ).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700289
Zsolt Haraszti66862032016-11-28 14:28:39 -0800290 yield registry.register(
291 'kafka_proxy',
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800292 KafkaProxy(
293 self.args.consul,
294 self.args.kafka,
295 config=self.config.get('kafka-proxy', {})
296 )
Zsolt Haraszti66862032016-11-28 14:28:39 -0800297 ).start()
298
299 yield registry.register(
300 'core',
301 VolthaCore(
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800302 instance_id=self.args.instance_id,
303 version=VERSION,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800304 log_level=LogLevel.INFO
305 )
306 ).start()
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800307
Zsolt Haraszti66862032016-11-28 14:28:39 -0800308 yield registry.register(
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800309 'frameio',
310 FrameIOManager()
311 ).start()
312
313 yield registry.register(
Zsolt Haraszti66862032016-11-28 14:28:39 -0800314 'adapter_loader',
315 AdapterLoader(config=self.config.get('adapter_loader', {}))
316 ).start()
khenb95fe9a2016-10-05 11:15:25 -0700317
Zsolt Haraszti99509d32016-12-10 16:41:45 -0800318 yield registry.register(
319 'diag',
320 Diagnostics(config=self.config.get('diagnostics', {}))
321 ).start()
322
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800323 if self.args.manhole_port is not None:
324 self.start_manhole(self.args.manhole_port)
325
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800326 self.log.info('started-internal-services')
327
328 except Exception as e:
329 self.log.exception('Failure to start all components {}'.format(e))
330
Zsolt Haraszti3bfff662016-12-14 23:41:49 -0800331 def start_manhole(self, port):
332 self.manhole = Manhole(
333 port,
334 pws=dict(admin='adminpw'),
335 eventbus = EventBusClient(),
336 **registry.components
337 )
338
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700339 @inlineCallbacks
340 def shutdown_components(self):
341 """Execute before the reactor is shut down"""
342 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800343 for component in reversed(registry.iterate()):
344 yield component.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700345
Zsolt Haraszti3300f742017-01-09 01:14:20 -0800346 import threading
347 self.log.info('THREADS:')
348 main_thread = threading.current_thread()
349 for t in threading.enumerate():
350 if t is main_thread:
351 continue
352 if not t.isDaemon():
353 continue
354 self.log.info('joining thread {} {}'.format(
355 t.getName(), "daemon" if t.isDaemon() else "not-daemon"))
356 t.join()
357
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700358 def start_reactor(self):
359 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700360 reactor.callWhenRunning(
361 lambda: self.log.info('twisted-reactor-started'))
362 reactor.addSystemEventTrigger('before', 'shutdown',
363 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700364 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700365
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700366 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700367
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700368 t0 = time.time()
369 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700370
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700371 def heartbeat():
372 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700373
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700374 from twisted.internet.task import LoopingCall
375 lc = LoopingCall(heartbeat)
376 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700377
khenb95fe9a2016-10-05 11:15:25 -0700378 # Temporary function to send a heartbeat message to the external kafka
379 # broker
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800380 def start_kafka_heartbeat(self, instance_id):
khenb95fe9a2016-10-05 11:15:25 -0700381 # For heartbeat we will send a message to a specific "voltha-heartbeat"
382 # topic. The message is a protocol buf
383 # message
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800384 message = dumps(dict(
385 type='heartbeat',
386 voltha_instance=instance_id,
387 ip=get_my_primary_local_ipv4()
388 ))
389 topic = "heartbeat.voltha"
khenb95fe9a2016-10-05 11:15:25 -0700390
391 from twisted.internet.task import LoopingCall
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800392 kafka_proxy = get_kafka_proxy()
393 if kafka_proxy:
394 lc = LoopingCall(kafka_proxy.send_message, topic, message)
395 lc.start(10)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800396 pass
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800397 else:
398 self.log.error('Kafka proxy has not been created!')
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700399
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800400
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700401if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700402 Main().start()