blob: ac6205468f9c9bf173fddd2f522255fa28d1d294 [file] [log] [blame]
Zsolt Harasztib71c2a02016-09-12 13:12:07 -07001#!/usr/bin/env python
2#
3# Copyright 2016 the original author or authors.
4#
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 Harasztie060a7d2016-09-16 11:08:24 -070025from twisted.internet.defer import inlineCallbacks
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070026
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070027from common.structlog_setup import setup_logging
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070028from common.utils.dockerhelpers import get_my_containers_name
29from common.utils.nethelpers import get_my_primary_interface, \
Khen Nursimulu441dedd2016-10-05 14:44:26 -070030 get_my_primary_local_ipv4
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080031from voltha.adapters.loader import AdapterLoader
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070032from voltha.coordinator import Coordinator
Zsolt Harasztidafefe12016-11-14 21:29:58 -080033from voltha.core.core import VolthaCore
Zsolt Harasztieb435072016-09-23 17:10:49 -070034from voltha.northbound.grpc.grpc_server import VolthaGrpcServer
khenb95fe9a2016-10-05 11:15:25 -070035from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070036from voltha.northbound.rest.health_check import init_rest_service
Zsolt Haraszti66862032016-11-28 14:28:39 -080037from voltha.protos.common_pb2 import LogLevel
Zsolt Harasztidafefe12016-11-14 21:29:58 -080038from voltha.registry import registry
39
40VERSION = '0.9.0'
khenb95fe9a2016-10-05 11:15:25 -070041
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070042defs = dict(
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070043 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070044 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti109db832016-09-16 16:32:36 -070045 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
46 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070047 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070048 grpc_port=os.environ.get('GRPC_PORT', 50055),
49 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
50 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
51 get_my_primary_local_ipv4()),
52 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070053 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070054 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070055)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070056
57
58def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070059 parser = argparse.ArgumentParser()
60
Zsolt Haraszti109db832016-09-16 16:32:36 -070061 _help = ('Path to voltha.yml config file (default: %s). '
62 'If relative, it is relative to main.py of voltha.'
63 % defs['config'])
64 parser.add_argument('-c', '--config',
65 dest='config',
66 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070067 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070068 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070069
Zsolt Haraszti109db832016-09-16 16:32:36 -070070 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
71 parser.add_argument(
72 '-C', '--consul', dest='consul', action='store',
73 default=defs['consul'],
74 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070075
Zsolt Haraszti109db832016-09-16 16:32:36 -070076 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
77 'the cluster (default: %s)' % defs['external_host_address'])
78 parser.add_argument('-E', '--external-host-address',
79 dest='external_host_address',
80 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070081 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070082 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070083
Zsolt Haraszti553826c2016-09-27 10:24:27 -070084 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
85 % defs['grpc_port'])
86 parser.add_argument('-g', '--grpc-port',
87 dest='grpc_port',
88 action='store',
89 default=defs['grpc_port'],
90 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -070091
Zsolt Haraszti109db832016-09-16 16:32:36 -070092 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
93 'specified (None), the address from the config file is used'
94 % defs['fluentd'])
95 parser.add_argument('-F', '--fluentd',
96 dest='fluentd',
97 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070098 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070099 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700100
Zsolt Haraszti109db832016-09-16 16:32:36 -0700101 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
102 'cluster (default: %s)' % defs['internal_host_address'])
103 parser.add_argument('-H', '--internal-host-address',
104 dest='internal_host_address',
105 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700106 default=defs['internal_host_address'],
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 = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700110 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700111 parser.add_argument('-i', '--instance-id',
112 dest='instance_id',
113 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700114 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700115 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700116
117 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700118 _help = 'ETH interface to send (default: %s)' % defs['interface']
119 parser.add_argument('-I', '--interface',
120 dest='interface',
121 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700122 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700123 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700124
Zsolt Haraszti109db832016-09-16 16:32:36 -0700125 _help = 'omit startup banner log lines'
126 parser.add_argument('-n', '--no-banner',
127 dest='no_banner',
128 action='store_true',
129 default=False,
130 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700131
Zsolt Haraszti109db832016-09-16 16:32:36 -0700132 _help = 'do not emit periodic heartbeat log messages'
133 parser.add_argument('-N', '--no-heartbeat',
134 dest='no_heartbeat',
135 action='store_true',
136 default=False,
137 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700138
Zsolt Haraszti109db832016-09-16 16:32:36 -0700139 _help = ('port number for the rest service (default: %d)'
140 % defs['rest_port'])
141 parser.add_argument('-R', '--rest-port',
142 dest='rest_port',
143 action='store',
144 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700145 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700146 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700147
Zsolt Haraszti109db832016-09-16 16:32:36 -0700148 _help = "suppress debug and info logs"
149 parser.add_argument('-q', '--quiet',
150 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700151 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700152 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700153
Zsolt Haraszti109db832016-09-16 16:32:36 -0700154 _help = 'enable verbose logging'
155 parser.add_argument('-v', '--verbose',
156 dest='verbose',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700157 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700158 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700159
Zsolt Haraszti109db832016-09-16 16:32:36 -0700160 _help = ('use docker container name as voltha instance id'
161 ' (overrides -i/--instance-id option)')
162 parser.add_argument('--instance-id-is-container-name',
163 dest='instance_id_is_container_name',
164 action='store_true',
165 default=False,
166 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700167
khenb95fe9a2016-10-05 11:15:25 -0700168 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
169 'specified (None), the address from the config file is used'
170 % defs['kafka'])
171 parser.add_argument('-K', '--kafka',
172 dest='kafka',
173 action='store',
174 default=defs['kafka'],
175 help=_help)
176
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700177 args = parser.parse_args()
178
179 # post-processing
180
181 if args.instance_id_is_container_name:
182 args.instance_id = get_my_containers_name()
183
184 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700185
186
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700187def load_config(args):
188 path = args.config
189 if path.startswith('.'):
190 dir = os.path.dirname(os.path.abspath(__file__))
191 path = os.path.join(dir, path)
192 path = os.path.abspath(path)
193 with open(path) as fd:
194 config = yaml.load(fd)
195 return config
196
197
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700198def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700199 log.info(' _ ______ __ ________ _____ ')
200 log.info('| | / / __ \/ / /_ __/ / / / |')
201 log.info('| | / / / / / / / / / /_/ / /| |')
202 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
203 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700204 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700205
206
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700207class Main(object):
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700208 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700209
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700210 self.args = args = parse_args()
211 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700212
213 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700214 self.log = setup_logging(self.config.get('logging', {}),
215 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700216 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700217 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700218
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400219 # configurable variables from voltha.yml file
220 #self.configurable_vars = self.config.get('Constants', {})
221
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700222 if not args.no_banner:
223 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700224
khenb95fe9a2016-10-05 11:15:25 -0700225 self.startup_components()
226
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700227 if not args.no_heartbeat:
228 self.start_heartbeat()
khenb95fe9a2016-10-05 11:15:25 -0700229 self.start_kafka_heartbeat()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700230
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700231 def start(self):
232 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700233
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800234 @inlineCallbacks
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700235 def startup_components(self):
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800236 try:
237 self.log.info('starting-internal-components')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800238
Zsolt Haraszti66862032016-11-28 14:28:39 -0800239 yield registry.register(
240 'coordinator',
241 Coordinator(
242 internal_host_address=self.args.internal_host_address,
243 external_host_address=self.args.external_host_address,
244 rest_port=self.args.rest_port,
245 instance_id=self.args.instance_id,
246 config=self.config,
247 consul=self.args.consul)
248 ).start()
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800249
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800250 init_rest_service(self.args.rest_port)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700251
Zsolt Haraszti66862032016-11-28 14:28:39 -0800252 yield registry.register(
253 'grpc_server',
254 VolthaGrpcServer(self.args.grpc_port)
255 ).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700256
Zsolt Haraszti66862032016-11-28 14:28:39 -0800257 yield registry.register(
258 'kafka_proxy',
259 KafkaProxy(self.args.consul, self.args.kafka)
260 ).start()
261
262 yield registry.register(
263 'core',
264 VolthaCore(
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800265 instance_id=self.args.instance_id,
266 version=VERSION,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800267 log_level=LogLevel.INFO
268 )
269 ).start()
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800270
Zsolt Haraszti66862032016-11-28 14:28:39 -0800271 yield registry.register(
272 'adapter_loader',
273 AdapterLoader(config=self.config.get('adapter_loader', {}))
274 ).start()
khenb95fe9a2016-10-05 11:15:25 -0700275
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800276 self.log.info('started-internal-services')
277
278 except Exception as e:
279 self.log.exception('Failure to start all components {}'.format(e))
280
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700281 @inlineCallbacks
282 def shutdown_components(self):
283 """Execute before the reactor is shut down"""
284 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Harasztidafefe12016-11-14 21:29:58 -0800285 for component in reversed(registry.iterate()):
286 yield component.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700287
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700288 def start_reactor(self):
289 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700290 reactor.callWhenRunning(
291 lambda: self.log.info('twisted-reactor-started'))
292 reactor.addSystemEventTrigger('before', 'shutdown',
293 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700294 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700295
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700296 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700297
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700298 t0 = time.time()
299 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700300
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700301 def heartbeat():
302 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700303
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700304 from twisted.internet.task import LoopingCall
305 lc = LoopingCall(heartbeat)
306 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700307
khenb95fe9a2016-10-05 11:15:25 -0700308 # Temporary function to send a heartbeat message to the external kafka
309 # broker
310 def start_kafka_heartbeat(self):
311 # For heartbeat we will send a message to a specific "voltha-heartbeat"
312 # topic. The message is a protocol buf
313 # message
314 message = 'Heartbeat message:{}'.format(get_my_primary_local_ipv4())
315 topic = "voltha-heartbeat"
316
317 from twisted.internet.task import LoopingCall
Khen Nursimulu3c74d3b2016-11-08 15:14:01 -0800318 kafka_proxy = get_kafka_proxy()
319 if kafka_proxy:
320 lc = LoopingCall(kafka_proxy.send_message, topic, message)
321 lc.start(10)
322 else:
323 self.log.error('Kafka proxy has not been created!')
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700324
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700325if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700326 Main().start()