improve code coverage rate (change ext conn) after vfclcm upgraded from python2...
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / change_ext_conn.py
1 # Copyright 2019 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import json
15 import logging
16 import traceback
17 from threading import Thread
18
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
33
34 logger = logging.getLogger(__name__)
35
36
37 class ChangeExtConn(Thread):
38     def __init__(self, data, nf_inst_id, job_id):
39         super(ChangeExtConn, self).__init__()
40         self.data = data
41         self.nf_inst_id = nf_inst_id
42         self.job_id = job_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,
49             lcm_op_id=job_id,
50             operation=OPERATION_TYPE.CHANGE_EXT_CONN,
51             task=OPERATION_TASK.CHANGE_EXT_CONN
52         )
53         self.pre_deal()
54
55     def run(self):
56         try:
57             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
58             JobUtil.add_job_status(
59                 self.job_id,
60                 10,
61                 "Start to apply grant."
62             )
63             self.apply_grant()
64             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
65             JobUtil.add_job_status(
66                 self.job_id,
67                 50,
68                 "Start to change ext conn."
69             )
70             self.lcm_op_occ.upd(
71                 sub_operation=SUB_OPERATION_TASK.GRANTED,
72                 operation_state=OPERATION_STATE_TYPE.PROCESSING
73             )
74             self.do_operation()
75             self.vnf_insts.update(
76                 status='INSTANTIATED',
77                 lastuptime=now_time()
78             )
79             self.send_notification()
80             JobUtil.add_job_status(
81                 self.job_id,
82                 100,
83                 "Change ext conn success."
84             )
85             self.lcm_op_occ.upd(
86                 sub_operation=SUB_OPERATION_TASK.SUCCESS,
87                 operation_state=OPERATION_STATE_TYPE.COMPLETED
88             )
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])
96
97     def pre_deal(self):
98         logger.debug("Start pre deal for VNF change_ext_conn task")
99
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
104             ))
105         self.lcm_op_occ.add()
106
107     def apply_grant(self):
108         vdus = VmInstModel.objects.filter(instid=self.nf_inst_id)
109         apply_result = grant_resource(
110             data=self.data,
111             nf_inst_id=self.nf_inst_id,
112             job_id=self.job_id,
113             grant_type=GRANT_TYPE.CHANGE_CONNECTIVITY,
114             vdus=vdus
115         )
116         logger.debug("Grant resource end, response: %s" % apply_result)
117
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")
123         if not vm_id:
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")
129
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(
139                     cp_protocol_data[0],
140                     "ipOverEthernet"
141                 ), "ipAddresses")
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")
147
148                 vdu_id = ""
149                 cps = ignore_case_get(vnfd_info, "cps")
150                 for cp in 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")
154                         break
155
156                 port = {
157                     "cp_id": cpd_id,
158                     "cpd_id": cpd_id,
159                     "vm_id": vm_id,
160                     "description": "",
161                     "properties": {
162                         # "name": "",
163                         # "mac_address": mac_address,
164                         # "ip_address:": fixed_addresse,
165                         # "ip_range_start": minAddress,
166                         # "ip_range_end": maxAddress,
167                         "location_info": {
168                             "vimid": vim_id,
169                             "tenant": tenant
170                         },
171                         # TODO need confirm
172                         "protocol_data": [
173                             {
174                                 "address_data": {
175                                     "l3_address_data": {
176                                         "fixed_ip_address":
177                                             {"ip_address_assignment": True,
178                                              "floating_ip_activated": True,
179                                              "ip_address_type": "IPV4",
180                                              "number_of_ip_address": 1}
181                                     },
182                                 },
183                             },
184                         ],
185                     },
186                     "vl_id": network_id,
187                     "vdu_id": vdu_id,
188                     "networkId": network_id,
189                     "subnetId": subnet_id
190                 }
191                 for resource_type in ['vdus', 'vls', 'cps', 'volume_storages']:
192                     for resource in ignore_case_get(vnfd_info, resource_type):
193                         if "location_info" not in resource["properties"]:
194                             resource["properties"]["location_info"] = {}
195                         resource["properties"]["location_info"]["vimid"] = vim_id
196                         resource["properties"]["location_info"]["tenant"] = tenant
197
198                 # if cp_instance_id:
199                 #     ret = adaptor.get_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
200                 #                                  self.do_notify_op, "port")
201                 #     port_info = ignore_case_get(ret, "interfaceAttachment")
202                 #     net_id = ignore_case_get(port_info, "net_id")
203                 #     if network_id == net_id:
204                 #         adaptor.update_port(self.vim_cache, self.res_cache, vnfd_info, port,
205                 #                             self.do_notify_op, "port")
206                 #     else:
207                 #         adaptor.delete_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
208                 #                                   self.do_notify_op, "port")
209                 #         adaptor.create_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
210                 #                                   self.do_notify_op, "port")
211                 # else:
212                 adaptor.create_port(
213                     self.vim_cache,
214                     self.res_cache,
215                     vnfd_info, port,
216                     self.do_create_port_notify,
217                     "port"
218                 )
219                 port["port_id"] = self.port_id
220                 logger.debug('create_port_of_vm request data = %s' % port)
221                 adaptor.create_port_of_vm(
222                     self.vim_cache,
223                     self.res_cache,
224                     vnfd_info,
225                     port,
226                     self.do_notify_op,
227                     "port"
228                 )
229                 PortInstModel.objects.filter(resourceid=self.port_id).update(vmid=vm_id)
230         logger.info('Operate resource complete')
231
232     def send_notification(self):
233         data = prepare_notification(
234             nfinstid=self.nf_inst_id,
235             jobid=self.job_id,
236             operation=OPERATION_TYPE.CHANGE_EXT_CONN,
237             operation_state=OPERATION_STATE_TYPE.COMPLETED
238         )
239         self.set_ext_connectivity(data['changedExtConnectivity'])
240
241         logger.debug('Notify request data = %s' % data)
242         NotificationsUtil().send_notification(data)
243
244     def rollback_operation(self):
245         pass
246
247     def query_inst_resource(self, inst_resource):
248         logger.debug('Query resource begin')
249         for resource_type in list(RESOURCE_MAP.keys()):
250             resource_table = globals().get(resource_type + 'InstModel')
251             resource_insts = resource_table.objects.filter(
252                 instid=self.nf_inst_id
253             )
254             for resource_inst in resource_insts:
255                 if not resource_inst.resourceid:
256                     continue
257                 inst_resource[RESOURCE_MAP.get(resource_type)].append(
258                     self.get_resource(resource_inst)
259                 )
260         logger.debug('Query resource end, resource=%s' % inst_resource)
261
262     def get_resource(self, resource):
263         return {
264             "vim_id": resource.vimid,
265             "tenant_id": resource.tenant,
266             "res_id": resource.resourceid
267         }
268
269     def do_create_port_notify(self, res_type, ret):
270         self.port_id = ignore_case_get(ret, "id")
271         port_save(self.job_id, self.nf_inst_id, ret)
272
273     def do_notify_op(self, operation_type, status, resid):
274         if operation_type == "delete":
275             PortInstModel.objects.filter()
276             # TODO delete port from table
277         elif operation_type == "create":
278             pass
279             # TODO save port in table
280         else:
281             pass
282             # TODO update port in table
283         logger.info('VNF resource %s updated to: %s' % (resid, status))
284
285     def set_ext_connectivity(self, ext_connectivity):
286         for extVirtualLink in self.extVirtualLinks:
287             vim_connection_id = ignore_case_get(extVirtualLink, "vimConnectionId")
288             network_id = ignore_case_get(extVirtualLink, "resourceId")
289             ext_cps = ignore_case_get(extVirtualLink, "extCps")
290             ext_link_ports = []
291             for ext_cp in ext_cps:
292                 cpd_id = ignore_case_get(ext_cp, "cpdId")
293                 cp_config = ignore_case_get(ext_cp, "cpConfig")
294                 cp_instance_id = ignore_case_get(cp_config[0], "cpInstanceId")
295                 ext_link_ports.append({
296                     'id': cp_instance_id,
297                     'resourceHandle': {
298                         'vimConnectionId': vim_connection_id,
299                         'resourceId': self.res_cache.get("port").get(cp_instance_id),
300                         'resourceProviderId': cpd_id,
301                         'vimLevelResourceType': 'port'
302                     },
303                     'cpInstanceId': cp_instance_id
304                 })
305             network_resource = {
306                 'vimConnectionId': vim_connection_id,
307                 'resourceId': network_id,
308                 'resourceProviderId': "",
309                 'vimLevelResourceType': 'network'
310             }
311             ext_connectivity.append({
312                 'id': network_id,
313                 'resourceHandle': network_resource,
314                 'extLinkPorts': ext_link_ports
315             })
316
317     def change_ext_conn_failed_handle(self, error_msg):
318         logger.error('Chnage ext conn failed, detail message: %s', error_msg)
319         self.vnf_insts.update(
320             status=VNF_STATUS.FAILED,
321             lastuptime=now_time()
322         )
323         self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
324         JobUtil.add_job_status(self.job_id, 255, error_msg)
325         self.lcm_op_occ.upd(
326             sub_operation=SUB_OPERATION_TASK.ERROR,
327             operation_state=OPERATION_STATE_TYPE.FAILED,
328             error={
329                 "status": 500,
330                 "detail": error_msg
331             }
332         )