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 | |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 6 | import sys |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 7 | import requests |
| 8 | import json |
| 9 | from requests.structures import CaseInsensitiveDict |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 10 | import logging as log |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 11 | |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 12 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 13 | class Roc(object): |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 14 | |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 15 | def __init__(self, url, keycloak, user, password, enterpriseId, siteId): |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 16 | self.url = url |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 17 | self.keycloak = keycloak |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 18 | self.user = user |
| 19 | self.password = password |
| 20 | self.key = self.get_key() |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 21 | self.enterpriseId = enterpriseId |
| 22 | self.siteId = siteId |
| 23 | self.update() |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 24 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 25 | def headers(self): |
| 26 | h = CaseInsensitiveDict() |
| 27 | h["Content-Type"] = "application/json" |
| 28 | h["Authorization"] = "Bearer " + self.key |
| 29 | return h |
| 30 | |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 31 | def update(self): |
| 32 | url = self.url + "enterprises" |
| 33 | response = requests.get(url, headers=self.headers()) |
| 34 | if response.status_code != 200: |
| 35 | log.error("get_enterprise() failed, status_code: {}".format(response.status_code)) |
| 36 | return None |
| 37 | |
| 38 | result = json.loads(response.text) |
| 39 | enterprises = result['enterprise'] |
| 40 | |
| 41 | for enterprise in enterprises: |
| 42 | for k, v in enterprise.items(): |
| 43 | if k == 'enterprise-id' and v == self.enterpriseId: |
| 44 | self.enterprise = enterprise |
| 45 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 46 | def get_mbr(self, device_group): |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 47 | url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 48 | response = requests.get(url, headers=self.headers()) |
| 49 | if response.status_code != 200: |
| 50 | log.error("Failed to get mbr, status_code: {}".format(response.status_code)) |
| 51 | return None |
| 52 | mbr = json.loads(response.text) |
| 53 | return mbr |
| 54 | |
| 55 | def set_mbr(self, device_group, mbr): |
| 56 | log.info("Set {} mbr to {}".format(device_group, mbr)) |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 57 | m = {"downlink": 10000000, "uplink": mbr} |
| 58 | url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 59 | response = requests.post(url, headers=self.headers(), json=m) |
| 60 | |
| 61 | # If error, renew api key and try again |
| 62 | if response.status_code != 201: |
| 63 | log.info("Renew ROC api key") |
| 64 | self.key = self.get_key() |
| 65 | response = requests.post(url, headers=self.headers(), json=m) |
| 66 | if response.status_code != 201: |
| 67 | log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr, response.status_code)) |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 68 | # sys.exit() |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 69 | |
| 70 | def get_key(self): |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 71 | headers = CaseInsensitiveDict() |
| 72 | headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 73 | data = { |
| 74 | 'grant_type': 'password', |
| 75 | 'client_id': 'aether-roc-gui', |
| 76 | 'username': self.user, |
| 77 | 'password': self.password, |
| 78 | 'scope': 'openid profile email groups' |
| 79 | } |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 80 | response = requests.post(self.keycloak, data, headers) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 81 | key = json.loads(response.text)['access_token'] |
| 82 | return key |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 83 | |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 84 | if __name__ == '__main__': |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 85 | |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 86 | log.basicConfig( |
| 87 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 88 | level=log.DEBUG, |
| 89 | datefmt='%Y-%m-%d %H:%M:%S', |
| 90 | stream=sys.stdout) |
| 91 | log.debug("Testing roc...") |
| 92 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 93 | # use valid keycloak user/password |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 94 | url = "http://roc.aetherproject.net/aether-roc-api/aether/v2.0.0/connectivity-service-v2/" |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 95 | user = sys.argv[1] |
| 96 | password = sys.argv[2] |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 97 | enterprise = 'ase-4g-enterprise' |
| 98 | site = 'ase-site-4g' |
| 99 | keycloak = "http://keycloak.aetherproject.net/auth/realms/aether/protocol/openid-connect/token" |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 100 | |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 101 | roc = Roc(url, keycloak, user, password, enterprise, site) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 102 | |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 103 | cameras = "ase-4g-device-group2" |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 104 | mbr = roc.get_mbr(cameras) |
| 105 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 106 | |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 107 | roc.set_mbr(cameras, 6000000) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 108 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 109 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame] | 110 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 111 | roc.set_mbr(cameras, 10000000) |
| 112 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 113 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |