blob: 7c1240910d7bac51957feda4bd2f10412f35cfa7 [file] [log] [blame]
Shad Ansari3a48b642021-12-01 22:42:19 +00001"""
2SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
3SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
4"""
5
Shad Ansari3fd05602022-03-28 20:32:37 -07006import sys
Shad Ansari3a48b642021-12-01 22:42:19 +00007import requests
8import json
9from requests.structures import CaseInsensitiveDict
shadf64b92a2022-01-13 19:06:29 +000010import logging as log
Shad Ansari3a48b642021-12-01 22:42:19 +000011
Shad Ansari3a48b642021-12-01 22:42:19 +000012
shadf64b92a2022-01-13 19:06:29 +000013class Roc(object):
Shad Ansari3a48b642021-12-01 22:42:19 +000014
Shad Ansari2948cc02022-04-14 21:33:37 +000015 def __init__(self, url, keycloak, user, password, enterpriseId, siteId):
Shad Ansari3fd05602022-03-28 20:32:37 -070016 self.url = url
Shad Ansari2948cc02022-04-14 21:33:37 +000017 self.keycloak = keycloak
shadf64b92a2022-01-13 19:06:29 +000018 self.user = user
19 self.password = password
20 self.key = self.get_key()
Shad Ansari3fd05602022-03-28 20:32:37 -070021 self.enterpriseId = enterpriseId
22 self.siteId = siteId
23 self.update()
Shad Ansari3a48b642021-12-01 22:42:19 +000024
shadf64b92a2022-01-13 19:06:29 +000025 def headers(self):
26 h = CaseInsensitiveDict()
27 h["Content-Type"] = "application/json"
28 h["Authorization"] = "Bearer " + self.key
29 return h
30
Shad Ansari3fd05602022-03-28 20:32:37 -070031 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
shadf64b92a2022-01-13 19:06:29 +000046 def get_mbr(self, device_group):
Shad Ansari3fd05602022-03-28 20:32:37 -070047 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000048 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 Ansari3fd05602022-03-28 20:32:37 -070057 m = {"downlink": 10000000, "uplink": mbr}
58 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000059 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 Ansari2948cc02022-04-14 21:33:37 +000068 # sys.exit()
shadf64b92a2022-01-13 19:06:29 +000069
70 def get_key(self):
shadf64b92a2022-01-13 19:06:29 +000071 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 Ansari2948cc02022-04-14 21:33:37 +000080 response = requests.post(self.keycloak, data, headers)
shadf64b92a2022-01-13 19:06:29 +000081 key = json.loads(response.text)['access_token']
82 return key
Shad Ansari3a48b642021-12-01 22:42:19 +000083
Shad Ansari3a48b642021-12-01 22:42:19 +000084if __name__ == '__main__':
Shad Ansari5e8d0692021-12-08 19:09:34 +000085
Shad Ansari3fd05602022-03-28 20:32:37 -070086 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
shadf64b92a2022-01-13 19:06:29 +000093 # use valid keycloak user/password
Shad Ansari2948cc02022-04-14 21:33:37 +000094 url = "http://roc.aetherproject.net/aether-roc-api/aether/v2.0.0/connectivity-service-v2/"
Shad Ansari3fd05602022-03-28 20:32:37 -070095 user = sys.argv[1]
96 password = sys.argv[2]
Shad Ansari2948cc02022-04-14 21:33:37 +000097 enterprise = 'ase-4g-enterprise'
98 site = 'ase-site-4g'
99 keycloak = "http://keycloak.aetherproject.net/auth/realms/aether/protocol/openid-connect/token"
Shad Ansari3fd05602022-03-28 20:32:37 -0700100
Shad Ansari2948cc02022-04-14 21:33:37 +0000101 roc = Roc(url, keycloak, user, password, enterprise, site)
shadf64b92a2022-01-13 19:06:29 +0000102
Shad Ansari2948cc02022-04-14 21:33:37 +0000103 cameras = "ase-4g-device-group2"
Shad Ansari3fd05602022-03-28 20:32:37 -0700104 mbr = roc.get_mbr(cameras)
105 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
shadf64b92a2022-01-13 19:06:29 +0000106
Shad Ansari2948cc02022-04-14 21:33:37 +0000107 roc.set_mbr(cameras, 6000000)
shadf64b92a2022-01-13 19:06:29 +0000108 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000109 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
Shad Ansari3fd05602022-03-28 20:32:37 -0700110
shadf64b92a2022-01-13 19:06:29 +0000111 roc.set_mbr(cameras, 10000000)
112 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000113 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))