blob: f28cdbe9433d32b5cdbd786a6d01708705f12e20 [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 Harasztieb435072016-09-23 17:10:49 -070033from voltha.northbound.grpc.grpc_server import VolthaGrpcServer
khenb95fe9a2016-10-05 11:15:25 -070034from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070035from voltha.northbound.rest.health_check import init_rest_service
khenb95fe9a2016-10-05 11:15:25 -070036
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070037defs = dict(
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070038 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070039 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti109db832016-09-16 16:32:36 -070040 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
41 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070042 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070043 grpc_port=os.environ.get('GRPC_PORT', 50055),
44 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
45 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
46 get_my_primary_local_ipv4()),
47 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070048 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070049 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070050)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070051
52
53def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070054 parser = argparse.ArgumentParser()
55
Zsolt Haraszti109db832016-09-16 16:32:36 -070056 _help = ('Path to voltha.yml config file (default: %s). '
57 'If relative, it is relative to main.py of voltha.'
58 % defs['config'])
59 parser.add_argument('-c', '--config',
60 dest='config',
61 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070062 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070063 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070064
Zsolt Haraszti109db832016-09-16 16:32:36 -070065 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
66 parser.add_argument(
67 '-C', '--consul', dest='consul', action='store',
68 default=defs['consul'],
69 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070070
Zsolt Haraszti109db832016-09-16 16:32:36 -070071 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
72 'the cluster (default: %s)' % defs['external_host_address'])
73 parser.add_argument('-E', '--external-host-address',
74 dest='external_host_address',
75 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070076 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070077 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070078
Zsolt Haraszti553826c2016-09-27 10:24:27 -070079 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
80 % defs['grpc_port'])
81 parser.add_argument('-g', '--grpc-port',
82 dest='grpc_port',
83 action='store',
84 default=defs['grpc_port'],
85 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -070086
Zsolt Haraszti109db832016-09-16 16:32:36 -070087 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
88 'specified (None), the address from the config file is used'
89 % defs['fluentd'])
90 parser.add_argument('-F', '--fluentd',
91 dest='fluentd',
92 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070093 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070094 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070095
Zsolt Haraszti109db832016-09-16 16:32:36 -070096 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
97 'cluster (default: %s)' % defs['internal_host_address'])
98 parser.add_argument('-H', '--internal-host-address',
99 dest='internal_host_address',
100 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700101 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700102 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700103
Zsolt Haraszti109db832016-09-16 16:32:36 -0700104 _help = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700105 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700106 parser.add_argument('-i', '--instance-id',
107 dest='instance_id',
108 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700109 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700110 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700111
112 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700113 _help = 'ETH interface to send (default: %s)' % defs['interface']
114 parser.add_argument('-I', '--interface',
115 dest='interface',
116 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700117 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700118 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700119
Zsolt Haraszti109db832016-09-16 16:32:36 -0700120 _help = 'omit startup banner log lines'
121 parser.add_argument('-n', '--no-banner',
122 dest='no_banner',
123 action='store_true',
124 default=False,
125 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700126
Zsolt Haraszti109db832016-09-16 16:32:36 -0700127 _help = 'do not emit periodic heartbeat log messages'
128 parser.add_argument('-N', '--no-heartbeat',
129 dest='no_heartbeat',
130 action='store_true',
131 default=False,
132 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700133
Zsolt Haraszti109db832016-09-16 16:32:36 -0700134 _help = ('port number for the rest service (default: %d)'
135 % defs['rest_port'])
136 parser.add_argument('-R', '--rest-port',
137 dest='rest_port',
138 action='store',
139 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700140 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700141 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700142
Zsolt Haraszti109db832016-09-16 16:32:36 -0700143 _help = "suppress debug and info logs"
144 parser.add_argument('-q', '--quiet',
145 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700146 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700147 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700148
Zsolt Haraszti109db832016-09-16 16:32:36 -0700149 _help = 'enable verbose logging'
150 parser.add_argument('-v', '--verbose',
151 dest='verbose',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700152 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700153 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700154
Zsolt Haraszti109db832016-09-16 16:32:36 -0700155 _help = ('use docker container name as voltha instance id'
156 ' (overrides -i/--instance-id option)')
157 parser.add_argument('--instance-id-is-container-name',
158 dest='instance_id_is_container_name',
159 action='store_true',
160 default=False,
161 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700162
khenb95fe9a2016-10-05 11:15:25 -0700163 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
164 'specified (None), the address from the config file is used'
165 % defs['kafka'])
166 parser.add_argument('-K', '--kafka',
167 dest='kafka',
168 action='store',
169 default=defs['kafka'],
170 help=_help)
171
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700172 args = parser.parse_args()
173
174 # post-processing
175
176 if args.instance_id_is_container_name:
177 args.instance_id = get_my_containers_name()
178
179 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700180
181
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700182def load_config(args):
183 path = args.config
184 if path.startswith('.'):
185 dir = os.path.dirname(os.path.abspath(__file__))
186 path = os.path.join(dir, path)
187 path = os.path.abspath(path)
188 with open(path) as fd:
189 config = yaml.load(fd)
190 return config
191
192
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700193def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700194 log.info(' _ ______ __ ________ _____ ')
195 log.info('| | / / __ \/ / /_ __/ / / / |')
196 log.info('| | / / / / / / / / / /_/ / /| |')
197 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
198 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700199 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700200
201
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700202class Main(object):
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700203 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700204
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700205 self.args = args = parse_args()
206 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700207
208 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700209 self.log = setup_logging(self.config.get('logging', {}),
210 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700211 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700212 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700213
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400214 # configurable variables from voltha.yml file
215 #self.configurable_vars = self.config.get('Constants', {})
216
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700217 # components
218 self.coordinator = None
Zsolt Harasztieb435072016-09-23 17:10:49 -0700219 self.grpc_server = None
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700220 self.kafka_proxy = None
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800221 self.adapter_loader = None
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700222
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700223 if not args.no_banner:
224 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700225
khenb95fe9a2016-10-05 11:15:25 -0700226 self.startup_components()
227
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700228 if not args.no_heartbeat:
229 self.start_heartbeat()
khenb95fe9a2016-10-05 11:15:25 -0700230 self.start_kafka_heartbeat()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700231
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700232 def start(self):
233 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700234
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800235 @inlineCallbacks
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700236 def startup_components(self):
237 self.log.info('starting-internal-components')
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800238 self.coordinator = yield Coordinator(
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700239 internal_host_address=self.args.internal_host_address,
240 external_host_address=self.args.external_host_address,
241 rest_port=self.args.rest_port,
242 instance_id=self.args.instance_id,
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400243 config=self.config,
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700244 consul=self.args.consul).start()
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700245 init_rest_service(self.args.rest_port)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700246
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800247 self.grpc_server = yield VolthaGrpcServer(self.args.grpc_port).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700248
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700249 # initialize kafka proxy singleton
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800250 self.kafka_proxy = yield KafkaProxy(self.args.consul, self.args.kafka)
251
252 # adapter loader
253 self.adapter_loader = yield AdapterLoader(
254 config=self.config.get('adapter_loader', {})).start()
khenb95fe9a2016-10-05 11:15:25 -0700255
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700256 self.log.info('started-internal-services')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700257
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700258 @inlineCallbacks
259 def shutdown_components(self):
260 """Execute before the reactor is shut down"""
261 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800262 if self.adapter_loader is not None:
263 yield self.adapter_loader.stop()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700264 if self.coordinator is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700265 yield self.coordinator.stop()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700266 if self.grpc_server is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700267 yield self.grpc_server.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700268
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700269 def start_reactor(self):
270 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700271 reactor.callWhenRunning(
272 lambda: self.log.info('twisted-reactor-started'))
273 reactor.addSystemEventTrigger('before', 'shutdown',
274 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700275 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700276
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700277 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700278
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700279 t0 = time.time()
280 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700281
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700282 def heartbeat():
283 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700284
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700285 from twisted.internet.task import LoopingCall
286 lc = LoopingCall(heartbeat)
287 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700288
khenb95fe9a2016-10-05 11:15:25 -0700289 # Temporary function to send a heartbeat message to the external kafka
290 # broker
291 def start_kafka_heartbeat(self):
292 # For heartbeat we will send a message to a specific "voltha-heartbeat"
293 # topic. The message is a protocol buf
294 # message
295 message = 'Heartbeat message:{}'.format(get_my_primary_local_ipv4())
296 topic = "voltha-heartbeat"
297
298 from twisted.internet.task import LoopingCall
299 lc = LoopingCall(get_kafka_proxy().send_message, topic, message)
300 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700301
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700302
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700303if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700304 Main().start()