1 # Copyright 2019 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from threading import Thread
19 from lcm.nf.biz.common import port_save
20 from lcm.nf.biz.grant_vnf import grant_resource
21 from lcm.nf.const import RESOURCE_MAP, GRANT_TYPE, OPERATION_STATE_TYPE
22 from lcm.nf.const import VNF_STATUS, OPERATION_TASK, OPERATION_TYPE
23 from lcm.nf.const import SUB_OPERATION_TASK
24 from lcm.pub.database.models import VmInstModel, NfInstModel, PortInstModel
25 from lcm.pub.utils.notificationsutil import NotificationsUtil, prepare_notification
26 from lcm.pub.utils.values import ignore_case_get
27 from lcm.pub.utils.timeutil import now_time
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.exceptions import NFLCMException
30 from lcm.pub.exceptions import NFLCMExceptionConflict
31 from lcm.pub.vimapi import adaptor
32 from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
34 logger = logging.getLogger(__name__)
37 class ChangeExtConn(Thread):
38 def __init__(self, data, nf_inst_id, job_id):
39 super(ChangeExtConn, self).__init__()
41 self.nf_inst_id = nf_inst_id
43 self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
44 self.extVirtualLinks = ignore_case_get(self.data, "extVirtualLinks")
45 self.vimConnectionInfo = ignore_case_get(self.data, "vimConnectionInfo")
46 self.additionalParams = ignore_case_get(self.data, "additionalParams")
47 self.lcm_op_occ = VnfLcmOpOcc(
48 vnf_inst_id=nf_inst_id,
50 operation=OPERATION_TYPE.CHANGE_EXT_CONN,
51 task=OPERATION_TASK.CHANGE_EXT_CONN
57 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
58 JobUtil.add_job_status(
61 "Start to apply grant."
64 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
65 JobUtil.add_job_status(
68 "Start to change ext conn."
71 sub_operation=SUB_OPERATION_TASK.GRANTED,
72 operation_state=OPERATION_STATE_TYPE.PROCESSING
75 self.vnf_insts.update(
76 status='INSTANTIATED',
79 self.send_notification()
80 JobUtil.add_job_status(
83 "Change ext conn success."
86 sub_operation=SUB_OPERATION_TASK.SUCCESS,
87 operation_state=OPERATION_STATE_TYPE.COMPLETED
89 except NFLCMException as e:
90 logger.error(e.args[0])
91 self.change_ext_conn_failed_handle(e.args[0])
92 except Exception as e:
93 logger.error(e.args[0])
94 logger.error(traceback.format_exc())
95 self.change_ext_conn_failed_handle(e.args[0])
98 logger.debug("Start pre deal for VNF change_ext_conn task")
100 vnf_is_in_processing, vnf_op = self.lcm_op_occ.is_in_processing()
101 if vnf_is_in_processing:
102 raise NFLCMExceptionConflict('VNF(%s) %s in processing.' % (
103 self.nf_inst_id, vnf_op
105 self.lcm_op_occ.add()
107 def apply_grant(self):
108 vdus = VmInstModel.objects.filter(instid=self.nf_inst_id)
109 apply_result = grant_resource(
111 nf_inst_id=self.nf_inst_id,
113 grant_type=GRANT_TYPE.CHANGE_CONNECTIVITY,
116 logger.debug("Grant resource end, response: %s" % apply_result)
118 def do_operation(self):
119 logger.info('Operation resource begin')
120 logger.debug("self.vimConnectionInfo: %s" % self.vimConnectionInfo)
121 vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
122 vm_id = ignore_case_get(self.additionalParams, "vmid")
124 vms = VmInstModel.objects.filter(instid=self.nf_inst_id)
125 vm_id = vms[0].resourceid
126 vim_id = ignore_case_get(self.vimConnectionInfo[0], "vimid")
127 accessInfo = ignore_case_get(self.vimConnectionInfo[0], "accessInfo")
128 tenant = ignore_case_get(accessInfo, "tenant")
130 self.vim_cache, self.res_cache = {}, {}
131 for extVirtualLink in self.extVirtualLinks:
132 network_id = ignore_case_get(extVirtualLink, "resourceId")
133 ext_cps = ignore_case_get(extVirtualLink, "extCps")
134 for ext_cp in ext_cps:
135 cpd_id = ignore_case_get(ext_cp, "cpdId")
136 cp_config = ignore_case_get(ext_cp, "cpConfig")
137 cp_protocol_data = ignore_case_get(cp_config[0], "cpProtocolData")
138 ip_addresses = ignore_case_get(ignore_case_get(
142 # fixed_addresse = ignore_case_get(ip_addresses[0], "fixedAddresses")[0]
143 # addressRange = ignore_case_get(ip_addresses[0], "addressRange")
144 # minAddress = ignore_case_get(addressRange, "minAddress")
145 # maxAddress = ignore_case_get(addressRange, "maxAddress")
146 subnet_id = ignore_case_get(ip_addresses[0], "subnetId")
149 cps = ignore_case_get(vnfd_info, "cps")
151 cpd_id_in_model = ignore_case_get(cp, "cpd_id")
152 if cpd_id == cpd_id_in_model:
153 vdu_id = ignore_case_get(cp, "vdu_id")
163 # "mac_address": mac_address,
164 # "ip_address:": fixed_addresse,
165 # "ip_range_start": minAddress,
166 # "ip_range_end": maxAddress,
174 "networkId": network_id,
175 "subnetId": subnet_id
177 for resource_type in ['vdus', 'vls', 'cps', 'volume_storages']:
178 for resource in ignore_case_get(vnfd_info, resource_type):
179 if "location_info" not in resource["properties"]:
180 resource["properties"]["location_info"] = {}
181 resource["properties"]["location_info"]["vimid"] = vim_id
182 resource["properties"]["location_info"]["tenant"] = tenant
185 # ret = adaptor.get_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
186 # self.do_notify_op, "port")
187 # port_info = ignore_case_get(ret, "interfaceAttachment")
188 # net_id = ignore_case_get(port_info, "net_id")
189 # if network_id == net_id:
190 # adaptor.update_port(self.vim_cache, self.res_cache, vnfd_info, port,
191 # self.do_notify_op, "port")
193 # adaptor.delete_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
194 # self.do_notify_op, "port")
195 # adaptor.create_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
196 # self.do_notify_op, "port")
202 self.do_create_port_notify,
205 port["port_id"] = self.port_id
206 logger.debug('create_port_of_vm request data = %s' % port)
207 adaptor.create_port_of_vm(
215 PortInstModel.objects.filter(resourceid=self.port_id).update(vmid=vm_id)
216 logger.info('Operate resource complete')
218 def send_notification(self):
219 data = prepare_notification(
220 nfinstid=self.nf_inst_id,
222 operation=OPERATION_TYPE.CHANGE_EXT_CONN,
223 operation_state=OPERATION_STATE_TYPE.COMPLETED
225 self.set_ext_connectivity(data['changedExtConnectivity'])
227 logger.debug('Notify request data = %s' % data)
228 NotificationsUtil().send_notification(data)
230 def rollback_operation(self):
233 def query_inst_resource(self, inst_resource):
234 logger.debug('Query resource begin')
235 for resource_type in list(RESOURCE_MAP.keys()):
236 resource_table = globals().get(resource_type + 'InstModel')
237 resource_insts = resource_table.objects.filter(
238 instid=self.nf_inst_id
240 for resource_inst in resource_insts:
241 if not resource_inst.resourceid:
243 inst_resource[RESOURCE_MAP.get(resource_type)].append(
244 self.get_resource(resource_inst)
246 logger.debug('Query resource end, resource=%s' % inst_resource)
248 def get_resource(self, resource):
250 "vim_id": resource.vimid,
251 "tenant_id": resource.tenant,
252 "res_id": resource.resourceid
255 def do_create_port_notify(self, res_type, ret):
256 self.port_id = ignore_case_get(ret, "id")
257 port_save("", self.nf_inst_id, ret)
259 def do_notify_op(self, operation_type, status, resid):
260 if operation_type == "delete":
261 PortInstModel.objects.filter()
262 # TODO delete port from table
263 elif operation_type == "create":
265 # TODO save port in table
268 # TODO update port in table
269 logger.info('VNF resource %s updated to: %s' % (resid, status))
271 def set_ext_connectivity(self, ext_connectivity):
272 for extVirtualLink in self.extVirtualLinks:
273 vim_connection_id = ignore_case_get(extVirtualLink, "vimConnectionId")
274 network_id = ignore_case_get(extVirtualLink, "resourceId")
275 ext_cps = ignore_case_get(extVirtualLink, "extCps")
277 for ext_cp in ext_cps:
278 cpd_id = ignore_case_get(ext_cp, "cpdId")
279 cp_config = ignore_case_get(ext_cp, "cpConfig")
280 cp_instance_id = ignore_case_get(cp_config[0], "cpInstanceId")
281 ext_link_ports.append({
282 'id': cp_instance_id,
284 'vimConnectionId': vim_connection_id,
285 'resourceId': self.res_cache.get("port").get(cp_instance_id),
286 'resourceProviderId': cpd_id,
287 'vimLevelResourceType': 'port'
289 'cpInstanceId': cp_instance_id
292 'vimConnectionId': vim_connection_id,
293 'resourceId': network_id,
294 'resourceProviderId': "",
295 'vimLevelResourceType': 'network'
297 ext_connectivity.append({
299 'resourceHandle': network_resource,
300 'extLinkPorts': ext_link_ports
303 def change_ext_conn_failed_handle(self, error_msg):
304 logger.error('Chnage ext conn failed, detail message: %s', error_msg)
305 self.vnf_insts.update(
306 status=VNF_STATUS.FAILED,
307 lastuptime=now_time()
309 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
310 JobUtil.add_job_status(self.job_id, 255, error_msg)
312 sub_operation=SUB_OPERATION_TASK.ERROR,
313 operation_state=OPERATION_STATE_TYPE.FAILED,