| yadavmr | 58af6b1 | 2019-09-18 15:33:25 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2019 Tech Mahindra Limited |
| 5 | # |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | ## |
| 20 | |
| 21 | ## Change log: |
| yadavmr | f3ec8f4 | 2020-01-30 16:07:59 +0530 | [diff] [blame] | 22 | # Jayant Madavi, Mrityunjay Yadav : MY00514913@techmahindra.com |
| 23 | ##̥ |
| 24 | |
| yadavmr | 58af6b1 | 2019-09-18 15:33:25 +0530 | [diff] [blame] | 25 | |
| yadavmr | 0784733 | 2019-11-28 12:01:48 +0530 | [diff] [blame] | 26 | import random |
| yadavmr | 58af6b1 | 2019-09-18 15:33:25 +0530 | [diff] [blame] | 27 | from haikunator import Haikunator |
| yadavmr | f3ec8f4 | 2020-01-30 16:07:59 +0530 | [diff] [blame] | 28 | import yaml |
| 29 | from os.path import basename |
| 30 | import hashlib |
| 31 | |
| 32 | from robot.api import logger |
| 33 | from robot.api.deco import keyword |
| yadavmr | 58af6b1 | 2019-09-18 15:33:25 +0530 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def generate_name(): |
| 37 | haikunator = Haikunator() |
| 38 | name = haikunator.haikunate(delimiter='_', token_length=2) |
| 39 | return name |
| yadavmr | 0784733 | 2019-11-28 12:01:48 +0530 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def get_random_item_from_list(l): |
| 43 | assert isinstance(l, list), "List should be provided" |
| 44 | return random.choice(l) |
| yadavmr | f3ec8f4 | 2020-01-30 16:07:59 +0530 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def get_scaled_vnf(nsr): |
| 48 | nsr = yaml.load(nsr) |
| 49 | if 'scaling-group' in nsr['_admin']: |
| 50 | return nsr['_admin']['scaling-group'][0]['nb-scale-op'] |
| 51 | else: |
| 52 | return 0 |
| 53 | |
| 54 | |
| 55 | @keyword('Get File Name From Path') |
| 56 | def get_filename(path): |
| 57 | filename = basename(path) |
| 58 | return filename, filename.split('.')[0] |
| 59 | |
| 60 | |
| 61 | @keyword('Generate MD5') |
| 62 | def generate_md5(fpath): |
| 63 | hash_md5 = hashlib.md5() |
| 64 | with open(fpath, "rb") as f: |
| 65 | for chunk in iter(lambda: f.read(1024), b""): |
| 66 | hash_md5.update(chunk) |
| 67 | return hash_md5.hexdigest() |