2 # -*- coding: utf-8 -*-
5 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
6 # This file is part of openvim
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: nfvlabs@tid.es
26 This is the main program of openvim, it reads the configuration
27 and launches the rest of threads: http clients, openflow controller
31 __author__
="Alfonso Tierno"
32 __date__
="$10-jul-2014 12:07:15$"
33 __version__
="0.5.0-r506"
34 version_date
="Oct 2016"
35 database_version
="0.8" #expected database schema version
38 import auxiliary_functions
as af
45 from jsonschema
import validate
as js_v
, exceptions
as js_e
46 import host_thread
as ht
47 import dhcp_thread
as dt
48 import openflow_thread
as oft
50 from vim_schema
import config_schema
52 import logging
.handlers
as log_handlers
58 logger
= logging
.getLogger('vim')
60 class LoadConfigurationException(Exception):
63 def load_configuration(configuration_file
):
64 default_tokens
={'http_port':9080, 'http_host':'localhost',
65 'of_controller_nets_with_same_vlan':True,
66 'image_path':'/opt/VNF/images',
67 'network_vlan_range_start':1000,
68 'network_vlan_range_end': 4096,
70 'log_level_db': "ERROR",
71 'log_level_of': 'ERROR',
74 #First load configuration from configuration file
75 #Check config file exists
76 if not os
.path
.isfile(configuration_file
):
77 return (False, "Configuration file '"+configuration_file
+"' does not exists")
80 (return_status
, code
) = af
.read_file(configuration_file
)
82 return (return_status
, "Error loading configuration file '"+configuration_file
+"': "+code
)
84 config
= yaml
.load(code
)
85 except yaml
.YAMLError
, exc
:
87 if hasattr(exc
, 'problem_mark'):
88 mark
= exc
.problem_mark
89 error_pos
= " at position: (%s:%s)" % (mark
.line
+1, mark
.column
+1)
90 return (False, "Error loading configuration file '"+configuration_file
+"'"+error_pos
+": content format error: Failed to parse yaml format")
94 js_v(config
, config_schema
)
95 except js_e
.ValidationError
, exc
:
97 if len(exc
.path
)>0: error_pos
=" at '" + ":".join(map(str, exc
.path
))+"'"
98 return False, "Error loading configuration file '"+configuration_file
+"'"+error_pos
+": "+exc
.message
101 #Check default values tokens
102 for k
,v
in default_tokens
.items():
103 if k
not in config
: config
[k
]=v
105 if config
["network_vlan_range_start"]+10 >= config
["network_vlan_range_end"]:
106 return False, "Error invalid network_vlan_range less than 10 elements"
109 return (False, "Error loading configuration file '"+configuration_file
+"': "+str(e
))
110 return (True, config
)
112 def create_database_connection(config_dic
):
113 db
= vim_db
.vim_db( (config_dic
["network_vlan_range_start"],config_dic
["network_vlan_range_end"]), config_dic
['log_level_db'] );
114 if db
.connect(config_dic
['db_host'], config_dic
['db_user'], config_dic
['db_passwd'], config_dic
['db_name']) == -1:
115 logger
.error("Cannot connect to database %s at %s@%s", config_dic
['db_name'], config_dic
['db_user'], config_dic
['db_host'])
120 print "Usage: ", sys
.argv
[0], "[options]"
121 print " -v|--version: prints current version"
122 print " -c|--config FILE: loads the configuration file (default: openvimd.cfg)"
123 print " -h|--help: shows this help"
124 print " -p|--port PORT: changes port number and overrides the port number in the configuration file (default: 908)"
125 print " -P|--adminport PORT: changes admin port number and overrides the port number in the configuration file (default: not listen)"
126 print " --dbname NAME: changes db_name and overrides the db_name in the configuration file"
127 #print( " --log-socket-host HOST: send logs to this host")
128 #print( " --log-socket-port PORT: send logs using this port (default: 9022)")
129 print( " --log-file FILE: send logs to this file")
133 if __name__
=="__main__":
134 hostname
= socket
.gethostname()
135 #streamformat = "%(levelname)s (%(module)s:%(lineno)d) %(message)s"
136 log_formatter_complete
= logging
.Formatter(
137 '%(asctime)s.%(msecs)03d00Z[{host}@openmanod] %(filename)s:%(lineno)s severity:%(levelname)s logger:%(name)s log:%(message)s'.format(host
=hostname
),
138 datefmt
='%Y-%m-%dT%H:%M:%S',
140 log_format_simple
= "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
141 log_formatter_simple
= logging
.Formatter(log_format_simple
, datefmt
='%Y-%m-%dT%H:%M:%S')
142 logging
.basicConfig(format
=log_format_simple
, level
= logging
.DEBUG
)
143 logger
= logging
.getLogger('openmano')
144 logger
.setLevel(logging
.DEBUG
)
146 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvc:p:P:", ["config=", "help", "version", "port=", "adminport=", "log-file=", "dbname="])
147 except getopt
.GetoptError
, err
:
148 # print help information and exit:
149 logger
.error("%s. Type -h for help", err
) # will print something like "option -a not recognized"
155 config_file
= 'openvimd.cfg'
160 if o
in ("-v", "--version"):
161 print "openvimd version", __version__
, version_date
162 print "(c) Copyright Telefonica"
164 elif o
in ("-h", "--help"):
167 elif o
in ("-c", "--config"):
169 elif o
in ("-p", "--port"):
171 elif o
in ("-P", "--adminport"):
173 elif o
in ("-P", "--dbname"):
175 elif o
== "--log-file":
178 assert False, "Unhandled option"
182 #Load configuration file
183 r
, config_dic
= load_configuration(config_file
)
186 logger
.error(config_dic
)
191 file_handler
= logging
.handlers
.RotatingFileHandler(log_file
, maxBytes
=100e6
, backupCount
=9, delay
=0)
192 file_handler
.setFormatter(log_formatter_simple
)
193 logger
.addHandler(file_handler
)
194 #logger.debug("moving logs to '%s'", global_config["log_file"])
195 #remove initial stream handler
196 logging
.root
.removeHandler(logging
.root
.handlers
[0])
197 print ("logging on '{}'".format(log_file
))
199 raise LoadConfigurationException("Cannot open logging file '{}': {}. Check folder exist and permissions".format(log_file
, str(e
)) )
201 logger
.setLevel(getattr(logging
, config_dic
['log_level']))
202 logger
.critical("Starting openvim server command: '%s'", sys
.argv
[0])
203 #override parameters obtained by command line
205 config_dic
['http_port'] = port
207 config_dic
['http_admin_port'] = port_admin
209 config_dic
['db_name'] = db_name
212 if 'mode' not in config_dic
:
213 config_dic
['mode'] = 'normal'
214 #allow backward compatibility of test_mode option
215 if 'test_mode' in config_dic
and config_dic
['test_mode']==True:
216 config_dic
['mode'] = 'test'
217 if config_dic
['mode'] == 'development' and ( 'development_bridge' not in config_dic
or config_dic
['development_bridge'] not in config_dic
.get("bridge_ifaces",None) ):
218 logger
.error("'%s' is not a valid 'development_bridge', not one of the 'bridge_ifaces'", config_file
)
221 if config_dic
['mode'] != 'normal':
222 print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
223 print "!! Warning, openvimd in TEST mode '%s'" % config_dic
['mode']
224 print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
225 config_dic
['version'] = __version__
228 db_http
= create_database_connection(config_dic
)
229 r
= db_http
.get_db_version()
231 logger
.error("DATABASE is not a VIM one or it is a '0.0' version. Try to upgrade to version '%s' with './database_utils/migrate_vim_db.sh'", database_version
)
233 elif r
[1]!=database_version
:
234 logger
.error("DATABASE wrong version '%s'. Try to upgrade/downgrade to version '%s' with './database_utils/migrate_vim_db.sh'", r
[1], database_version
)
236 db_of
= create_database_connection(config_dic
)
237 db_lock
= threading
.Lock()
238 config_dic
['db'] = db_of
239 config_dic
['db_lock'] = db_lock
241 #precreate interfaces; [bridge:<host_bridge_name>, VLAN used at Host, uuid of network camping in this bridge, speed in Gbit/s
242 config_dic
['dhcp_nets']=[]
243 config_dic
['bridge_nets']=[]
244 for bridge
,vlan_speed
in config_dic
["bridge_ifaces"].items():
245 #skip 'development_bridge'
246 if config_dic
['mode'] == 'development' and config_dic
['development_bridge'] == bridge
:
248 config_dic
['bridge_nets'].append( [bridge
, vlan_speed
[0], vlan_speed
[1], None] )
249 del config_dic
["bridge_ifaces"]
251 #check if this bridge is already used (present at database) for a network)
253 for brnet
in config_dic
['bridge_nets']:
254 r
,nets
= db_of
.get_table(SELECT
=('uuid',), FROM
='nets',WHERE
={'provider': "bridge:"+brnet
[0]})
256 brnet
[3] = nets
[0]['uuid']
257 used_bridge_nets
.append(brnet
[0])
258 if config_dic
.get("dhcp_server"):
259 if brnet
[0] in config_dic
["dhcp_server"]["bridge_ifaces"]:
260 config_dic
['dhcp_nets'].append(nets
[0]['uuid'])
261 if len(used_bridge_nets
) > 0 :
262 logger
.info("found used bridge nets: " + ",".join(used_bridge_nets
))
263 #get nets used by dhcp
264 if config_dic
.get("dhcp_server"):
265 for net
in config_dic
["dhcp_server"].get("nets", () ):
266 r
,nets
= db_of
.get_table(SELECT
=('uuid',), FROM
='nets',WHERE
={'name': net
})
268 config_dic
['dhcp_nets'].append(nets
[0]['uuid'])
270 # get host list from data base before starting threads
271 r
,hosts
= db_of
.get_table(SELECT
=('name','ip_name','user','uuid'), FROM
='hosts', WHERE
={'status':'ok'})
273 logger
.error("Cannot get hosts from database %s", hosts
)
275 # create connector to the openflow controller
276 of_test_mode
= False if config_dic
['mode']=='normal' or config_dic
['mode']=="OF only" else True
279 OF_conn
= oft
.of_test_connector({"of_debug": config_dic
['log_level_of']} )
281 #load other parameters starting by of_ from config dict in a temporal dict
282 temp_dict
={ "of_ip": config_dic
['of_controller_ip'],
283 "of_port": config_dic
['of_controller_port'],
284 "of_dpid": config_dic
['of_controller_dpid'],
285 "of_debug": config_dic
['log_level_of']
287 for k
,v
in config_dic
.iteritems():
288 if type(k
) is str and k
[0:3]=="of_" and k
[0:13] != "of_controller":
290 if config_dic
['of_controller']=='opendaylight':
292 elif "of_controller_module" in config_dic
:
293 module
= config_dic
["of_controller_module"]
295 module
= config_dic
['of_controller']
298 module_info
= imp
.find_module(module
)
300 OF_conn
= imp
.load_module("OF_conn", *module_info
)
302 OF_conn
= OF_conn
.OF_conn(temp_dict
)
303 except Exception as e
:
304 logger
.error("Cannot open the Openflow controller '%s': %s", type(e
).__name
__, str(e
))
305 if module_info
and module_info
[0]:
306 file.close(module_info
[0])
308 except (IOError, ImportError) as e
:
309 if module_info
and module_info
[0]:
310 file.close(module_info
[0])
311 logger
.error("Cannot open openflow controller module '%s'; %s: %s; revise 'of_controller' field of configuration file.", module
, type(e
).__name
__, str(e
))
315 #create openflow thread
316 thread
= oft
.openflow_thread(OF_conn
, of_test
=of_test_mode
, db
=db_of
, db_lock
=db_lock
,
317 pmp_with_same_vlan
=config_dic
['of_controller_nets_with_same_vlan'],
318 debug
=config_dic
['log_level_of'])
319 r
,c
= thread
.OF_connector
.obtain_port_correspondence()
321 logger
.error("Cannot get openflow information %s", c
)
324 config_dic
['of_thread'] = thread
326 #create dhcp_server thread
327 host_test_mode
= True if config_dic
['mode']=='test' or config_dic
['mode']=="OF only" else False
328 dhcp_params
= config_dic
.get("dhcp_server")
330 thread
= dt
.dhcp_thread(dhcp_params
=dhcp_params
, test
=host_test_mode
, dhcp_nets
=config_dic
["dhcp_nets"], db
=db_of
, db_lock
=db_lock
, debug
=config_dic
['log_level_of'])
332 config_dic
['dhcp_thread'] = thread
335 #Create one thread for each host
336 host_test_mode
= True if config_dic
['mode']=='test' or config_dic
['mode']=="OF only" else False
337 host_develop_mode
= True if config_dic
['mode']=='development' else False
338 host_develop_bridge_iface
= config_dic
.get('development_bridge', None)
339 config_dic
['host_threads'] = {}
341 host
['image_path'] = '/opt/VNF/images/openvim'
342 thread
= ht
.host_thread(name
=host
['name'], user
=host
['user'], host
=host
['ip_name'], db
=db_of
, db_lock
=db_lock
,
343 test
=host_test_mode
, image_path
=config_dic
['image_path'], version
=config_dic
['version'],
344 host_id
=host
['uuid'], develop_mode
=host_develop_mode
, develop_bridge_iface
=host_develop_bridge_iface
)
346 config_dic
['host_threads'][ host
['uuid'] ] = thread
350 #Create thread to listen to web requests
351 http_thread
= httpserver
.httpserver(db_http
, 'http', config_dic
['http_host'], config_dic
['http_port'], False, config_dic
)
354 if 'http_admin_port' in config_dic
:
355 db_http
= create_database_connection(config_dic
)
356 http_thread_admin
= httpserver
.httpserver(db_http
, 'http-admin', config_dic
['http_host'], config_dic
['http_admin_port'], True)
357 http_thread_admin
.start()
359 http_thread_admin
= None
361 logger
.info('Waiting for http clients')
362 print ('openvimd ready')
363 print ('====================')
366 #TODO: Interactive console would be nice here instead of join or sleep
368 r
="help" #force print help at the beginning
373 print "type 'exit' for terminate"
376 except (KeyboardInterrupt, SystemExit):
380 except getopt
.GetoptError
as e
:
381 logger
.critical(str(e
)) # will print something like "option -a not recognized"
384 except LoadConfigurationException
as e
:
385 logger
.critical(str(e
))
388 logger
.info('Exiting openvimd')
389 threads
= config_dic
.get('host_threads', {})
390 if 'of_thread' in config_dic
:
391 threads
['of'] = (config_dic
['of_thread'])
392 if 'dhcp_thread' in config_dic
:
393 threads
['dhcp'] = (config_dic
['dhcp_thread'])
395 for thread
in threads
.values():
396 thread
.insert_task("exit")
397 for thread
in threads
.values():
400 #if http_thread_admin is not None:
401 #http_thread_admin.join()
402 logger
.debug( "bye!")