summaryrefslogtreecommitdiffstats
path: root/lcm/lcm/nf/biz/change_ext_conn.py
blob: b423a2ec679ac5088e608ef4f52d0858d493ee54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright 2019 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import traceback
from threading import Thread

from lcm.nf.biz.common import port_save
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

logger = logging.getLogger(__name__)


class ChangeExtConn(Thread):
    def __init__(self, data, nf_inst_id, job_id):
        super(ChangeExtConn, self).__init__()
        self.data = data
        self.nf_inst_id = nf_inst_id
        self.job_id = job_id
        self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
        self.extVirtualLinks = ignore_case_get(self.data, "extVirtualLinks")
        self.vimConnectionInfo = ignore_case_get(self.data, "vimConnectionInfo")
        self.additionalParams = ignore_case_get(self.data, "additionalParams")
        self.lcm_op_occ = VnfLcmOpOcc(
            vnf_inst_id=nf_inst_id,
            lcm_op_id=job_id,
            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."
            )
            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."
            )
            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.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.args[0])
            self.change_ext_conn_failed_handle(e.args[0])
        except Exception as e:
            logger.error(e.args[0])
            logger.error(traceback.format_exc())
            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
        )
        logger.debug("Grant resource end, response: %s" % apply_result)

    def do_operation(self):
        logger.info('Operation resource begin')
        logger.debug("self.vimConnectionInfo: %s" % self.vimConnectionInfo)
        vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
        vm_id = ignore_case_get(self.additionalParams, "vmid")
        if not vm_id:
            vms = VmInstModel.objects.filter(instid=self.nf_inst_id)
            vm_id = vms[0].resourceid
        vim_id = ignore_case_get(self.vimConnectionInfo[0], "vimid")
        accessInfo = ignore_case_get(self.vimConnectionInfo[0], "accessInfo")
        tenant = ignore_case_get(accessInfo, "tenant")

        self.vim_cache, self.res_cache = {}, {}
        for extVirtualLink in self.extVirtualLinks:
            network_id = ignore_case_get(extVirtualLink, "resourceId")
            ext_cps = ignore_case_get(extVirtualLink, "extCps")
            for ext_cp in ext_cps:
                cpd_id = ignore_case_get(ext_cp, "cpdId")
                cp_config = ignore_case_get(ext_cp, "cpConfig")
                cp_protocol_data = ignore_case_get(cp_config[0], "cpProtocolData")
                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")
                # maxAddress = ignore_case_get(addressRange, "maxAddress")
                subnet_id = ignore_case_get(ip_addresses[0], "subnetId")

                vdu_id = ""
                cps = ignore_case_get(vnfd_info, "cps")
                for cp in cps:
                    cpd_id_in_model = ignore_case_get(cp, "cpd_id")
                    if cpd_id == cpd_id_in_model:
                        vdu_id = ignore_case_get(cp, "vdu_id")
                        break

                port = {
                    "cp_id": cpd_id,
                    "cpd_id": cpd_id,
                    "vm_id": vm_id,
                    "description": "",
                    "properties": {
                        # "name": "",
                        # "mac_address": mac_address,
                        # "ip_address:": fixed_addresse,
                        # "ip_range_start": minAddress,
                        # "ip_range_end": maxAddress,
                        "location_info": {
                            "vimid": vim_id,
                            "tenant": tenant
                        }
                    },
                    "vl_id": network_id,
                    "vdu_id": vdu_id,
                    "networkId": network_id,
                    "subnetId": subnet_id
                }
                for resource_type in ['vdus', 'vls', 'cps', 'volume_storages']:
                    for resource in ignore_case_get(vnfd_info, resource_type):
                        if "location_info" not in resource["properties"]:
                            resource["properties"]["location_info"] = {}
                        resource["properties"]["location_info"]["vimid"] = vim_id
                        resource["properties"]["location_info"]["tenant"] = tenant

                # if cp_instance_id:
                #     ret = adaptor.get_port_of_vm(self.vim_cache, self.res_cache, vnfd_info, port,
                #                                  self.do_notify_op, "port")
                #     port_info = ignore_case_get(ret, "interfaceAttachment")
                #     net_id = ignore_case_get(port_info, "net_id")
                #     if network_id == net_id:
                #         adaptor.update_port(self.vim_cache, self.res_cache, vnfd_info, port,
                #                             self.do_notify_op, "port")
                #     else:
                #         adaptor.delete_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")
                # else:
                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"
                )
                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=OPERATION_TYPE.CHANGE_EXT_CONN,
            operation_state=OPERATION_STATE_TYPE.COMPLETED
        )
        self.set_ext_connectivity(data['changedExtConnectivity'])

        logger.debug('Notify request data = %s' % data)
        NotificationsUtil().send_notification(data)

    def rollback_operation(self):
        pass

    def query_inst_resource(self, inst_resource):
        logger.debug('Query resource begin')
        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
            )
            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)
                )
        logger.debug('Query resource end, resource=%s' % inst_resource)

    def get_resource(self, resource):
        return {
            "vim_id": resource.vimid,
            "tenant_id": resource.tenant,
            "res_id": resource.resourceid
        }

    def do_create_port_notify(self, res_type, ret):
        self.port_id = ignore_case_get(ret, "id")
        port_save("", self.nf_inst_id, ret)

    def do_notify_op(self, operation_type, status, resid):
        if operation_type == "delete":
            PortInstModel.objects.filter()
            # TODO delete port from table
        elif operation_type == "create":
            pass
            # TODO save port in table
        else:
            pass
            # TODO update port in table
        logger.info('VNF resource %s updated to: %s' % (resid, status))

    def set_ext_connectivity(self, ext_connectivity):
        for extVirtualLink in self.extVirtualLinks:
            vim_connection_id = ignore_case_get(extVirtualLink, "vimConnectionId")
            network_id = ignore_case_get(extVirtualLink, "resourceId")
            ext_cps = ignore_case_get(extVirtualLink, "extCps")
            ext_link_ports = []
            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")
                ext_link_ports.append({
                    'id': cp_instance_id,
                    'resourceHandle': {
                        'vimConnectionId': vim_connection_id,
                        'resourceId': self.res_cache.get("port").get(cp_instance_id),
                        'resourceProviderId': cpd_id,
                        'vimLevelResourceType': 'port'
                    },
                    'cpInstanceId': cp_instance_id
                })
            network_resource = {
                'vimConnectionId': vim_connection_id,
                'resourceId': network_id,
                'resourceProviderId': "",
                'vimLevelResourceType': 'network'
            }
            ext_connectivity.append({
                'id': network_id,
                'resourceHandle': network_resource,
                'extLinkPorts': ext_link_ports
            })

    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, 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
            }
        )