Code Coverage

Cobertura Coverage Report > osm_nbi.osm_vnfm >

vnf_instance_actions.py

Trend

Classes100%
 
Lines82%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
vnf_instance_actions.py
100%
1/1
82%
74/90
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
vnf_instance_actions.py
82%
74/90
N/A

Source

osm_nbi/osm_vnfm/vnf_instance_actions.py
1 # Copyright 2021 K Sai Kiran (Tata Elxsi)
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 1 __author__ = "K Sai Kiran <saikiran.k@tataelxsi.co.in>, Selvi Jayaraman <selvi.j@tataelxsi.co.in>"
17 1 __date__ = "$12-June-2021 8:30:59$"
18
19 1 from osm_nbi.instance_topics import NsrTopic, NsLcmOpTopic, VnfrTopic
20 1 from .base_methods import BaseMethod
21
22
23 1 class VnfLcmOp2NsLcmOp:
24
25 1     def __init__(self, db, fs, msg, auth):
26         """
27         Constructor of Vnf lcm op to Ns lcm op
28         """
29 1         self.new_vnf_lcmop = NewVnfLcmOp(db, fs, msg, auth)
30 1         self.list_vnf_lcmop = ListVnfLcmOp(db, fs, msg, auth)
31 1         self.show_vnf_lcmop = ShowVnfLcmOp(db, fs, msg, auth)
32
33 1     def new(self, rollback, session, indata=None, kwargs=None, headers=None):
34         """
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
41         :return: _id, op_id:
42             _id: identity of the inserted data.
43              op_id: operation id if this is asynchronous, None otherwise
44         """
45 1         return self.new_vnf_lcmop.action(rollback, session, indata, kwargs, headers)
46
47 1     def list(self, session, filter_q=None, api_req=False):
48         """
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.
54         """
55 0         return self.list_vnf_lcmop.action(session, filter_q, api_req)
56
57 1     def show(self, session, _id, api_req=False):
58         """
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.
64         """
65 1         return self.show_vnf_lcmop.action(session, _id, api_req)
66
67
68 1 class NewVnfLcmOp(BaseMethod):
69
70 1     def __init__(self, db, fs, msg, auth):
71         """
72         Constructor of new Vnf Lcm Op
73         """
74 1         super().__init__()
75 1         self.msg = msg
76 1         self.nslcmoptopic = NsLcmOpTopic(db, fs, msg, auth)
77 1         self.nsrtopic = NsrTopic(db, fs, msg, auth)
78 1         self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
79
80 1     def __get_nsdid(self, session, vnf_instance_id):
81         """
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
85         :return: id of nsd id
86         """
87 1         nsr = self.nsrtopic.show(session, vnf_instance_id)
88 1         return nsr['nsd']['_id']
89
90 1     def __get_formatted_indata(self, session, indata):
91         """
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.
96         """
97 1         formatted_indata = {}
98 1         if indata["lcmOperationType"] == "instantiate":
99 1             formatted_indata = {
100                 "nsName": indata["vnfName"],
101                 "nsDescription": indata["vnfDescription"],
102                 "nsdId": self.__get_nsdid(session, indata["vnfInstanceId"]),
103                 "vimAccountId": indata["vimAccountId"],
104                 "nsr_id": indata["vnfInstanceId"],
105                 "lcmOperationType": indata["lcmOperationType"],
106                 "nsInstanceId": indata["vnfInstanceId"]
107             }
108 0         elif indata["lcmOperationType"] == "terminate":
109 0             formatted_indata = {
110                 "lcmOperationType": indata["lcmOperationType"],
111                 "nsInstanceId": indata["vnfInstanceId"]
112             }
113 0         elif indata["lcmOperationType"] == "scale":
114 0             formatted_indata = {
115                 "lcmOperationType": indata["lcmOperationType"],
116                 "nsInstanceId": indata["vnfInstanceId"],
117                 "scaleType": "SCALE_VNF",
118                 "scaleVnfData": {
119                     "scaleVnfType": indata["type"],
120                     "scaleByStepData": {
121                         "scaling-group-descriptor": indata["aspectId"],
122                         "member-vnf-index": indata["additionalParams"]["member-vnf-index"]
123                     }
124                 }
125             }
126 0         elif indata["lcmOperationType"] == "action":
127 0             formatted_indata = {
128                 "lcmOperationType": indata["lcmOperationType"],
129                 "nsInstanceId": indata["vnfInstanceId"],
130                 "member_vnf_index": indata["member_vnf_index"],
131                 "primitive": indata["primitive"],
132                 "primitive_params": indata["primitive_params"]
133             }
134 1         return formatted_indata
135
136 1     def notify_operation(self, session, _id, lcm_operation, op_id):
137         """
138         Formats the operation message params and sends to kafka
139         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
140         :param _id: vnf instance id
141         :param lcm_operation: lcm operation type of a VNF (instantiate, scale, terminate)
142         :param op_id: lcm operation id of a VNF
143         :return: None
144         """
145 1         vnfInstanceId = _id
146 1         operation = lcm_operation
147 1         nslcmop_rec = self.nslcmoptopic.show(session, op_id)
148 1         operation_status = nslcmop_rec["operationState"]
149 1         vnfr = self.vnfrtopic.show(session, vnfInstanceId)
150 1         links = {"self": "/osm/vnflcm/v1/vnf_lcm_op_occs/" + op_id,
151                  "vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfInstanceId}
152 1         params = {"vnfdId": vnfr["vnfd-ref"],
153                   "vnfInstanceId": vnfInstanceId,
154                   "operationState": operation_status,
155                   "vnfLcmOpOccId": op_id,
156                   "_links": links
157                   }
158 1         self.msg.write("vnf", operation, params)
159 1         return None
160
161 1     def action(self, rollback, session, indata=None, kwargs=None, headers=None):
162         """
163         Creates an new lcm operation.
164         :param rollback: list to append the created items at database in case a rollback must be done
165         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
166         :param indata: params to be used for the nsr
167         :param kwargs: used to override the indata
168         :param headers: http request headers
169         :return: id of new lcm operation.
170         """
171 1         vnfInstanceId = indata["vnfInstanceId"]
172 1         lcm_operation = indata["lcmOperationType"]
173 1         vnfr = self.vnfrtopic.show(session, vnfInstanceId)
174 1         indata["vnfInstanceId"] = vnfr.get("nsr-id-ref")
175 1         indata = self.__get_formatted_indata(session, indata)
176 1         op_id, _ = self.nslcmoptopic.new(rollback, session, indata, kwargs, headers)
177 1         self.notify_operation(session, vnfInstanceId, lcm_operation, op_id)
178 1         return op_id, _
179
180
181 1 class ListVnfLcmOp(BaseMethod):
182
183 1     def __init__(self, db, fs, msg, auth):
184         """
185         Constructor call for listing vnf lcm operations
186         """
187 1         super().__init__()
188 1         self.nslcmoptopic = NsLcmOpTopic(db, fs, msg, auth)
189 1         self.nsrtopic = NsrTopic(db, fs, msg, auth)
190
191 1     def action(self, session, filter_q=None, api_req=False):
192         """
193         To get list of vnf lcm operations that matches a filter
194         :param session: contains the used login username and working project
195         :param filter_q: filter of data to be applied
196         :param api_req: True if this call is serving an external API request. False if serving internal request.
197         :return: The list, it can be empty if no one match the filter.
198         """
199 0         list = []
200 0         records = self.nslcmoptopic.list(session, filter_q, api_req)
201 0         for record in records:
202 0             ns_id = record.get("nsInstanceId")
203 0             nsr = self.nsrtopic.show(session, ns_id)
204 0             vnfInstance_id = nsr['constituent-vnfr-ref'][0]
205 0             outdata = sol003_projection(record, vnfInstance_id)
206 0             list.append(outdata)
207 0         return list
208
209
210 1 class ShowVnfLcmOp(BaseMethod):
211
212 1     def __init__(self, db, fs, msg, auth):
213         """
214         Constructor call for showing vnf lcm operation
215         """
216 1         super().__init__()
217 1         self.nslcmoptopic = NsLcmOpTopic(db, fs, msg, auth)
218 1         self.nsrtopic = NsrTopic(db, fs, msg, auth)
219
220 1     def action(self, session, _id, api_req=False):
221         """
222         Get complete information on an Vnf Lcm Operation.
223         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
224         :param _id: Vnf Lcm operation id
225         :param api_req: True if this call is serving an external API request. False if serving internal request.
226         :return: dictionary, raise exception if not found.
227         """
228 1         record = self.nslcmoptopic.show(session, _id, api_req)
229 1         ns_id = record.get("nsInstanceId")
230 1         nsr = self.nsrtopic.show(session, ns_id)
231 1         vnfinstance_id = nsr['constituent-vnfr-ref'][0]
232 1         outdata = sol003_projection(record, vnfinstance_id)
233 1         return outdata
234
235
236 1 def sol003_projection(data, vnfinstance_id):
237     """
238     Returns SOL003 formatted data
239     :param data: contains Lcm Operation information
240     :param vnfinstance_id: id of vnf_instance
241     :return: SOL003 formatted data of vnf lcm op
242     """
243 1     data.pop("nsInstanceId")
244 1     data.pop("operationParams")
245 1     data.pop("links")
246 1     links = {
247         "self": "/osm/vnflcm/v1/vnf_lcm_op_occs/" + data["_id"],
248         "vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfinstance_id,
249     }
250 1     data["_links"] = links
251 1     data["vnfInstanceId"] = vnfinstance_id
252 1     return data