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 #self.update_vnf_by_vnfdmodule()
52 logger.debug("notify lcm end")
53 except NSLCMException as e:
54 self.exception(e.message)
56 logger.error(traceback.format_exc())
57 self.exception('unexpected exception')
59 def get_vnfinstid(self, mnfinstid, vnfm_inst_id):
60 nfinst = NfInstModel.objects.filter(mnfinstid=mnfinstid, vnfm_inst_id=vnfm_inst_id).first()
62 return nfinst.nfinstid
64 self.exception('vnfinstid not exist')
66 def exception(self, error_msg):
67 logger.error('Notify Lcm failed, detail message: %s' % error_msg)
68 return Response(data={'error': '%s' % error_msg}, status=status.HTTP_409_CONFLICT)
70 def update_Vnfc(self):
71 for vnfc in self.affectedVnfc:
72 vnfcInstanceId = ignore_case_get(vnfc, 'vnfcInstanceId')
73 vduId = ignore_case_get(vnfc, 'vduId')
74 changeType = ignore_case_get(vnfc, 'changeType')
75 vimId = ignore_case_get(vnfc, 'vimid')
76 vmId = ignore_case_get(vnfc, 'vmid')
77 vmName = ignore_case_get(vnfc, 'vmname')
78 # resourceType = ignore_case_get(vmResource, 'resourceType')
79 # resourceId = ignore_case_get(vmId, 'resourceId')
82 # if resourceType != 'vm':
83 # self.exception('affectedVnfc struct error: resourceType not euqal vm')
85 if changeType == 'added':
86 VNFCInstModel(vnfcinstanceid=vnfcInstanceId, vduid=vduId,
87 nfinstid=self.vnf_instid, vmid=vmId).save()
88 VmInstModel(vmid=vmId, vimid=vimId, resouceid=vmId, insttype=INST_TYPE.VNF,
89 instid=self.vnf_instid, vmname=vmName, hostid='1').save()
90 elif changeType == 'removed':
91 VNFCInstModel.objects.filter(vnfcinstanceid=vnfcInstanceId).delete()
92 elif changeType == 'modified':
93 VNFCInstModel.objects.filter(vnfcinstanceid=vnfcInstanceId).update(vduid=vduId,
94 nfinstid=self.vnf_instid,
97 self.exception('affectedVnfc struct error: changeType not in {added,removed,modified}')
100 for vl in self.affectedVl:
101 vlInstanceId = ignore_case_get(vl, 'vlInstanceId')
102 vldid = ignore_case_get(vl, 'vldid')
103 changeType = ignore_case_get(vl, 'changeType')
104 networkResource = ignore_case_get(vl, 'networkResource')
105 resourceType = ignore_case_get(networkResource, 'resourceType')
106 resourceId = ignore_case_get(networkResource, 'resourceId')
108 if resourceType != 'network':
109 self.exception('affectedVl struct error: resourceType not euqal network')
111 ownerId = self.vnf_instid
112 ownerId = self.get_vnfinstid(self.vnf_instid, self.vnfmid)
114 if changeType == 'added':
115 VLInstModel(vlInstanceId=vlInstanceId, vldId=vldid, ownerType=0, ownerId=ownerId,
116 relatedNetworkId=resourceId, vlType=0).save()
117 elif changeType == 'removed':
118 VLInstModel.objects.filter(vlInstanceId=vlInstanceId).delete()
119 elif changeType == 'modified':
120 VLInstModel.objects.filter(vlInstanceId=vlInstanceId)\
121 .update(vldId=vldid, ownerType=0, ownerId=ownerId, relatedNetworkId=resourceId, vlType=0)
123 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
126 for cp in self.affectedCp:
127 virtualLinkInstanceId = ignore_case_get(cp, 'virtualLinkInstanceId')
128 #ownerid = ignore_case_get(cp, 'ownerid')
129 ownertype = ignore_case_get(cp, 'ownertype')
132 ownerid = self.vnf_instid if str(ownertype) == "0" else ignore_case_get(cp, 'ownerid')
133 cpInstanceId = ignore_case_get(cp, 'cpinstanceid')
134 cpdId = ignore_case_get(cp, 'cpdid')
135 changeType = ignore_case_get(cp, 'changetype')
137 portResource = ignore_case_get(cp, 'portResource')
139 vimId = ignore_case_get(portResource, 'vimid')
140 resourceId = ignore_case_get(portResource, 'resourceid')
141 resourceName = ignore_case_get(portResource, 'resourceName')
142 tenant = ignore_case_get(portResource, 'tenant')
143 ipAddress = ignore_case_get(portResource, 'ipAddress')
144 macAddress = ignore_case_get(portResource, 'macAddress')
145 sfcEncapsulation = ignore_case_get(portResource, 'sfcEncapsulation')
146 instId = ignore_case_get(portResource, 'instId')
147 portid = str(uuid.uuid4())
148 PortInstModel(portid=portid, networkid='unknown', subnetworkid='unknown', vimid=vimId,
149 resourceid=resourceId, name=resourceName, instid=instId, cpinstanceid=cpInstanceId,
150 bandwidth='unknown', operationalstate='active', ipaddress=ipAddress, macaddress=macAddress,
151 floatipaddress='unknown', serviceipaddress='unknown', typevirtualnic='unknown',
152 sfcencapsulation='gre', direction='unknown', tenant=tenant).save()
153 relatedportId = portid
155 if changeType == 'added':
156 CPInstModel(cpinstanceid=cpInstanceId, cpdid=cpdId, ownertype=ownertype, ownerid=ownerid,
157 relatedtype=2, relatedport=relatedportId, status='active').save()
158 elif changeType == 'removed':
159 CPInstModel.objects.filter(cpinstanceid=cpInstanceId).delete()
160 elif changeType == 'changed':
161 CPInstModel.objects.filter(cpinstanceid=cpInstanceId).update(cpdid=cpdId, ownertype=ownertype,
163 vlinstanceid=virtualLinkInstanceId,
164 relatedtype=2, relatedport=relatedportId)
166 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
168 def update_Storage(self):
171 def update_vnf_by_vnfdmodule(self):
172 NfInstModel.objects.filter(nfinstid=self.vnf_instid).update(vnfd_model=self.vnfdmodule)