from lcm.nf.biz.grant_vnf import grant_resource
from lcm.nf.const import RESOURCE_MAP, GRANT_TYPE, OPERATION_STATE_TYPE
from lcm.nf.const import VNF_STATUS, OPERATION_TASK, OPERATION_TYPE
+from lcm.nf.const import SUB_OPERATION_TASK
from lcm.pub.database.models import VmInstModel, NfInstModel, PortInstModel
from lcm.pub.utils.notificationsutil import NotificationsUtil, prepare_notification
from lcm.pub.utils.values import ignore_case_get
from lcm.pub.utils.timeutil import now_time
from lcm.pub.utils.jobutil import JobUtil
from lcm.pub.exceptions import NFLCMException
+from lcm.pub.exceptions import NFLCMExceptionConflict
from lcm.pub.vimapi import adaptor
from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
operation=OPERATION_TYPE.CHANGE_EXT_CONN,
task=OPERATION_TASK.CHANGE_EXT_CONN
)
+ self.pre_deal()
def run(self):
try:
self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
- JobUtil.add_job_status(self.job_id,
- 10,
- "Start to apply grant.")
+ JobUtil.add_job_status(
+ self.job_id,
+ 10,
+ "Start to apply grant."
+ )
self.apply_grant()
self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
- JobUtil.add_job_status(self.job_id,
- 50,
- "Start to change ext conn.")
+ JobUtil.add_job_status(
+ self.job_id,
+ 50,
+ "Start to change ext conn."
+ )
+ self.lcm_op_occ.upd(
+ sub_operation=SUB_OPERATION_TASK.GRANTED,
+ operation_state=OPERATION_STATE_TYPE.PROCESSING
+ )
self.do_operation()
self.vnf_insts.update(
status='INSTANTIATED',
lastuptime=now_time()
)
- self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.COMPLETED)
- JobUtil.add_job_status(self.job_id,
- 100,
- "Change ext conn success.")
+ self.send_notification()
+ JobUtil.add_job_status(
+ self.job_id,
+ 100,
+ "Change ext conn success."
+ )
+ self.lcm_op_occ.upd(
+ sub_operation=SUB_OPERATION_TASK.SUCCESS,
+ operation_state=OPERATION_STATE_TYPE.COMPLETED
+ )
except NFLCMException as e:
- logger.error(e.message)
- self.change_ext_conn_failed_handle(e.message)
+ logger.error(e.args[0])
+ self.change_ext_conn_failed_handle(e.args[0])
except Exception as e:
- logger.error(e.message)
+ logger.error(e.args[0])
logger.error(traceback.format_exc())
- self.change_ext_conn_failed_handle(e.message)
+ self.change_ext_conn_failed_handle(e.args[0])
+
+ def pre_deal(self):
+ logger.debug("Start pre deal for VNF change_ext_conn task")
+
+ vnf_is_in_processing, vnf_op = self.lcm_op_occ.is_in_processing()
+ if vnf_is_in_processing:
+ raise NFLCMExceptionConflict('VNF(%s) %s in processing.' % (
+ self.nf_inst_id, vnf_op
+ ))
+ self.lcm_op_occ.add()
def apply_grant(self):
vdus = VmInstModel.objects.filter(instid=self.nf_inst_id)
- apply_result = grant_resource(data=self.data,
- nf_inst_id=self.nf_inst_id,
- job_id=self.job_id,
- grant_type=GRANT_TYPE.CHANGE_CONNECTIVITY,
- vdus=vdus)
+ apply_result = grant_resource(
+ data=self.data,
+ nf_inst_id=self.nf_inst_id,
+ job_id=self.job_id,
+ grant_type=GRANT_TYPE.CHANGE_CONNECTIVITY,
+ vdus=vdus
+ )
logger.debug("Grant resource end, response: %s" % apply_result)
def do_operation(self):
for ext_cp in ext_cps:
cpd_id = ignore_case_get(ext_cp, "cpdId")
cp_config = ignore_case_get(ext_cp, "cpConfig")
- # cp_instance_id = ignore_case_get(cp_config[0], "cpInstanceId")
cp_protocol_data = ignore_case_get(cp_config[0], "cpProtocolData")
- # mac_address = ignore_case_get(ignore_case_get(cp_protocol_data[0], "ipOverEthernet"), "macAddress")
- ip_addresses = ignore_case_get(ignore_case_get(cp_protocol_data[0], "ipOverEthernet"), "ipAddresses")
+ ip_addresses = ignore_case_get(ignore_case_get(
+ cp_protocol_data[0],
+ "ipOverEthernet"
+ ), "ipAddresses")
# fixed_addresse = ignore_case_get(ip_addresses[0], "fixedAddresses")[0]
# addressRange = ignore_case_get(ip_addresses[0], "addressRange")
# minAddress = ignore_case_get(addressRange, "minAddress")
"location_info": {
"vimid": vim_id,
"tenant": tenant
- }
+ },
+ # TODO need confirm
+ "protocol_data": [
+ {
+ "address_data": {
+ "l3_address_data": {
+ "fixed_ip_address":
+ {"ip_address_assignment": True,
+ "floating_ip_activated": True,
+ "ip_address_type": "IPV4",
+ "number_of_ip_address": 1}
+ },
+ },
+ },
+ ],
},
"vl_id": network_id,
"vdu_id": vdu_id,
# adaptor.create_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
# self.do_notify_op, "port")
# else:
- adaptor.create_port(self.vim_cache, self.res_cache, vnfd_info, port, self.do_create_port_notify, "port")
+ adaptor.create_port(
+ self.vim_cache,
+ self.res_cache,
+ vnfd_info, port,
+ self.do_create_port_notify,
+ "port"
+ )
port["port_id"] = self.port_id
logger.debug('create_port_of_vm request data = %s' % port)
- adaptor.create_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
- self.do_notify_op, "port")
+ adaptor.create_port_of_vm(
+ self.vim_cache,
+ self.res_cache,
+ vnfd_info,
+ port,
+ self.do_notify_op,
+ "port"
+ )
PortInstModel.objects.filter(resourceid=self.port_id).update(vmid=vm_id)
logger.info('Operate resource complete')
def send_notification(self):
- data = prepare_notification(nfinstid=self.nf_inst_id,
- jobid=self.job_id,
- operation=self.op_type,
- operation_state=OPERATION_STATE_TYPE.COMPLETED)
+ data = prepare_notification(
+ nfinstid=self.nf_inst_id,
+ jobid=self.job_id,
+ operation=OPERATION_TYPE.CHANGE_EXT_CONN,
+ operation_state=OPERATION_STATE_TYPE.COMPLETED
+ )
self.set_ext_connectivity(data['changedExtConnectivity'])
logger.debug('Notify request data = %s' % data)
def query_inst_resource(self, inst_resource):
logger.debug('Query resource begin')
- for resource_type in RESOURCE_MAP.keys():
+ for resource_type in list(RESOURCE_MAP.keys()):
resource_table = globals().get(resource_type + 'InstModel')
- resource_insts = resource_table.objects.filter(instid=self.nf_inst_id)
+ resource_insts = resource_table.objects.filter(
+ instid=self.nf_inst_id
+ )
for resource_inst in resource_insts:
if not resource_inst.resourceid:
continue
- inst_resource[RESOURCE_MAP.get(resource_type)].append(self.get_resource(resource_inst))
+ inst_resource[RESOURCE_MAP.get(resource_type)].append(
+ self.get_resource(resource_inst)
+ )
logger.debug('Query resource end, resource=%s' % inst_resource)
def get_resource(self, resource):
def do_create_port_notify(self, res_type, ret):
self.port_id = ignore_case_get(ret, "id")
- port_save("", self.nf_inst_id, ret)
+ port_save(self.job_id, self.nf_inst_id, ret)
def do_notify_op(self, operation_type, status, resid):
if operation_type == "delete":
def change_ext_conn_failed_handle(self, error_msg):
logger.error('Chnage ext conn failed, detail message: %s', error_msg)
- self.vnf_insts.update(status=VNF_STATUS.FAILED,
- lastuptime=now_time())
- self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED)
+ self.vnf_insts.update(
+ status=VNF_STATUS.FAILED,
+ lastuptime=now_time()
+ )
+ self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
JobUtil.add_job_status(self.job_id, 255, error_msg)
+ self.lcm_op_occ.upd(
+ sub_operation=SUB_OPERATION_TASK.ERROR,
+ operation_state=OPERATION_STATE_TYPE.FAILED,
+ error={
+ "status": 500,
+ "detail": error_msg
+ }
+ )