Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / nfpkg.py
1 # Copyright ETSI Contributors and Others.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import click
17 from osmclient.common.exceptions import ClientException
18 from osmclient.cli_commands import utils
19 from prettytable import PrettyTable
20 import yaml
21 import json
22 from datetime import datetime
23 import logging
24
25 logger = logging.getLogger("osmclient")
26
27
28 def vnfd_list(ctx, nf_type, filter, long):
29 logger.debug("")
30 if nf_type:
31 utils.check_client_version(ctx.obj, "--nf_type")
32 elif filter:
33 utils.check_client_version(ctx.obj, "--filter")
34 if filter:
35 filter = "&".join(filter)
36 if nf_type:
37 if nf_type == "vnf":
38 nf_filter = "_admin.type=vnfd"
39 elif nf_type == "pnf":
40 nf_filter = "_admin.type=pnfd"
41 elif nf_type == "hnf":
42 nf_filter = "_admin.type=hnfd"
43 else:
44 raise ClientException(
45 'wrong value for "--nf_type" option, allowed values: vnf, pnf, hnf'
46 )
47 if filter:
48 filter = "{}&{}".format(nf_filter, filter)
49 else:
50 filter = nf_filter
51 if filter:
52 resp = ctx.obj.vnfd.list(filter)
53 else:
54 resp = ctx.obj.vnfd.list()
55 # print(yaml.safe_dump(resp))
56 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
57 if fullclassname == "osmclient.sol005.client.Client":
58 if long:
59 table = PrettyTable(
60 [
61 "nfpkg name",
62 "id",
63 "desc type",
64 "vendor",
65 "version",
66 "onboarding state",
67 "operational state",
68 "usage state",
69 "date",
70 "last update",
71 ]
72 )
73 else:
74 table = PrettyTable(["nfpkg name", "id", "desc type"])
75 for vnfd in resp:
76 name = vnfd.get("id", vnfd.get("name", "-"))
77 descriptor_type = "sol006" if "product-name" in vnfd else "rel8"
78 if long:
79 onb_state = vnfd["_admin"].get("onboardingState", "-")
80 op_state = vnfd["_admin"].get("operationalState", "-")
81 vendor = vnfd.get("provider", vnfd.get("vendor"))
82 version = vnfd.get("version")
83 usage_state = vnfd["_admin"].get("usageState", "-")
84 date = datetime.fromtimestamp(vnfd["_admin"]["created"]).strftime(
85 "%Y-%m-%dT%H:%M:%S"
86 )
87 last_update = datetime.fromtimestamp(
88 vnfd["_admin"]["modified"]
89 ).strftime("%Y-%m-%dT%H:%M:%S")
90 table.add_row(
91 [
92 name,
93 vnfd["_id"],
94 descriptor_type,
95 vendor,
96 version,
97 onb_state,
98 op_state,
99 usage_state,
100 date,
101 last_update,
102 ]
103 )
104 else:
105 table.add_row([name, vnfd["_id"], descriptor_type])
106 else:
107 table = PrettyTable(["nfpkg name", "id"])
108 for vnfd in resp:
109 table.add_row([vnfd["name"], vnfd["id"]])
110 table.align = "l"
111 print(table)
112
113
114 @click.command(name="vnfd-list", short_help="list all xNF packages (VNF, HNF, PNF)")
115 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
116 @click.option(
117 "--filter",
118 default=None,
119 multiple=True,
120 help="restricts the list to the NF pkg matching the filter",
121 )
122 @click.option("--long", is_flag=True, help="get more details")
123 @click.pass_context
124 def vnfd_list1(ctx, nf_type, filter, long):
125 """list all xNF packages (VNF, HNF, PNF)"""
126 logger.debug("")
127 vnfd_list(ctx, nf_type, filter, long)
128
129
130 @click.command(name="vnfpkg-list", short_help="list all xNF packages (VNF, HNF, PNF)")
131 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
132 @click.option(
133 "--filter",
134 default=None,
135 multiple=True,
136 help="restricts the list to the NFpkg matching the filter",
137 )
138 @click.option("--long", is_flag=True, help="get more details")
139 @click.pass_context
140 def vnfd_list2(ctx, nf_type, filter, long):
141 """list all xNF packages (VNF, HNF, PNF)"""
142 logger.debug("")
143 vnfd_list(ctx, nf_type, filter, long)
144
145
146 @click.command(name="nfpkg-list", short_help="list all xNF packages (VNF, HNF, PNF)")
147 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
148 @click.option(
149 "--filter",
150 default=None,
151 multiple=True,
152 help="restricts the list to the NFpkg matching the filter",
153 )
154 @click.option("--long", is_flag=True, help="get more details")
155 @click.pass_context
156 def nfpkg_list(ctx, nf_type, filter, long):
157 """list all xNF packages (VNF, HNF, PNF)"""
158 logger.debug("")
159 utils.check_client_version(ctx.obj, ctx.command.name)
160 vnfd_list(ctx, nf_type, filter, long)
161
162
163 def vnfd_show(ctx, name, literal):
164 logger.debug("")
165 resp = ctx.obj.vnfd.get(name)
166 # resp = ctx.obj.vnfd.get_individual(name)
167
168 if literal:
169 print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
170 return
171
172 table = PrettyTable(["field", "value"])
173 for k, v in list(resp.items()):
174 table.add_row([k, utils.wrap_text(text=json.dumps(v, indent=2), width=100)])
175 table.align = "l"
176 print(table)
177
178
179 @click.command(name="vnfd-show", short_help="shows the details of a NF package")
180 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
181 @click.argument("name")
182 @click.pass_context
183 def vnfd_show1(ctx, name, literal):
184 """shows the content of a VNFD
185
186 NAME: name or ID of the VNFD/VNFpkg
187 """
188 logger.debug("")
189 vnfd_show(ctx, name, literal)
190
191
192 @click.command(name="vnfpkg-show", short_help="shows the details of a NF package")
193 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
194 @click.argument("name")
195 @click.pass_context
196 def vnfd_show2(ctx, name, literal):
197 """shows the content of a VNFD
198
199 NAME: name or ID of the VNFD/VNFpkg
200 """
201 logger.debug("")
202 vnfd_show(ctx, name, literal)
203
204
205 @click.command(name="nfpkg-show", short_help="shows the details of a NF package")
206 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
207 @click.argument("name")
208 @click.pass_context
209 def nfpkg_show(ctx, name, literal):
210 """shows the content of a NF Descriptor
211
212 NAME: name or ID of the NFpkg
213 """
214 logger.debug("")
215 vnfd_show(ctx, name, literal)
216
217
218 def vnfd_create(
219 ctx,
220 filename,
221 overwrite,
222 skip_charm_build,
223 override_epa,
224 override_nonepa,
225 override_paravirt,
226 repo,
227 vendor,
228 version,
229 ):
230 logger.debug("")
231 utils.check_client_version(ctx.obj, ctx.command.name)
232 if repo:
233 filename = ctx.obj.osmrepo.get_pkg("vnf", filename, repo, vendor, version)
234 ctx.obj.vnfd.create(
235 filename,
236 overwrite=overwrite,
237 skip_charm_build=skip_charm_build,
238 override_epa=override_epa,
239 override_nonepa=override_nonepa,
240 override_paravirt=override_paravirt,
241 )
242
243
244 @click.command(name="vnfd-create", short_help="creates a new VNFD/VNFpkg")
245 @click.argument("filename")
246 @click.option(
247 "--overwrite", "overwrite", default=None, help="overwrite deprecated, use override"
248 )
249 @click.option(
250 "--override",
251 "overwrite",
252 default=None,
253 help="overrides fields in descriptor, format: "
254 '"key1.key2...=value[;key3...=value;...]"',
255 )
256 @click.option(
257 "--skip-charm-build",
258 default=False,
259 is_flag=True,
260 help="The charm will not be compiled, it is assumed to already exist",
261 )
262 @click.option(
263 "--override-epa",
264 required=False,
265 default=False,
266 is_flag=True,
267 help="adds guest-epa parameters to all VDU",
268 )
269 @click.option(
270 "--override-nonepa",
271 required=False,
272 default=False,
273 is_flag=True,
274 help="removes all guest-epa parameters from all VDU",
275 )
276 @click.option(
277 "--override-paravirt",
278 required=False,
279 default=False,
280 is_flag=True,
281 help="overrides all VDU interfaces to PARAVIRT",
282 )
283 @click.option("--repo", default=None, help="[repository]: Repository name")
284 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
285 @click.option(
286 "--version",
287 default="latest",
288 help="[repository]: filter by version. Default: latest",
289 )
290 @click.pass_context
291 def vnfd_create1(
292 ctx,
293 filename,
294 overwrite,
295 skip_charm_build,
296 override_epa,
297 override_nonepa,
298 override_paravirt,
299 repo,
300 vendor,
301 version,
302 ):
303 """creates a new VNFD/VNFpkg
304 \b
305 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
306 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
307 If FILENAME is an NF Package folder, it is built and then onboarded.
308 """
309 logger.debug("")
310 vnfd_create(
311 ctx,
312 filename,
313 overwrite=overwrite,
314 skip_charm_build=skip_charm_build,
315 override_epa=override_epa,
316 override_nonepa=override_nonepa,
317 override_paravirt=override_paravirt,
318 repo=repo,
319 vendor=vendor,
320 version=version,
321 )
322
323
324 @click.command(name="vnfpkg-create", short_help="creates a new VNFD/VNFpkg")
325 @click.argument("filename")
326 @click.option(
327 "--overwrite",
328 "overwrite",
329 default=None, # hidden=True,
330 help="Deprecated. Use override",
331 )
332 @click.option(
333 "--override",
334 "overwrite",
335 default=None,
336 help="overrides fields in descriptor, format: "
337 '"key1.key2...=value[;key3...=value;...]"',
338 )
339 @click.option(
340 "--skip-charm-build",
341 default=False,
342 is_flag=True,
343 help="The charm will not be compiled, it is assumed to already exist",
344 )
345 @click.option(
346 "--override-epa",
347 required=False,
348 default=False,
349 is_flag=True,
350 help="adds guest-epa parameters to all VDU",
351 )
352 @click.option(
353 "--override-nonepa",
354 required=False,
355 default=False,
356 is_flag=True,
357 help="removes all guest-epa parameters from all VDU",
358 )
359 @click.option(
360 "--override-paravirt",
361 required=False,
362 default=False,
363 is_flag=True,
364 help="overrides all VDU interfaces to PARAVIRT",
365 )
366 @click.option("--repo", default=None, help="[repository]: Repository name")
367 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
368 @click.option(
369 "--version",
370 default="latest",
371 help="[repository]: filter by version. Default: latest",
372 )
373 @click.pass_context
374 def vnfd_create2(
375 ctx,
376 filename,
377 overwrite,
378 skip_charm_build,
379 override_epa,
380 override_nonepa,
381 override_paravirt,
382 repo,
383 vendor,
384 version,
385 ):
386 """creates a new VNFD/VNFpkg
387 \b
388 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
389 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
390 If FILENAME is an NF Package folder, it is built and then onboarded.
391 """
392 logger.debug("")
393 vnfd_create(
394 ctx,
395 filename,
396 overwrite=overwrite,
397 skip_charm_build=skip_charm_build,
398 override_epa=override_epa,
399 override_nonepa=override_nonepa,
400 override_paravirt=override_paravirt,
401 repo=repo,
402 vendor=vendor,
403 version=version,
404 )
405
406
407 @click.command(name="nfpkg-create", short_help="creates a new NFpkg")
408 @click.argument("filename")
409 @click.option(
410 "--overwrite",
411 "overwrite",
412 default=None, # hidden=True,
413 help="Deprecated. Use override",
414 )
415 @click.option(
416 "--override",
417 "overwrite",
418 default=None,
419 help="overrides fields in descriptor, format: "
420 '"key1.key2...=value[;key3...=value;...]"',
421 )
422 @click.option(
423 "--skip-charm-build",
424 default=False,
425 is_flag=True,
426 help="The charm will not be compiled, it is assumed to already exist",
427 )
428 @click.option(
429 "--override-epa",
430 required=False,
431 default=False,
432 is_flag=True,
433 help="adds guest-epa parameters to all VDU",
434 )
435 @click.option(
436 "--override-nonepa",
437 required=False,
438 default=False,
439 is_flag=True,
440 help="removes all guest-epa parameters from all VDU",
441 )
442 @click.option(
443 "--override-paravirt",
444 required=False,
445 default=False,
446 is_flag=True,
447 help="overrides all VDU interfaces to PARAVIRT",
448 )
449 @click.option("--repo", default=None, help="[repository]: Repository name")
450 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
451 @click.option(
452 "--version",
453 default="latest",
454 help="[repository]: filter by version. Default: latest",
455 )
456 @click.pass_context
457 def nfpkg_create(
458 ctx,
459 filename,
460 overwrite,
461 skip_charm_build,
462 override_epa,
463 override_nonepa,
464 override_paravirt,
465 repo,
466 vendor,
467 version,
468 ):
469 """creates a new NFpkg
470
471 \b
472 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
473 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
474 If FILENAME is an NF Package folder, it is built and then onboarded.
475 """
476 logger.debug("")
477 vnfd_create(
478 ctx,
479 filename,
480 overwrite=overwrite,
481 skip_charm_build=skip_charm_build,
482 override_epa=override_epa,
483 override_nonepa=override_nonepa,
484 override_paravirt=override_paravirt,
485 repo=repo,
486 vendor=vendor,
487 version=version,
488 )
489
490
491 def vnfd_update(ctx, name, content):
492 logger.debug("")
493 utils.check_client_version(ctx.obj, ctx.command.name)
494 ctx.obj.vnfd.update(name, content)
495
496
497 @click.command(name="vnfd-update", short_help="updates a new VNFD/VNFpkg")
498 @click.argument("name")
499 @click.option(
500 "--content",
501 default=None,
502 help="filename with the VNFD/VNFpkg replacing the current one",
503 )
504 @click.pass_context
505 def vnfd_update1(ctx, name, content):
506 """updates a VNFD/VNFpkg
507
508 NAME: name or ID of the VNFD/VNFpkg
509 """
510 logger.debug("")
511 vnfd_update(ctx, name, content)
512
513
514 @click.command(name="vnfpkg-update", short_help="updates a VNFD/VNFpkg")
515 @click.argument("name")
516 @click.option(
517 "--content",
518 default=None,
519 help="filename with the VNFD/VNFpkg replacing the current one",
520 )
521 @click.pass_context
522 def vnfd_update2(ctx, name, content):
523 """updates a VNFD/VNFpkg
524
525 NAME: VNFD yaml file or VNFpkg tar.gz file
526 """
527 logger.debug("")
528 vnfd_update(ctx, name, content)
529
530
531 @click.command(name="nfpkg-update", short_help="updates a NFpkg")
532 @click.argument("name")
533 @click.option(
534 "--content", default=None, help="filename with the NFpkg replacing the current one"
535 )
536 @click.pass_context
537 def nfpkg_update(ctx, name, content):
538 """updates a NFpkg
539
540 NAME: NF Descriptor yaml file or NFpkg tar.gz file
541 """
542 logger.debug("")
543 vnfd_update(ctx, name, content)
544
545
546 def vnfd_delete(ctx, name, force):
547 logger.debug("")
548 if not force:
549 ctx.obj.vnfd.delete(name)
550 else:
551 utils.check_client_version(ctx.obj, "--force")
552 ctx.obj.vnfd.delete(name, force)
553
554
555 @click.command(name="vnfd-delete", short_help="deletes a VNFD/VNFpkg")
556 @click.argument("name")
557 @click.option(
558 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
559 )
560 @click.pass_context
561 def vnfd_delete1(ctx, name, force):
562 """deletes a VNFD/VNFpkg
563
564 NAME: name or ID of the VNFD/VNFpkg to be deleted
565 """
566 logger.debug("")
567 vnfd_delete(ctx, name, force)
568
569
570 @click.command(name="vnfpkg-delete", short_help="deletes a VNFD/VNFpkg")
571 @click.argument("name")
572 @click.option(
573 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
574 )
575 @click.pass_context
576 def vnfd_delete2(ctx, name, force):
577 """deletes a VNFD/VNFpkg
578
579 NAME: name or ID of the VNFD/VNFpkg to be deleted
580 """
581 logger.debug("")
582 vnfd_delete(ctx, name, force)
583
584
585 @click.command(name="nfpkg-delete", short_help="deletes a NFpkg")
586 @click.argument("name")
587 @click.option(
588 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
589 )
590 @click.pass_context
591 def nfpkg_delete(ctx, name, force):
592 """deletes a NFpkg
593
594 NAME: name or ID of the NFpkg to be deleted
595 """
596 logger.debug("")
597 vnfd_delete(ctx, name, force)