blob: 35680a493df3c1260ced50a5b7f6caa788e7cbb7 [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 Ansari3fd05602022-03-28 20:32:37 -070015 def __init__(self, url, user, password, enterpriseId, siteId):
16 self.url = url
shadf64b92a2022-01-13 19:06:29 +000017 self.user = user
18 self.password = password
19 self.key = self.get_key()
Shad Ansari3fd05602022-03-28 20:32:37 -070020 self.enterpriseId = enterpriseId
21 self.siteId = siteId
22 self.update()
Shad Ansari3a48b642021-12-01 22:42:19 +000023
shadf64b92a2022-01-13 19:06:29 +000024 def headers(self):
25 h = CaseInsensitiveDict()
26 h["Content-Type"] = "application/json"
27 h["Authorization"] = "Bearer " + self.key
28 return h
29
Shad Ansari3fd05602022-03-28 20:32:37 -070030 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
shadf64b92a2022-01-13 19:06:29 +000045 def get_mbr(self, device_group):
Shad Ansari3fd05602022-03-28 20:32:37 -070046 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000047 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 Ansari3fd05602022-03-28 20:32:37 -070056 m = {"downlink": 10000000, "uplink": mbr}
57 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000058 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 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 Ansari3fd05602022-03-28 20:32:37 -070094 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)
shadf64b92a2022-01-13 19:06:29 +0000101
102 cameras = "menlo-4g-cameras"
Shad Ansari3fd05602022-03-28 20:32:37 -0700103 mbr = roc.get_mbr(cameras)
104 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
shadf64b92a2022-01-13 19:06:29 +0000105
Shad Ansari3fd05602022-03-28 20:32:37 -0700106 roc.set_mbr(cameras, 7000000)
shadf64b92a2022-01-13 19:06:29 +0000107 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000108 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
Shad Ansari3fd05602022-03-28 20:32:37 -0700109
shadf64b92a2022-01-13 19:06:29 +0000110 roc.set_mbr(cameras, 10000000)
111 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000112 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))