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