| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 1 | # Copyright 2020 ETSI OSM |
| 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 | from subprocess import run, PIPE |
| 17 | import requests |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 18 | import time |
| 19 | import os |
| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 20 | |
| 21 | class OSMException(Exception): |
| 22 | """A base class for MyProject exceptions.""" |
| 23 | |
| 24 | class ResourceException(OSMException): |
| 25 | def __init__(self, *args, **kwargs): |
| 26 | super().__init__(*args) |
| 27 | self.message = kwargs.get('message') |
| 28 | |
| 29 | class OSM: |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 30 | def __init__(self, osm_hostname, port): |
| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 31 | self.osm_hostname = osm_hostname |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 32 | self.port = port |
| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 33 | |
| 34 | def run_command(self, command): |
| 35 | p = run(["osm {}".format(command)], shell=True, stdout=PIPE, stderr=PIPE) |
| 36 | if p.returncode != 0: |
| 37 | print(p.stdout) |
| 38 | print(p.stderr) |
| 39 | raise ResourceException(message=p.stdout) |
| 40 | else: |
| 41 | return self.clean_output(p.stdout) |
| 42 | |
| 43 | def get_auth_token(self): |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 44 | |
| 45 | r = requests.post("https://{}:{}/osm/admin/v1/tokens".format(self.osm_hostname, self.port), verify=False, headers={"Accept":"application/json"}, json={ |
| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 46 | "username": "admin", |
| 47 | "password": "admin", |
| 48 | "project": "admin" |
| 49 | }) |
| 50 | return r.json()["id"] |
| 51 | |
| 52 | def clean_output(self, output): |
| 53 | return output.decode("utf-8").strip() |
| 54 | |
| 55 | def create_nsd(self, filename): |
| 56 | return self.run_command("nsd-create {}".format(filename)) |
| 57 | |
| 58 | def create_vnfd(self, filename): |
| 59 | return self.run_command("vnfd-create {}".format(filename)) |
| 60 | |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 61 | def create_ns(self, name, nsd, vim): |
| 62 | return self.run_command("ns-create --ns_name {} --nsd_name {} --vim_account {}".format(name, nsd, vim)) |
| 63 | |
| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 64 | def delete_nsd(self, id): |
| 65 | return self.run_command("nsd-delete {}".format(id)) |
| 66 | |
| 67 | def delete_vnfd(self, id): |
| veleza | 6be83cf | 2021-11-16 11:23:47 +0000 | [diff] [blame] | 68 | return self.run_command("vnfd-delete {}".format(id)) |
| 69 | |
| 70 | def delete_ns(self, id): |
| 71 | self.run_command("ns-delete {}".format(id)) |
| 72 | while (requests.get("https://{}:{}/osm/nslcm/v1/ns_instances/{}".format(self.osm_hostname, self.port, id), verify=False, headers={"Accept":"application/json", "Authorization":"Bearer {}".format(os.environ["AUTH_TOKEN"])}).status_code != 404): |
| 73 | print("Deleting NS...") |
| 74 | time.sleep(5) |