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 | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 15 | def __init__(self, url, user, password, enterpriseId, siteId): |
| 16 | self.url = url |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 17 | self.user = user |
| 18 | self.password = password |
| 19 | self.key = self.get_key() |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 20 | self.enterpriseId = enterpriseId |
| 21 | self.siteId = siteId |
| 22 | self.update() |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 23 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 24 | def headers(self): |
| 25 | h = CaseInsensitiveDict() |
| 26 | h["Content-Type"] = "application/json" |
| 27 | h["Authorization"] = "Bearer " + self.key |
| 28 | return h |
| 29 | |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 30 | def update(self): |
| 31 | url = self.url + "enterprises" |
| 32 | response = requests.get(url, headers=self.headers()) |
| 33 | if response.status_code != 200: |
| 34 | log.error("get_enterprise() failed, status_code: {}".format(response.status_code)) |
| 35 | return None |
| 36 | |
| 37 | result = json.loads(response.text) |
| 38 | enterprises = result['enterprise'] |
| 39 | |
| 40 | for enterprise in enterprises: |
| 41 | for k, v in enterprise.items(): |
| 42 | if k == 'enterprise-id' and v == self.enterpriseId: |
| 43 | self.enterprise = enterprise |
| 44 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 45 | def get_mbr(self, device_group): |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 46 | 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] | 47 | response = requests.get(url, headers=self.headers()) |
| 48 | if response.status_code != 200: |
| 49 | log.error("Failed to get mbr, status_code: {}".format(response.status_code)) |
| 50 | return None |
| 51 | mbr = json.loads(response.text) |
| 52 | return mbr |
| 53 | |
| 54 | def set_mbr(self, device_group, mbr): |
| 55 | log.info("Set {} mbr to {}".format(device_group, mbr)) |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 56 | m = {"downlink": 10000000, "uplink": mbr} |
| 57 | 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] | 58 | response = requests.post(url, headers=self.headers(), json=m) |
| 59 | |
| 60 | # If error, renew api key and try again |
| 61 | if response.status_code != 201: |
| 62 | log.info("Renew ROC api key") |
| 63 | self.key = self.get_key() |
| 64 | response = requests.post(url, headers=self.headers(), json=m) |
| 65 | if response.status_code != 201: |
| 66 | log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr, response.status_code)) |
| 67 | sys.exit() |
| 68 | |
| 69 | def get_key(self): |
| 70 | url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token" |
| 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 | } |
| 80 | response = requests.post(url, data, headers) |
| 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 | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 94 | url = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v2.0.0/connectivity-service-v2/" |
| 95 | user = sys.argv[1] |
| 96 | password = sys.argv[2] |
| 97 | enterprise = 'aether-onf' |
| 98 | site = 'menlo-4g' |
| 99 | |
| 100 | roc = Roc(url, user, password, enterprise, site) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 101 | |
| 102 | cameras = "menlo-4g-cameras" |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 103 | mbr = roc.get_mbr(cameras) |
| 104 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 105 | |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 106 | roc.set_mbr(cameras, 7000000) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 107 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 108 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
Shad Ansari | 3fd0560 | 2022-03-28 20:32:37 -0700 | [diff] [blame^] | 109 | |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 110 | roc.set_mbr(cameras, 10000000) |
| 111 | mbr = roc.get_mbr(cameras) |
Shad Ansari | 3a48b64 | 2021-12-01 22:42:19 +0000 | [diff] [blame] | 112 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |