Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 1 | """ |
| 2 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 3 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 4 | """ |
| 5 | |
| 6 | import requests |
| 7 | import json |
| 8 | from requests.structures import CaseInsensitiveDict |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 9 | import logging as log |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 10 | |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 11 | |
Shad Ansari | ab4141c | 2021-12-03 03:33:18 +0000 | [diff] [blame] | 12 | URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/" |
| 13 | #URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/" |
| 14 | |
Shad Ansari | ab4141c | 2021-12-03 03:33:18 +0000 | [diff] [blame] | 15 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 16 | class Roc(object): |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 17 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 18 | def __init__(self, user, password): |
| 19 | self.user = user |
| 20 | self.password = password |
| 21 | self.key = self.get_key() |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 22 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 23 | def headers(self): |
| 24 | h = CaseInsensitiveDict() |
| 25 | h["Content-Type"] = "application/json" |
| 26 | h["Authorization"] = "Bearer " + self.key |
| 27 | return h |
| 28 | |
| 29 | def get_mbr(self, device_group): |
| 30 | url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group) |
| 31 | response = requests.get(url, headers=self.headers()) |
| 32 | if response.status_code != 200: |
| 33 | log.error("Failed to get mbr, status_code: {}".format(response.status_code)) |
| 34 | return None |
| 35 | mbr = json.loads(response.text) |
| 36 | return mbr |
| 37 | |
| 38 | def set_mbr(self, device_group, mbr): |
| 39 | log.info("Set {} mbr to {}".format(device_group, mbr)) |
| 40 | m = {'uplink' : mbr} |
| 41 | url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group) |
| 42 | response = requests.post(url, headers=self.headers(), json=m) |
| 43 | |
| 44 | # If error, renew api key and try again |
| 45 | if response.status_code != 201: |
| 46 | log.info("Renew ROC api key") |
| 47 | self.key = self.get_key() |
| 48 | response = requests.post(url, headers=self.headers(), json=m) |
| 49 | if response.status_code != 201: |
| 50 | log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr, response.status_code)) |
| 51 | sys.exit() |
| 52 | |
| 53 | def get_key(self): |
| 54 | url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token" |
| 55 | headers = CaseInsensitiveDict() |
| 56 | headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 57 | data = { |
| 58 | 'grant_type': 'password', |
| 59 | 'client_id': 'aether-roc-gui', |
| 60 | 'username': self.user, |
| 61 | 'password': self.password, |
| 62 | 'scope': 'openid profile email groups' |
| 63 | } |
| 64 | response = requests.post(url, data, headers) |
| 65 | key = json.loads(response.text)['access_token'] |
| 66 | return key |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 67 | |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 68 | if __name__ == '__main__': |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 69 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 70 | # use valid keycloak user/password |
| 71 | roc = Roc("user", "password") |
| 72 | |
| 73 | cameras = "menlo-4g-cameras" |
| 74 | |
| 75 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 76 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 77 | roc.set_mbr(cameras, 5000000) |
| 78 | mbr = roc.get_mbr(cameras) |
Shad Ansari | ab4141c | 2021-12-03 03:33:18 +0000 | [diff] [blame] | 79 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame^] | 80 | roc.set_mbr(cameras, 10000000) |
| 81 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 82 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |