blob: fa2b5c03c986ee99fc80e4e5fe8328b4e5454ae0 [file] [log] [blame]
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001#!/usr/bin/env python
2#
3# Copyright 2017 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#
17import argparse
18import os
19
20import yaml
21from twisted.internet import reactor
22from twisted.internet.defer import inlineCallbacks
23
William Kurkianfc0dcda2019-04-08 16:54:36 -040024from pyvoltha.common.structlog_setup import setup_logging
25from pyvoltha.common.utils.dockerhelpers import get_my_containers_name
26from pyvoltha.common.utils.nethelpers import get_my_primary_local_ipv4
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050027from connection_mgr import ConnectionManager
28
29defs = dict(
30 config=os.environ.get('CONFIG', './ofagent.yml'),
31 consul=os.environ.get('CONSUL', 'localhost:8500'),
32 controller=os.environ.get('CONTROLLER', 'localhost:6653'),
33 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
34 get_my_primary_local_ipv4()),
35 grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'),
Richard Jankowski46464e92019-03-05 11:53:55 -050036 grpc_timeout=os.environ.get('GRPC_TIMEOUT', '10'),
37 core_binding_key=os.environ.get('CORE_BINDING_KEY', 'voltha_backend_name'),
khenaidoo43aa6bd2019-05-29 13:35:13 -040038 core_transaction_key=os.environ.get('CORE_TRANSACTION_KEY', 'voltha_serial_number'),
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050039 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
40 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
41 get_my_primary_local_ipv4()),
42 work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent'),
43 key_file=os.environ.get('KEY_FILE', '/ofagent/pki/voltha.key'),
44 cert_file=os.environ.get('CERT_FILE', '/ofagent/pki/voltha.crt')
45)
46
47
48def parse_args():
49
50 parser = argparse.ArgumentParser()
51
52 _help = ('Path to ofagent.yml config file (default: %s). '
53 'If relative, it is relative to main.py of ofagent.'
54 % defs['config'])
55 parser.add_argument('-c', '--config',
56 dest='config',
57 action='store',
58 default=defs['config'],
59 help=_help)
60
61 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
62 parser.add_argument(
63 '-C', '--consul', dest='consul', action='store',
64 default=defs['consul'],
65 help=_help)
66
67 _help = '<hostname1>:<port1> <hostname2>:<port2> <hostname3>:<port3> ... <hostnamen>:<portn> to openflow controller (default: %s)' % \
68 defs['controller']
69 parser.add_argument(
70 '-O', '--controller',nargs = '*', dest='controller', action='store',
71 default=defs['controller'],
72 help=_help)
73
74 _help = ('<hostname> or <ip> at which ofagent is reachable from outside '
75 'the cluster (default: %s)' % defs['external_host_address'])
76 parser.add_argument('-E', '--external-host-address',
77 dest='external_host_address',
78 action='store',
79 default=defs['external_host_address'],
80 help=_help)
81
Richard Jankowski46464e92019-03-05 11:53:55 -050082 _help = ('gRPC end-point to connect to. It can either be a direct '
83 'definition in the form of <hostname>:<port>, or it can be an '
84 'indirect definition in the form of @<service-name> where '
85 '<service-name> is the name of the grpc service as registered '
86 'in consul (example: @voltha-grpc). (default: %s)'
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050087 % defs['grpc_endpoint'])
88 parser.add_argument('-G', '--grpc-endpoint',
89 dest='grpc_endpoint',
90 action='store',
91 default=defs['grpc_endpoint'],
92 help=_help)
93
Richard Jankowski46464e92019-03-05 11:53:55 -050094 _help = 'gRPC timeout in seconds (default: %s)' % defs['grpc_timeout']
95 parser.add_argument('-T', '--grpc-timeout',
96 dest='grpc_timeout',
97 action='store',
98 default=defs['grpc_timeout'],
99 help=_help)
100
101 _help = ('The name of the meta-key whose value is the rw-core group '
102 'to which the ofagent\'s gRPC client is bound. '
103 '(default: %s)' % defs['core_binding_key'])
104 parser.add_argument('-B', '--core-binding-key',
105 dest='core_binding_key',
106 action='store',
107 default=defs['core_binding_key'],
108 help=_help)
109
khenaidoo43aa6bd2019-05-29 13:35:13 -0400110 _help = ('The name of the meta-key whose value is the transaction ID '
111 'used by the OFAgent to send requests to the Voltha RW Core. '
112 '(default: %s)' % defs['core_transaction_key'])
113 parser.add_argument('-ctk', '--core_transaction_key',
114 dest='core_transaction_key',
115 action='store',
116 default=defs['core_transaction_key'],
117 help=_help)
118
Richard Jankowski46464e92019-03-05 11:53:55 -0500119 _help = ('<hostname> or <ip> at which ofagent is reachable from inside '
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500120 'the cluster (default: %s)' % defs['internal_host_address'])
121 parser.add_argument('-H', '--internal-host-address',
122 dest='internal_host_address',
123 action='store',
124 default=defs['internal_host_address'],
125 help=_help)
126
127 _help = ('unique string id of this ofagent instance (default: %s)'
128 % defs['instance_id'])
129 parser.add_argument('-i', '--instance-id',
130 dest='instance_id',
131 action='store',
132 default=defs['instance_id'],
133 help=_help)
134
135 _help = 'omit startup banner log lines'
136 parser.add_argument('-n', '--no-banner',
137 dest='no_banner',
138 action='store_true',
139 default=False,
140 help=_help)
141
142 _help = "suppress debug and info logs"
143 parser.add_argument('-q', '--quiet',
144 dest='quiet',
145 action='count',
146 help=_help)
147
148 _help = 'enable verbose logging'
149 parser.add_argument('-v', '--verbose',
150 dest='verbose',
151 action='count',
152 help=_help)
153
154 _help = ('work dir to compile and assemble generated files (default=%s)'
155 % defs['work_dir'])
156 parser.add_argument('-w', '--work-dir',
157 dest='work_dir',
158 action='store',
159 default=defs['work_dir'],
160 help=_help)
161
162 _help = ('use docker container name as ofagent instance id'
163 ' (overrides -i/--instance-id option)')
164 parser.add_argument('--instance-id-is-container-name',
165 dest='instance_id_is_container_name',
166 action='store_true',
167 default=False,
168 help=_help)
169
170 _help = ('Specify this option to enable TLS security between ofagent \
171 and onos.')
172 parser.add_argument('-t', '--enable-tls',
173 dest='enable_tls',
174 action='store_true',
175 help=_help)
176
177 _help = ('key file to be used for tls security (default=%s)'
178 % defs['key_file'])
179 parser.add_argument('-k', '--key-file',
180 dest='key_file',
181 action='store',
182 default=defs['key_file'],
183 help=_help)
184
185 _help = ('certificate file to be used for tls security (default=%s)'
186 % defs['cert_file'])
187 parser.add_argument('-r', '--cert-file',
188 dest='cert_file',
189 action='store',
190 default=defs['cert_file'],
191 help=_help)
192
193 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
201
202
203def 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
213banner = r'''
214 ___ _____ _ _
215 / _ \| ___/ \ __ _ ___ _ __ | |_
216| | | | |_ / _ \ / _` |/ _ \ '_ \| __|
217| |_| | _/ ___ \ (_| | __/ | | | |_
218 \___/|_|/_/ \_\__, |\___|_| |_|\__|
219 |___/
220'''
221
222def print_banner(log):
223 for line in banner.strip('\n').splitlines():
224 log.info(line)
225 log.info('(to stop: press Ctrl-C)')
226
227
228class Main(object):
229
230 def __init__(self):
231
232 self.args = args = parse_args()
233 self.config = load_config(args)
Richard Jankowski46464e92019-03-05 11:53:55 -0500234 self.grpc_timeout = int(self.args.grpc_timeout)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500235
236 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
237 self.log = setup_logging(self.config.get('logging', {}),
238 args.instance_id,
239 verbosity_adjust=verbosity_adjust)
240
241 # components
242 self.connection_manager = None
243
244 self.exiting = False
245
246 if not args.no_banner:
247 print_banner(self.log)
248
249 self.startup_components()
250
251 def start(self):
252 self.start_reactor() # will not return except Keyboard interrupt
253
254 @inlineCallbacks
255 def startup_components(self):
256 self.log.info('starting-internal-components')
257 args = self.args
258 self.connection_manager = yield ConnectionManager(
khenaidoo43aa6bd2019-05-29 13:35:13 -0400259 args.consul,
260 args.grpc_endpoint,
261 self.grpc_timeout,
262 args.core_binding_key,
263 args.core_transaction_key,
264 args.controller,
265 args.instance_id,
266 args.enable_tls,
267 args.key_file,
268 args.cert_file).start()
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500269 self.log.info('started-internal-services')
270
271 @inlineCallbacks
272 def shutdown_components(self):
273 """Execute before the reactor is shut down"""
274 self.log.info('exiting-on-keyboard-interrupt')
275 self.exiting = True
276 if self.connection_manager is not None:
277 yield self.connection_manager.stop()
278
279 def start_reactor(self):
280 reactor.callWhenRunning(
281 lambda: self.log.info('twisted-reactor-started'))
282
283 reactor.addSystemEventTrigger('before', 'shutdown',
284 self.shutdown_components)
285 reactor.run()
286
287
288if __name__ == '__main__':
289 Main().start()