blob: c4ed67b7c5568a7bcba92232aac4209ac944bab9 [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 Harasztid70cd4d2016-11-03 23:23:36 -070031from voltha.coordinator import Coordinator
Zsolt Harasztieb435072016-09-23 17:10:49 -070032from voltha.northbound.grpc.grpc_server import VolthaGrpcServer
khenb95fe9a2016-10-05 11:15:25 -070033from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070034from voltha.northbound.rest.health_check import init_rest_service
khenb95fe9a2016-10-05 11:15:25 -070035
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070036defs = dict(
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070037 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070038 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti109db832016-09-16 16:32:36 -070039 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
40 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070041 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070042 grpc_port=os.environ.get('GRPC_PORT', 50055),
43 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
44 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
45 get_my_primary_local_ipv4()),
46 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070047 rest_port=os.environ.get('REST_PORT', 8880),
khenb95fe9a2016-10-05 11:15:25 -070048 kafka=os.environ.get('KAFKA', 'localhost:9092'),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070049)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070050
51
52def parse_args():
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070053 parser = argparse.ArgumentParser()
54
Zsolt Haraszti109db832016-09-16 16:32:36 -070055 _help = ('Path to voltha.yml config file (default: %s). '
56 'If relative, it is relative to main.py of voltha.'
57 % defs['config'])
58 parser.add_argument('-c', '--config',
59 dest='config',
60 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070061 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070062 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070063
Zsolt Haraszti109db832016-09-16 16:32:36 -070064 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
65 parser.add_argument(
66 '-C', '--consul', dest='consul', action='store',
67 default=defs['consul'],
68 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070069
Zsolt Haraszti109db832016-09-16 16:32:36 -070070 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
71 'the cluster (default: %s)' % defs['external_host_address'])
72 parser.add_argument('-E', '--external-host-address',
73 dest='external_host_address',
74 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070075 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070076 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070077
Zsolt Haraszti553826c2016-09-27 10:24:27 -070078 _help = ('port number of the GRPC service exposed by voltha (default: %s)'
79 % defs['grpc_port'])
80 parser.add_argument('-g', '--grpc-port',
81 dest='grpc_port',
82 action='store',
83 default=defs['grpc_port'],
84 help=_help)
Khen Nursimulu441dedd2016-10-05 14:44:26 -070085
Zsolt Haraszti109db832016-09-16 16:32:36 -070086 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
87 'specified (None), the address from the config file is used'
88 % defs['fluentd'])
89 parser.add_argument('-F', '--fluentd',
90 dest='fluentd',
91 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070092 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070093 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070094
Zsolt Haraszti109db832016-09-16 16:32:36 -070095 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
96 'cluster (default: %s)' % defs['internal_host_address'])
97 parser.add_argument('-H', '--internal-host-address',
98 dest='internal_host_address',
99 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700100 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700101 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700102
Zsolt Haraszti109db832016-09-16 16:32:36 -0700103 _help = ('unique string id of this voltha instance (default: %s)'
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700104 % defs['instance_id'])
Zsolt Haraszti109db832016-09-16 16:32:36 -0700105 parser.add_argument('-i', '--instance-id',
106 dest='instance_id',
107 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700108 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700109 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700110
111 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700112 _help = 'ETH interface to send (default: %s)' % defs['interface']
113 parser.add_argument('-I', '--interface',
114 dest='interface',
115 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700116 default=defs['interface'],
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 = 'omit startup banner log lines'
120 parser.add_argument('-n', '--no-banner',
121 dest='no_banner',
122 action='store_true',
123 default=False,
124 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700125
Zsolt Haraszti109db832016-09-16 16:32:36 -0700126 _help = 'do not emit periodic heartbeat log messages'
127 parser.add_argument('-N', '--no-heartbeat',
128 dest='no_heartbeat',
129 action='store_true',
130 default=False,
131 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700132
Zsolt Haraszti109db832016-09-16 16:32:36 -0700133 _help = ('port number for the rest service (default: %d)'
134 % defs['rest_port'])
135 parser.add_argument('-R', '--rest-port',
136 dest='rest_port',
137 action='store',
138 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700139 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700140 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700141
Zsolt Haraszti109db832016-09-16 16:32:36 -0700142 _help = "suppress debug and info logs"
143 parser.add_argument('-q', '--quiet',
144 dest='quiet',
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700145 action='count',
Zsolt Haraszti109db832016-09-16 16:32:36 -0700146 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700147
Zsolt Haraszti109db832016-09-16 16:32:36 -0700148 _help = 'enable verbose logging'
149 parser.add_argument('-v', '--verbose',
150 dest='verbose',
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 = ('use docker container name as voltha instance id'
155 ' (overrides -i/--instance-id option)')
156 parser.add_argument('--instance-id-is-container-name',
157 dest='instance_id_is_container_name',
158 action='store_true',
159 default=False,
160 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700161
khenb95fe9a2016-10-05 11:15:25 -0700162 _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not '
163 'specified (None), the address from the config file is used'
164 % defs['kafka'])
165 parser.add_argument('-K', '--kafka',
166 dest='kafka',
167 action='store',
168 default=defs['kafka'],
169 help=_help)
170
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700171 args = parser.parse_args()
172
173 # post-processing
174
175 if args.instance_id_is_container_name:
176 args.instance_id = get_my_containers_name()
177
178 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700179
180
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700181def load_config(args):
182 path = args.config
183 if path.startswith('.'):
184 dir = os.path.dirname(os.path.abspath(__file__))
185 path = os.path.join(dir, path)
186 path = os.path.abspath(path)
187 with open(path) as fd:
188 config = yaml.load(fd)
189 return config
190
191
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700192def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700193 log.info(' _ ______ __ ________ _____ ')
194 log.info('| | / / __ \/ / /_ __/ / / / |')
195 log.info('| | / / / / / / / / / /_/ / /| |')
196 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
197 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700198 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700199
200
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700201class Main(object):
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700202 def __init__(self):
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700203
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700204 self.args = args = parse_args()
205 self.config = load_config(args)
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700206
207 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700208 self.log = setup_logging(self.config.get('logging', {}),
209 args.instance_id,
Zsolt Haraszti1420def2016-09-18 00:07:31 -0700210 verbosity_adjust=verbosity_adjust,
Zsolt Haraszti109db832016-09-16 16:32:36 -0700211 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700212
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400213 # configurable variables from voltha.yml file
214 #self.configurable_vars = self.config.get('Constants', {})
215
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700216 # components
217 self.coordinator = None
Zsolt Harasztieb435072016-09-23 17:10:49 -0700218 self.grpc_server = None
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700219 self.kafka_proxy = None
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700220
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700221 if not args.no_banner:
222 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700223
khenb95fe9a2016-10-05 11:15:25 -0700224 self.startup_components()
225
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700226 if not args.no_heartbeat:
227 self.start_heartbeat()
khenb95fe9a2016-10-05 11:15:25 -0700228 self.start_kafka_heartbeat()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700229
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700230 def start(self):
231 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700232
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700233 def startup_components(self):
234 self.log.info('starting-internal-components')
235 self.coordinator = Coordinator(
236 internal_host_address=self.args.internal_host_address,
237 external_host_address=self.args.external_host_address,
238 rest_port=self.args.rest_port,
239 instance_id=self.args.instance_id,
Rouzbahan Rashidi-Tabrizi1c3eba82016-10-27 21:47:18 -0400240 config=self.config,
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700241 consul=self.args.consul).start()
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700242 init_rest_service(self.args.rest_port)
Zsolt Harasztieb435072016-09-23 17:10:49 -0700243
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700244 self.grpc_server = VolthaGrpcServer(self.args.grpc_port).start()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700245
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700246 # initialize kafka proxy singleton
khenb95fe9a2016-10-05 11:15:25 -0700247 self.kafka_proxy = KafkaProxy(self.args.consul, self.args.kafka)
248
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700249 self.log.info('started-internal-services')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700250
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700251 @inlineCallbacks
252 def shutdown_components(self):
253 """Execute before the reactor is shut down"""
254 self.log.info('exiting-on-keyboard-interrupt')
Zsolt Harasztieb435072016-09-23 17:10:49 -0700255 if self.coordinator is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700256 yield self.coordinator.stop()
Zsolt Harasztieb435072016-09-23 17:10:49 -0700257 if self.grpc_server is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700258 yield self.grpc_server.stop()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700259
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700260 def start_reactor(self):
261 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700262 reactor.callWhenRunning(
263 lambda: self.log.info('twisted-reactor-started'))
264 reactor.addSystemEventTrigger('before', 'shutdown',
265 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700266 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700267
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700268 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700269
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700270 t0 = time.time()
271 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700272
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700273 def heartbeat():
274 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700275
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700276 from twisted.internet.task import LoopingCall
277 lc = LoopingCall(heartbeat)
278 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700279
khenb95fe9a2016-10-05 11:15:25 -0700280 # Temporary function to send a heartbeat message to the external kafka
281 # broker
282 def start_kafka_heartbeat(self):
283 # For heartbeat we will send a message to a specific "voltha-heartbeat"
284 # topic. The message is a protocol buf
285 # message
286 message = 'Heartbeat message:{}'.format(get_my_primary_local_ipv4())
287 topic = "voltha-heartbeat"
288
289 from twisted.internet.task import LoopingCall
290 lc = LoopingCall(get_kafka_proxy().send_message, topic, message)
291 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700292
Khen Nursimulu441dedd2016-10-05 14:44:26 -0700293
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700294if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700295 Main().start()