1 # Copyright 2021 K Sai Kiran (Tata Elxsi)
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 __author__
= "K Sai Kiran <saikiran.k@tataelxsi.co.in>, Selvi Jayaraman <selvi.j@tataelxsi.co.in>"
17 __date__
= "$12-June-2021 8:30:59$"
19 from osm_nbi
.instance_topics
import NsrTopic
, NsLcmOpTopic
, VnfrTopic
20 from .base_methods
import BaseMethod
23 class VnfLcmOp2NsLcmOp
:
25 def __init__(self
, db
, fs
, msg
, auth
):
27 Constructor of Vnf lcm op to Ns lcm op
29 self
.new_vnf_lcmop
= NewVnfLcmOp(db
, fs
, msg
, auth
)
30 self
.list_vnf_lcmop
= ListVnfLcmOp(db
, fs
, msg
, auth
)
31 self
.show_vnf_lcmop
= ShowVnfLcmOp(db
, fs
, msg
, auth
)
33 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
35 Creates a new entry into database.
36 :param rollback: list to append created items at database in case a rollback may to be done
37 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
38 :param indata: data to be inserted
39 :param kwargs: used to override the indata descriptor
40 :param headers: http request headers
42 _id: identity of the inserted data.
43 op_id: operation id if this is asynchronous, None otherwise
45 return self
.new_vnf_lcmop
.action(rollback
, session
, indata
, kwargs
, headers
)
47 def list(self
, session
, filter_q
=None, api_req
=False):
49 Get a list of the Vnf Lcm Operation that matches a filter
50 :param session: contains the used login username and working project
51 :param filter_q: filter of data to be applied
52 :param api_req: True if this call is serving an external API request. False if serving internal request.
53 :return: The list, it can be empty if no one match the filter.
55 return self
.list_vnf_lcmop
.action(session
, filter_q
, api_req
)
57 def show(self
, session
, _id
, api_req
=False):
59 Get complete information on an Vnf Lcm Operation
60 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
61 :param _id: server internal id
62 :param api_req: True if this call is serving an external API request. False if serving internal request.
63 :return: dictionary, raise exception if not found.
65 return self
.show_vnf_lcmop
.action(session
, _id
, api_req
)
68 class NewVnfLcmOp(BaseMethod
):
70 def __init__(self
, db
, fs
, msg
, auth
):
72 Constructor of new Vnf Lcm Op
76 self
.nslcmoptopic
= NsLcmOpTopic(db
, fs
, msg
, auth
)
77 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
78 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
80 def __get_nsdid(self
, session
, vnf_instance_id
):
82 Returns a nsd id from vnf instance id.
83 :param session: contains the used login username and working project
84 :param vnf_instance_id: id of vnf instance
87 nsr
= self
.nsrtopic
.show(session
, vnf_instance_id
)
88 return nsr
['nsd']['_id']
90 def __get_formatted_indata(self
, session
, indata
):
92 Returns formatted data for new vnf lcm op
93 :param session: contains the used login username and working project
94 :param indata: contains information for new lcm operation.
95 :return: formatted indata for new lcm op.
98 if indata
["lcmOperationType"] == "instantiate":
100 "nsName": indata
["vnfName"],
101 "nsDescription": indata
["vnfDescription"],
102 "nsdId": self
.__get
_nsdid
(session
, indata
["vnfInstanceId"]),
103 "vimAccountId": indata
.get("vimAccountId"),
104 "paasAccountId": indata
.get("paasAccountId"),
105 "nsr_id": indata
["vnfInstanceId"],
106 "lcmOperationType": indata
["lcmOperationType"],
107 "nsInstanceId": indata
["vnfInstanceId"]
109 elif indata
["lcmOperationType"] == "terminate":
111 "lcmOperationType": indata
["lcmOperationType"],
112 "nsInstanceId": indata
["vnfInstanceId"]
114 elif indata
["lcmOperationType"] == "scale":
116 "lcmOperationType": indata
["lcmOperationType"],
117 "nsInstanceId": indata
["vnfInstanceId"],
118 "scaleType": "SCALE_VNF",
120 "scaleVnfType": indata
["type"],
122 "scaling-group-descriptor": indata
["aspectId"],
123 "member-vnf-index": indata
["additionalParams"]["member-vnf-index"]
127 elif indata
["lcmOperationType"] == "action":
129 "lcmOperationType": indata
["lcmOperationType"],
130 "nsInstanceId": indata
["vnfInstanceId"],
131 "member_vnf_index": indata
["member_vnf_index"],
132 "primitive": indata
["primitive"],
133 "primitive_params": indata
["primitive_params"]
135 return formatted_indata
137 def notify_operation(self
, session
, _id
, lcm_operation
, op_id
):
139 Formats the operation message params and sends to kafka
140 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
141 :param _id: vnf instance id
142 :param lcm_operation: lcm operation type of a VNF (instantiate, scale, terminate)
143 :param op_id: lcm operation id of a VNF
147 operation
= lcm_operation
148 nslcmop_rec
= self
.nslcmoptopic
.show(session
, op_id
)
149 operation_status
= nslcmop_rec
["operationState"]
150 vnfr
= self
.vnfrtopic
.show(session
, vnfInstanceId
)
151 links
= {"self": "/osm/vnflcm/v1/vnf_lcm_op_occs/" + op_id
,
152 "vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfInstanceId
}
153 params
= {"vnfdId": vnfr
["vnfd-ref"],
154 "vnfInstanceId": vnfInstanceId
,
155 "operationState": operation_status
,
156 "vnfLcmOpOccId": op_id
,
159 self
.msg
.write("vnf", operation
, params
)
162 def action(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
164 Creates an new lcm operation.
165 :param rollback: list to append the created items at database in case a rollback must be done
166 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
167 :param indata: params to be used for the nsr
168 :param kwargs: used to override the indata
169 :param headers: http request headers
170 :return: id of new lcm operation.
172 vnfInstanceId
= indata
["vnfInstanceId"]
173 lcm_operation
= indata
["lcmOperationType"]
174 vnfr
= self
.vnfrtopic
.show(session
, vnfInstanceId
)
175 indata
["vnfInstanceId"] = vnfr
.get("nsr-id-ref")
176 indata
= self
.__get
_formatted
_indata
(session
, indata
)
177 op_id
, _
= self
.nslcmoptopic
.new(rollback
, session
, indata
, kwargs
, headers
)
178 self
.notify_operation(session
, vnfInstanceId
, lcm_operation
, op_id
)
182 class ListVnfLcmOp(BaseMethod
):
184 def __init__(self
, db
, fs
, msg
, auth
):
186 Constructor call for listing vnf lcm operations
189 self
.nslcmoptopic
= NsLcmOpTopic(db
, fs
, msg
, auth
)
190 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
192 def action(self
, session
, filter_q
=None, api_req
=False):
194 To get list of vnf lcm operations that matches a filter
195 :param session: contains the used login username and working project
196 :param filter_q: filter of data to be applied
197 :param api_req: True if this call is serving an external API request. False if serving internal request.
198 :return: The list, it can be empty if no one match the filter.
201 records
= self
.nslcmoptopic
.list(session
, filter_q
, api_req
)
202 for record
in records
:
203 ns_id
= record
.get("nsInstanceId")
204 nsr
= self
.nsrtopic
.show(session
, ns_id
)
205 vnfInstance_id
= nsr
['constituent-vnfr-ref'][0]
206 outdata
= sol003_projection(record
, vnfInstance_id
)
211 class ShowVnfLcmOp(BaseMethod
):
213 def __init__(self
, db
, fs
, msg
, auth
):
215 Constructor call for showing vnf lcm operation
218 self
.nslcmoptopic
= NsLcmOpTopic(db
, fs
, msg
, auth
)
219 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
221 def action(self
, session
, _id
, api_req
=False):
223 Get complete information on an Vnf Lcm Operation.
224 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
225 :param _id: Vnf Lcm operation id
226 :param api_req: True if this call is serving an external API request. False if serving internal request.
227 :return: dictionary, raise exception if not found.
229 record
= self
.nslcmoptopic
.show(session
, _id
, api_req
)
230 ns_id
= record
.get("nsInstanceId")
231 nsr
= self
.nsrtopic
.show(session
, ns_id
)
232 vnfinstance_id
= nsr
['constituent-vnfr-ref'][0]
233 outdata
= sol003_projection(record
, vnfinstance_id
)
237 def sol003_projection(data
, vnfinstance_id
):
239 Returns SOL003 formatted data
240 :param data: contains Lcm Operation information
241 :param vnfinstance_id: id of vnf_instance
242 :return: SOL003 formatted data of vnf lcm op
244 data
.pop("nsInstanceId")
245 data
.pop("operationParams")
248 "self": "/osm/vnflcm/v1/vnf_lcm_op_occs/" + data
["_id"],
249 "vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfinstance_id
,
251 data
["_links"] = links
252 data
["vnfInstanceId"] = vnfinstance_id