1 # Copyright 2016 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.
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
26 logger = logging.getLogger(__name__)
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))
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')
46 self.vnf_instid = self.get_vnfinstid(self.m_vnfInstanceId, self.vnfmid)
51 logger.debug("notify lcm end")
52 except NSLCMException as e:
53 self.exception(e.message)
55 logger.error(traceback.format_exc())
56 self.exception('unexpected exception')
58 def get_vnfinstid(self, mnfinstid, vnfm_inst_id):
59 nfinst = NfInstModel.objects.filter(mnfinstid=mnfinstid, vnfm_inst_id=vnfm_inst_id).first()
61 return nfinst.nfinstid
63 self.exception('vnfinstid not exist')
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)
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')
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,
90 self.exception('affectedVnfc struct error: changeType not in {added,removed,modified}')
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')
101 if resourceType != 'network':
102 self.exception('affectedVl struct error: resourceType not euqal network')
104 ownerId = self.vnf_instid
105 ownerId = self.get_vnfinstid(self.vnf_instid, self.vnfmid)
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)
116 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
119 for cp in self.affectedCp:
120 virtualLinkInstanceId = ignore_case_get(cp, 'virtualLinkInstanceId')
121 ownertype = ignore_case_get(cp, 'ownertype')
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')
129 portResource = ignore_case_get(cp, '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
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,
154 vlinstanceid=virtualLinkInstanceId,
155 relatedtype=2, relatedport=relatedportId)
157 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
159 def update_Storage(self):
162 def update_vnf_by_vnfdmodule(self):
163 NfInstModel.objects.filter(nfinstid=self.vnf_instid).update(vnfd_model=self.vnfdmodule)