blob: 003d121eb8c4edd37fe13801032b2caf458cbb50 [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
6import requests
7import json
8from requests.structures import CaseInsensitiveDict
shadf64b92a2022-01-13 19:06:29 +00009import logging as log
Shad Ansari3a48b642021-12-01 22:42:19 +000010
Shad Ansari3a48b642021-12-01 22:42:19 +000011
Shad Ansariab4141c2021-12-03 03:33:18 +000012URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
13#URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
14
Shad Ansariab4141c2021-12-03 03:33:18 +000015
shadf64b92a2022-01-13 19:06:29 +000016class Roc(object):
Shad Ansari3a48b642021-12-01 22:42:19 +000017
shadf64b92a2022-01-13 19:06:29 +000018 def __init__(self, user, password):
19 self.user = user
20 self.password = password
21 self.key = self.get_key()
Shad Ansari3a48b642021-12-01 22:42:19 +000022
shadf64b92a2022-01-13 19:06:29 +000023 def headers(self):
24 h = CaseInsensitiveDict()
25 h["Content-Type"] = "application/json"
26 h["Authorization"] = "Bearer " + self.key
27 return h
28
29 def get_mbr(self, device_group):
30 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
31 response = requests.get(url, headers=self.headers())
32 if response.status_code != 200:
33 log.error("Failed to get mbr, status_code: {}".format(response.status_code))
34 return None
35 mbr = json.loads(response.text)
36 return mbr
37
38 def set_mbr(self, device_group, mbr):
39 log.info("Set {} mbr to {}".format(device_group, mbr))
40 m = {'uplink' : mbr}
41 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
42 response = requests.post(url, headers=self.headers(), json=m)
43
44 # If error, renew api key and try again
45 if response.status_code != 201:
46 log.info("Renew ROC api key")
47 self.key = self.get_key()
48 response = requests.post(url, headers=self.headers(), json=m)
49 if response.status_code != 201:
50 log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr, response.status_code))
51 sys.exit()
52
53 def get_key(self):
54 url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
55 headers = CaseInsensitiveDict()
56 headers["Content-Type"] = "application/x-www-form-urlencoded"
57 data = {
58 'grant_type': 'password',
59 'client_id': 'aether-roc-gui',
60 'username': self.user,
61 'password': self.password,
62 'scope': 'openid profile email groups'
63 }
64 response = requests.post(url, data, headers)
65 key = json.loads(response.text)['access_token']
66 return key
Shad Ansari3a48b642021-12-01 22:42:19 +000067
Shad Ansari3a48b642021-12-01 22:42:19 +000068if __name__ == '__main__':
Shad Ansari5e8d0692021-12-08 19:09:34 +000069
shadf64b92a2022-01-13 19:06:29 +000070 # use valid keycloak user/password
71 roc = Roc("user", "password")
72
73 cameras = "menlo-4g-cameras"
74
75 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +000076 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
shadf64b92a2022-01-13 19:06:29 +000077 roc.set_mbr(cameras, 5000000)
78 mbr = roc.get_mbr(cameras)
Shad Ansariab4141c2021-12-03 03:33:18 +000079 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
shadf64b92a2022-01-13 19:06:29 +000080 roc.set_mbr(cameras, 10000000)
81 mbr = roc.get_mbr(cameras)
Shad Ansari3a48b642021-12-01 22:42:19 +000082 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))