blob: 23335bb803ab260de3b1b1c19edbf895efa7a869 [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#!/usr/bin/env python
2#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08003# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -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#
Khen Nursimulu68b9be32016-10-25 11:57:04 -040017import argparse
Zsolt Haraszti8a774382016-10-24 18:25:54 -070018import os
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070019
Zsolt Haraszti8a774382016-10-24 18:25:54 -070020import yaml
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070021from twisted.internet import reactor
Khen Nursimulu68b9be32016-10-25 11:57:04 -040022from twisted.internet.defer import inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070023
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070024from common.structlog_setup import setup_logging
Khen Nursimulu68b9be32016-10-25 11:57:04 -040025from common.utils.dockerhelpers import get_my_containers_name
26from common.utils.nethelpers import get_my_primary_local_ipv4
Khen Nursimulu68b9be32016-10-25 11:57:04 -040027from connection_mgr import ConnectionManager
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070028
Khen Nursimulu68b9be32016-10-25 11:57:04 -040029defs = dict(
30 config=os.environ.get('CONFIG', './ofagent.yml'),
31 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Harasztiee5c4c82017-01-09 14:37:57 -080032 controller=os.environ.get('CONTROLLER', 'localhost:6653'),
Khen Nursimulu68b9be32016-10-25 11:57:04 -040033 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'),
Khen Nursimulu68b9be32016-10-25 11:57:04 -040036 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
37 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
38 get_my_primary_local_ipv4()),
Girishf6eeaea2017-11-13 10:53:57 +053039 work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent'),
40 key_file=os.environ.get('KEY_FILE', '/ofagent/pki/voltha.key'),
41 cert_file=os.environ.get('CERT_FILE', '/ofagent/pki/voltha.crt')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040042)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070043
44
Khen Nursimulu68b9be32016-10-25 11:57:04 -040045def parse_args():
46
47 parser = argparse.ArgumentParser()
48
49 _help = ('Path to ofagent.yml config file (default: %s). '
50 'If relative, it is relative to main.py of ofagent.'
51 % defs['config'])
52 parser.add_argument('-c', '--config',
53 dest='config',
54 action='store',
55 default=defs['config'],
56 help=_help)
57
58 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
59 parser.add_argument(
60 '-C', '--consul', dest='consul', action='store',
61 default=defs['consul'],
62 help=_help)
63
sgovindacc736782017-05-02 20:06:37 +053064 _help = '<hostname1>:<port1> <hostname2>:<port2> <hostname3>:<port3> ... <hostnamen>:<portn> to openflow controller (default: %s)' % \
Khen Nursimulu68b9be32016-10-25 11:57:04 -040065 defs['controller']
66 parser.add_argument(
sgovindacc736782017-05-02 20:06:37 +053067 '-O', '--controller',nargs = '*', dest='controller', action='store',
Khen Nursimulu68b9be32016-10-25 11:57:04 -040068 default=defs['controller'],
69 help=_help)
70
71 _help = ('<hostname> or <ip> at which ofagent 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',
76 default=defs['external_host_address'],
77 help=_help)
78
Khen Nursimulu68b9be32016-10-25 11:57:04 -040079 _help = ('gRPC end-point to connect to. It can either be a direct'
80 'definition in the form of <hostname>:<port>, or it can be an'
81 'indirect definition in the form of @<service-name> where'
82 '<service-name> is the name of the grpc service as registered'
83 'in consul (example: @voltha-grpc). (default: %s'
84 % defs['grpc_endpoint'])
85 parser.add_argument('-G', '--grpc-endpoint',
86 dest='grpc_endpoint',
87 action='store',
88 default=defs['grpc_endpoint'],
89 help=_help)
90
91 _help = ('<hostname> or <ip> at which ofagent is reachable from inside'
92 'the cluster (default: %s)' % defs['internal_host_address'])
93 parser.add_argument('-H', '--internal-host-address',
94 dest='internal_host_address',
95 action='store',
96 default=defs['internal_host_address'],
97 help=_help)
98
99 _help = ('unique string id of this ofagent instance (default: %s)'
100 % defs['instance_id'])
101 parser.add_argument('-i', '--instance-id',
102 dest='instance_id',
103 action='store',
104 default=defs['instance_id'],
105 help=_help)
106
107 _help = 'omit startup banner log lines'
108 parser.add_argument('-n', '--no-banner',
109 dest='no_banner',
110 action='store_true',
111 default=False,
112 help=_help)
113
114 _help = "suppress debug and info logs"
115 parser.add_argument('-q', '--quiet',
116 dest='quiet',
117 action='count',
118 help=_help)
119
120 _help = 'enable verbose logging'
121 parser.add_argument('-v', '--verbose',
122 dest='verbose',
123 action='count',
124 help=_help)
125
126 _help = ('work dir to compile and assemble generated files (default=%s)'
127 % defs['work_dir'])
128 parser.add_argument('-w', '--work-dir',
129 dest='work_dir',
130 action='store',
131 default=defs['work_dir'],
132 help=_help)
133
134 _help = ('use docker container name as ofagent instance id'
135 ' (overrides -i/--instance-id option)')
136 parser.add_argument('--instance-id-is-container-name',
137 dest='instance_id_is_container_name',
138 action='store_true',
139 default=False,
140 help=_help)
141
Girishf6eeaea2017-11-13 10:53:57 +0530142 _help = ('Specify this option to enable TLS security between ofagent \
143 and onos.')
144 parser.add_argument('-t', '--enable-tls',
145 dest='enable_tls',
146 action='store_true',
147 help=_help)
148
149 _help = ('key file to be used for tls security (default=%s)'
150 % defs['key_file'])
151 parser.add_argument('-k', '--key-file',
152 dest='key_file',
153 action='store',
154 default=defs['key_file'],
155 help=_help)
156
157 _help = ('certificate file to be used for tls security (default=%s)'
158 % defs['cert_file'])
159 parser.add_argument('-r', '--cert-file',
160 dest='cert_file',
161 action='store',
162 default=defs['cert_file'],
163 help=_help)
164
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400165 args = parser.parse_args()
166
167 # post-processing
168
169 if args.instance_id_is_container_name:
170 args.instance_id = get_my_containers_name()
171
172 return args
173
174
175def load_config(args):
176 path = args.config
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700177 if path.startswith('.'):
178 dir = os.path.dirname(os.path.abspath(__file__))
179 path = os.path.join(dir, path)
180 path = os.path.abspath(path)
181 with open(path) as fd:
182 config = yaml.load(fd)
183 return config
184
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400185banner = r'''
186 ___ _____ _ _
187 / _ \| ___/ \ __ _ ___ _ __ | |_
188| | | | |_ / _ \ / _` |/ _ \ '_ \| __|
189| |_| | _/ ___ \ (_| | __/ | | | |_
190 \___/|_|/_/ \_\__, |\___|_| |_|\__|
191 |___/
192'''
193
194def print_banner(log):
195 for line in banner.strip('\n').splitlines():
196 log.info(line)
197 log.info('(to stop: press Ctrl-C)')
198
199
200class Main(object):
201
202 def __init__(self):
203
204 self.args = args = parse_args()
205 self.config = load_config(args)
206
207 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
208 self.log = setup_logging(self.config.get('logging', {}),
209 args.instance_id,
khenaidoo50b286d2018-03-02 17:44:30 -0500210 verbosity_adjust=verbosity_adjust)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400211
212 # components
213 self.connection_manager = None
214
215 self.exiting = False
216
217 if not args.no_banner:
218 print_banner(self.log)
219
220 self.startup_components()
221
222 def start(self):
223 self.start_reactor() # will not return except Keyboard interrupt
224
225 @inlineCallbacks
226 def startup_components(self):
227 self.log.info('starting-internal-components')
228 args = self.args
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700229 self.connection_manager = yield ConnectionManager(
Richard Jankowskic9d89202018-01-25 10:25:10 -0500230 args.consul, args.grpc_endpoint, args.controller, args.instance_id, \
Girishf6eeaea2017-11-13 10:53:57 +0530231 args.enable_tls, args.key_file, args.cert_file).start()
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400232 self.log.info('started-internal-services')
233
234 @inlineCallbacks
235 def shutdown_components(self):
236 """Execute before the reactor is shut down"""
237 self.log.info('exiting-on-keyboard-interrupt')
238 self.exiting = True
239 if self.connection_manager is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700240 yield self.connection_manager.stop()
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400241
242 def start_reactor(self):
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400243 reactor.callWhenRunning(
244 lambda: self.log.info('twisted-reactor-started'))
245
246 reactor.addSystemEventTrigger('before', 'shutdown',
247 self.shutdown_components)
248 reactor.run()
249
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700250
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700251if __name__ == '__main__':
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400252 Main().start()