Fix pep8 error for vnfs
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / notify_lcm.py
1 # Copyright 2016 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 uuid
15 import logging
16 import traceback
17
18 from rest_framework import status
19 from rest_framework.response import Response
20 from lcm.ns.vnfs.const import INST_TYPE
21 from lcm.pub.exceptions import NSLCMException
22 from lcm.pub.database.models import VNFCInstModel, VLInstModel, NfInstModel, PortInstModel, CPInstModel, VmInstModel
23 from lcm.pub.utils.values import ignore_case_get
24
25
26 logger = logging.getLogger(__name__)
27
28
29 class NotifyLcm(object):
30     def __init__(self, vnfmid, vnfInstanceId, data):
31         logger.debug("[Notify LCM] vnfmid=%s, vnfInstanceId=%s, data=%s" % (vnfmid, vnfInstanceId, data))
32         self.vnf_instid = ''
33         self.vnfmid = vnfmid
34         self.m_vnfInstanceId = vnfInstanceId
35         self.status = ignore_case_get(data, 'status')
36         self.operation = ignore_case_get(data, 'operation')
37         self.lcm_jobid = ignore_case_get(data, 'jobId')
38         self.vnfdmodule = ignore_case_get(data, 'vnfdmodule')
39         self.affectedVnfc = ignore_case_get(data, 'affectedVnfc')
40         self.affectedVl = ignore_case_get(data, 'affectedVl')
41         self.affectedCp = ignore_case_get(data, 'affectedCp')
42         self.affectedVirtualStorage = ignore_case_get(data, 'affectedVirtualStorage')
43
44     def do_biz(self):
45         try:
46             self.vnf_instid = self.get_vnfinstid(self.m_vnfInstanceId, self.vnfmid)
47             self.update_Vnfc()
48             self.update_Vl()
49             self.update_Cp()
50             self.update_Storage()
51             logger.debug("notify lcm end")
52         except NSLCMException as e:
53             self.exception(e.message)
54         except Exception:
55             logger.error(traceback.format_exc())
56             self.exception('unexpected exception')
57
58     def get_vnfinstid(self, mnfinstid, vnfm_inst_id):
59         nfinst = NfInstModel.objects.filter(mnfinstid=mnfinstid, vnfm_inst_id=vnfm_inst_id).first()
60         if nfinst:
61             return nfinst.nfinstid
62         else:
63             self.exception('vnfinstid not exist')
64
65     def exception(self, error_msg):
66         logger.error('Notify Lcm failed, detail message: %s' % error_msg)
67         return Response(data={'error': '%s' % error_msg}, status=status.HTTP_409_CONFLICT)
68
69     def update_Vnfc(self):
70         for vnfc in self.affectedVnfc:
71             vnfcInstanceId = ignore_case_get(vnfc, 'vnfcInstanceId')
72             vduId = ignore_case_get(vnfc, 'vduId')
73             changeType = ignore_case_get(vnfc, 'changeType')
74             vimId = ignore_case_get(vnfc, 'vimid')
75             vmId = ignore_case_get(vnfc, 'vmid')
76             vmName = ignore_case_get(vnfc, 'vmname')
77
78             if changeType == 'added':
79                 VNFCInstModel(vnfcinstanceid=vnfcInstanceId, vduid=vduId,
80                               nfinstid=self.vnf_instid, vmid=vmId).save()
81                 VmInstModel(vmid=vmId, vimid=vimId, resouceid=vmId, insttype=INST_TYPE.VNF,
82                             instid=self.vnf_instid, vmname=vmName, hostid='1').save()
83             elif changeType == 'removed':
84                 VNFCInstModel.objects.filter(vnfcinstanceid=vnfcInstanceId).delete()
85             elif changeType == 'modified':
86                 VNFCInstModel.objects.filter(vnfcinstanceid=vnfcInstanceId).update(vduid=vduId,
87                                                                                    nfinstid=self.vnf_instid,
88                                                                                    vmid=vmId)
89             else:
90                 self.exception('affectedVnfc struct error: changeType not in {added,removed,modified}')
91
92     def update_Vl(self):
93         for vl in self.affectedVl:
94             vlInstanceId = ignore_case_get(vl, 'vlInstanceId')
95             vldid = ignore_case_get(vl, 'vldid')
96             changeType = ignore_case_get(vl, 'changeType')
97             networkResource = ignore_case_get(vl, 'networkResource')
98             resourceType = ignore_case_get(networkResource, 'resourceType')
99             resourceId = ignore_case_get(networkResource, 'resourceId')
100
101             if resourceType != 'network':
102                 self.exception('affectedVl struct error: resourceType not euqal network')
103
104             ownerId = self.vnf_instid
105             ownerId = self.get_vnfinstid(self.vnf_instid, self.vnfmid)
106
107             if changeType == 'added':
108                 VLInstModel(vlInstanceId=vlInstanceId, vldId=vldid, ownerType=0, ownerId=ownerId,
109                             relatedNetworkId=resourceId, vlType=0).save()
110             elif changeType == 'removed':
111                 VLInstModel.objects.filter(vlInstanceId=vlInstanceId).delete()
112             elif changeType == 'modified':
113                 VLInstModel.objects.filter(vlInstanceId=vlInstanceId)\
114                     .update(vldId=vldid, ownerType=0, ownerId=ownerId, relatedNetworkId=resourceId, vlType=0)
115             else:
116                 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
117
118     def update_Cp(self):
119         for cp in self.affectedCp:
120             virtualLinkInstanceId = ignore_case_get(cp, 'virtualLinkInstanceId')
121             ownertype = ignore_case_get(cp, 'ownertype')
122             if not ownertype:
123                 ownertype = 0
124             ownerid = self.vnf_instid if str(ownertype) == "0" else ignore_case_get(cp, 'ownerid')
125             cpInstanceId = ignore_case_get(cp, 'cpinstanceid')
126             cpdId = ignore_case_get(cp, 'cpdid')
127             changeType = ignore_case_get(cp, 'changetype')
128             relatedportId = ''
129             portResource = ignore_case_get(cp, 'portResource')
130             if portResource:
131                 vimId = ignore_case_get(portResource, 'vimid')
132                 resourceId = ignore_case_get(portResource, 'resourceid')
133                 resourceName = ignore_case_get(portResource, 'resourceName')
134                 tenant = ignore_case_get(portResource, 'tenant')
135                 ipAddress = ignore_case_get(portResource, 'ipAddress')
136                 macAddress = ignore_case_get(portResource, 'macAddress')
137                 instId = ignore_case_get(portResource, 'instId')
138                 portid = str(uuid.uuid4())
139                 PortInstModel(portid=portid, networkid='unknown', subnetworkid='unknown', vimid=vimId,
140                               resourceid=resourceId, name=resourceName, instid=instId, cpinstanceid=cpInstanceId,
141                               bandwidth='unknown', operationalstate='active', ipaddress=ipAddress, macaddress=macAddress,
142                               floatipaddress='unknown', serviceipaddress='unknown', typevirtualnic='unknown',
143                               sfcencapsulation='gre', direction='unknown', tenant=tenant).save()
144                 relatedportId = portid
145
146             if changeType == 'added':
147                 CPInstModel(cpinstanceid=cpInstanceId, cpdid=cpdId, ownertype=ownertype, ownerid=ownerid,
148                             relatedtype=2, relatedport=relatedportId, status='active').save()
149             elif changeType == 'removed':
150                 CPInstModel.objects.filter(cpinstanceid=cpInstanceId).delete()
151             elif changeType == 'changed':
152                 CPInstModel.objects.filter(cpinstanceid=cpInstanceId).update(cpdid=cpdId, ownertype=ownertype,
153                                                                              ownerid=ownerid,
154                                                                              vlinstanceid=virtualLinkInstanceId,
155                                                                              relatedtype=2, relatedport=relatedportId)
156             else:
157                 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
158
159     def update_Storage(self):
160         pass
161
162     def update_vnf_by_vnfdmodule(self):
163         NfInstModel.objects.filter(nfinstid=self.vnf_instid).update(vnfd_model=self.vnfdmodule)