blob: a5f38dff9f73f0fd5f6282f4a6896d3c72aa9422 [file] [log] [blame]
Shad Ansari3a48b642021-12-01 22:42:19 +00001"""
Shad Ansarif53bc4f2022-06-07 14:31:11 -07002SPDX-FileCopyrightText: 2022-present Intel Corporation
Shad Ansari3a48b642021-12-01 22:42:19 +00003SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
Shad Ansarif53bc4f2022-06-07 14:31:11 -07004SPDX-License-Identifier: Apache-2.0
Shad Ansari3a48b642021-12-01 22:42:19 +00005"""
6
Shad Ansari3fd05602022-03-28 20:32:37 -07007import sys
Shad Ansari3a48b642021-12-01 22:42:19 +00008import requests
9import json
10from requests.structures import CaseInsensitiveDict
shadf64b92a2022-01-13 19:06:29 +000011import logging as log
Shad Ansari3a48b642021-12-01 22:42:19 +000012
Shad Ansari3a48b642021-12-01 22:42:19 +000013
shadf64b92a2022-01-13 19:06:29 +000014class Roc(object):
Shad Ansari3a48b642021-12-01 22:42:19 +000015
Shad Ansari2948cc02022-04-14 21:33:37 +000016 def __init__(self, url, keycloak, user, password, enterpriseId, siteId):
Shad Ansari3fd05602022-03-28 20:32:37 -070017 self.url = url
Shad Ansari2948cc02022-04-14 21:33:37 +000018 self.keycloak = keycloak
shadf64b92a2022-01-13 19:06:29 +000019 self.user = user
20 self.password = password
21 self.key = self.get_key()
Shad Ansari3fd05602022-03-28 20:32:37 -070022 self.enterpriseId = enterpriseId
23 self.siteId = siteId
24 self.update()
Shad Ansari3a48b642021-12-01 22:42:19 +000025
shadf64b92a2022-01-13 19:06:29 +000026 def headers(self):
27 h = CaseInsensitiveDict()
28 h["Content-Type"] = "application/json"
29 h["Authorization"] = "Bearer " + self.key
30 return h
31
Shad Ansari3fd05602022-03-28 20:32:37 -070032 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
shadf64b92a2022-01-13 19:06:29 +000047 def get_mbr(self, device_group):
Shad Ansari3fd05602022-03-28 20:32:37 -070048 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000049 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 Ansari3fd05602022-03-28 20:32:37 -070058 m = {"downlink": 10000000, "uplink": mbr}
59 url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
shadf64b92a2022-01-13 19:06:29 +000060 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 Ansari2948cc02022-04-14 21:33:37 +000069 # sys.exit()
shadf64b92a2022-01-13 19:06:29 +000070
71 def get_key(self):
shadf64b92a2022-01-13 19:06:29 +000072 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 Ansari2948cc02022-04-14 21:33:37 +000081 response = requests.post(self.keycloak, data, headers)
shadf64b92a2022-01-13 19:06:29 +000082 key = json.loads(response.text)['access_token']
83 return key
Shad Ansari3a48b642021-12-01 22:42:19 +000084
Shad Ansari3a48b642021-12-01 22:42:19 +000085if __name__ == '__main__':
Shad Ansari5e8d0692021-12-08 19:09:34 +000086
Shad Ansari3fd05602022-03-28 20:32:37 -070087 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
shadf64b92a2022-01-13 19:06:29 +000094 # use valid keycloak user/password
Shad Ansari2948cc02022-04-14 21:33:37 +000095 url = "http://roc.aetherproject.net/aether-roc-api/aether/v2.0.0/connectivity-service-v2/"
Shad Ansari3fd05602022-03-28 20:32:37 -070096 user = sys.argv[1]
97 password = sys.argv[2]
Shad Ansari2948cc02022-04-14 21:33:37 +000098 enterprise = 'ase-4g-enterprise'
99 site = 'ase-site-4g'
100 keycloak = "http://keycloak.aetherproject.net/auth/realms/aether/protocol/openid-connect/token"
Shad Ansari3fd05602022-03-28 20:32:37 -0700101
Shad Ansari2948cc02022-04-14 21:33:37 +0000102 roc = Roc(url, keycloak, user, password, enterprise, site)
shadf64b92a2022-01-13 19:06:29 +0000103
Shad Ansari2948cc02022-04-14 21:33:37 +0000104 cameras = "ase-4g-device-group2"
Shad Ansari3fd05602022-03-28 20:32:37 -0700105 mbr = roc.get_mbr(cameras)
106 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
shadf64b92a2022-01-13 19:06:29 +0000107
Shad Ansari2948cc02022-04-14 21:33:37 +0000108 roc.set_mbr(cameras, 6000000)
shadf64b92a2022-01-13 19:06:29 +0000109 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000110 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
Shad Ansari3fd05602022-03-28 20:32:37 -0700111
shadf64b92a2022-01-13 19:06:29 +0000112 roc.set_mbr(cameras, 10000000)
113 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +0000114 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))