- '''update a network_id into the database.'''
- my = config_dic['http_threads'][ threading.current_thread().name ]
- #parse input data
- http_content = format_in( network_update_schema )
- r = remove_extra_items(http_content, network_update_schema)
- change_keys_http2db(http_content['network'], http2db_network)
- network=http_content['network']
-
- #Look for the previous data
- where_ = {'uuid': network_id}
- result, network_old = my.db.get_table(FROM='nets', WHERE=where_)
- if result < 0:
- print "http_put_network_id error %d %s" % (result, network_old)
- bottle.abort(-result, network_old)
- return
- elif result==0:
- print "http_put_network_id network '%s' not found" % network_id
- bottle.abort(HTTP_Not_Found, 'network %s not found' % network_id)
- return
- #get ports
- nbports, content = my.db.get_table(FROM='ports', SELECT=('uuid as port_id',),
- WHERE={'net_id': network_id}, LIMIT=100)
- if result < 0:
- print "http_put_network_id error %d %s" % (result, network_old)
- bottle.abort(-result, content)
- return
- if nbports>0:
- if 'type' in network and network['type'] != network_old[0]['type']:
- bottle.abort(HTTP_Method_Not_Allowed, "Can not change type of network while having ports attached")
- if 'vlan' in network and network['vlan'] != network_old[0]['vlan']:
- bottle.abort(HTTP_Method_Not_Allowed, "Can not change vlan of network while having ports attached")
-
- #check valid params
- net_provider = network.get('provider', network_old[0]['provider'])
- net_type = network.get('type', network_old[0]['type'])
- net_bind_net = network.get("bind_net")
- net_bind_type= network.get("bind_type")
- if net_bind_net != None:
- #look for a valid net
- if check_valid_uuid(net_bind_net):
- net_bind_key = "uuid"
- else:
- net_bind_key = "name"
- result, content = my.db.get_table(FROM='nets', WHERE={net_bind_key: net_bind_net} )
- if result<0:
- bottle.abort(HTTP_Internal_Server_Error, 'getting nets from db ' + content)
- return
- elif result==0:
- bottle.abort(HTTP_Bad_Request, "bind_net %s '%s'not found" % (net_bind_key, net_bind_net) )
- return
- elif result>1:
- bottle.abort(HTTP_Bad_Request, "more than one bind_net %s '%s' found, use uuid" % (net_bind_key, net_bind_net) )
- return
- network["bind_net"] = content[0]["uuid"]
- if net_bind_type != None:
- if net_bind_type[0:5] != "vlan:":
- bottle.abort(HTTP_Bad_Request, "bad format for 'bind_type', must be 'vlan:<tag>'")
- return
- if int(net_bind_type[5:]) > 4095 or int(net_bind_type[5:])<=0 :
- bottle.abort(HTTP_Bad_Request, "bad format for 'bind_type', must be 'vlan:<tag>' with a tag between 1 and 4095")
- return
- if net_provider!=None:
- if net_provider[:9]=="openflow:":
- if net_type!="ptp" and net_type!="data":
- bottle.abort(HTTP_Bad_Request, "Only 'ptp' or 'data' net types can be bound to 'openflow'")
- else:
- if net_type!="bridge_man" and net_type!="bridge_data":
- bottle.abort(HTTP_Bad_Request, "Only 'bridge_man' or 'bridge_data' net types can be bound to 'bridge', 'macvtap' or 'default")
+ """
+ Update a network_id into DB.
+ :param network_id: network id
+ :return:
+ """
+ my = config_dic['http_threads'][threading.current_thread().name]
+
+ try:
+ # parse input data
+ http_content = format_in(network_update_schema)
+ change_keys_http2db(http_content['network'], http2db_network)
+ network = http_content['network']
+ return my.ovim.edit_network(network_id, network)
+
+ except ovim.ovimException as e:
+ my.logger.error(str(e), exc_info=True)
+ bottle.abort(e.http_code, str(e))
+ except Exception as e:
+ my.logger.error(str(e), exc_info=True)
+ bottle.abort(HTTP_Bad_Request, str(e))