blob: be609bf7d34daab5f86643766bcc3c491ec73c66 [file] [log] [blame]
Shad Ansari6fcfa292022-01-28 00:34:13 +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
Shad Ansarid88692c2022-02-01 22:47:43 +00008# noinspection PyUnresolvedReferences
Shad Ansari6fcfa292022-01-28 00:34:13 +00009from requests.structures import CaseInsensitiveDict
10import logging as log
11import pyaml
Shad Ansariae3903e2022-02-05 01:03:01 +000012from datetime import datetime
Shad Ansari6fcfa292022-01-28 00:34:13 +000013
Shad Ansarid88692c2022-02-01 22:47:43 +000014from device import Device
15
Shad Ansari6fcfa292022-01-28 00:34:13 +000016URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
17
18
19# URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
20
21
22class Roc(object):
23
24 def __init__(self, user, password):
25 self.user = user
26 self.password = password
27 self.key = self.get_key()
28
29 def headers(self):
30 h = CaseInsensitiveDict()
31 h["Content-Type"] = "application/json"
32 h["Authorization"] = "Bearer " + self.key
33 return h
34
35 def get_mbr(self, device_group):
36 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
37 response = requests.get(url, headers=self.headers())
38 if response.status_code != 200:
39 log.error("Failed to get mbr, status_code: {}".format(response.status_code))
40 return None
41 mbr = json.loads(response.text)
42 return mbr
43
44 def set_mbr(self, device_group, mbr):
45 log.info("Set {} mbr to {}".format(device_group, mbr))
46 m = {'uplink': mbr}
47 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
48 response = requests.post(url, headers=self.headers(), json=m)
49
50 # If error, renew api key and try again
51 if response.status_code != 201:
52 log.info("Renew ROC api key")
53 self.key = self.get_key()
54 response = requests.post(url, headers=self.headers(), json=m)
55 if response.status_code != 201:
56 log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr,
57 response.status_code))
Shad Ansarid88692c2022-02-01 22:47:43 +000058 # noinspection PyUnresolvedReferences
Shad Ansari6fcfa292022-01-28 00:34:13 +000059 sys.exit()
60
61 def get_key(self):
62 url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
63 headers = CaseInsensitiveDict()
64 headers["Content-Type"] = "application/x-www-form-urlencoded"
65 data = {
66 'grant_type': 'password',
67 'client_id': 'aether-roc-gui',
68 'username': self.user,
69 'password': self.password,
70 'scope': 'openid profile email groups'
71 }
72 response = requests.post(url, data, headers)
73 key = json.loads(response.text)['access_token']
74 return key
75
76 def get_enterprise(self):
77 url = URL + "enterprise"
78 response = requests.get(url, headers=self.headers())
79 if response.status_code != 200:
80 log.error("get_enterprise() failed, status_code: {}".format(response.status_code))
81 return None
82 return json.loads(response.text)
83
84 def get_site(self):
85 url = URL + "site"
86 response = requests.get(url, headers=self.headers())
87 if response.status_code != 200:
88 log.error("get_site() failed, status_code: {}".format(response.status_code))
89 return None
90 return json.loads(response.text)
91
92 def get_upf(self):
93 url = URL + "upf"
94 response = requests.get(url, headers=self.headers())
95 if response.status_code != 200:
96 log.error("get_upf() failed, status_code: {}".format(response.status_code))
97 return None
98 return json.loads(response.text)
99
Shad Ansarid88692c2022-02-01 22:47:43 +0000100 def get_devicegroups(self):
Shad Ansari6fcfa292022-01-28 00:34:13 +0000101 url = URL + "device-group"
102 response = requests.get(url, headers=self.headers())
103 if response.status_code != 200:
Shad Ansarid88692c2022-02-01 22:47:43 +0000104 log.error("get_devicegroups() failed, status_code: {}".format(response.status_code))
Shad Ansari6fcfa292022-01-28 00:34:13 +0000105 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000106 return json.loads(response.text)['device-group']
Shad Ansari6fcfa292022-01-28 00:34:13 +0000107
108 def get_ipdomain(self):
109 url = URL + "ip-domain"
110 response = requests.get(url, headers=self.headers())
111 if response.status_code != 200:
112 log.error("get_ipdomain() failed, status_code: {}".format(response.status_code))
113 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000114 return json.loads(response.text)['ip-domain']
115
Shad Ansarid88692c2022-02-01 22:47:43 +0000116 def get_devices(self):
Shad Ansari500f9a02022-02-04 21:15:24 +0000117 devices = {}
118 for dg in self.get_devicegroups():
119 for d in dg['imsis']:
120 devices[d['imsi-id']] = d['imsi-range-from']
121 return devices
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000122
Shad Ansarid88692c2022-02-01 22:47:43 +0000123 def update_devices(self, devices):
Shad Ansariae3903e2022-02-05 01:03:01 +0000124 new = {}
Shad Ansarid88692c2022-02-01 22:47:43 +0000125 dgs = self.get_devicegroups()
126 for dg in dgs:
127 for d in dg['imsis']:
Shad Ansariae3903e2022-02-05 01:03:01 +0000128 imsi_id = str(d['imsi-id'])
129 imsi = str(d['imsi-range-from'])
130 last_reachable = datetime.min
131 if imsi_id in devices:
132 last_reachable = devices[imsi_id].last_reachable
133 new[imsi_id] = Device(imsi_id, imsi, last_reachable)
134 return new
Shad Ansarid88692c2022-02-01 22:47:43 +0000135
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000136 def get_subnet(self):
137 subnets = {}
138 ip_domains = self.get_ipdomain()
139 for ip_domain in ip_domains:
140 subnets[ip_domain['id']] = ip_domain['subnet']
141 return subnets
142
143
144 def dump(self):
145 print(pyaml.dump(self.get_enterprise()))
146 print(pyaml.dump(self.get_site()))
147 print(pyaml.dump(self.get_upf()))
Shad Ansarid88692c2022-02-01 22:47:43 +0000148 print(pyaml.dump(self.get_devicegroups()))
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000149 print(pyaml.dump(self.get_ipdomain()))
150
Shad Ansari6fcfa292022-01-28 00:34:13 +0000151
152
153if __name__ == '__main__':
154 # use valid keycloak user/password
155 roc = Roc("user", "password")
156
157 print(pyaml.dump(roc.get_enterprise()))
158 print(pyaml.dump(roc.get_site()))
159 print(pyaml.dump(roc.get_upf()))
Shad Ansarid88692c2022-02-01 22:47:43 +0000160 print(pyaml.dump(roc.get_devicegroups()))
Shad Ansari6fcfa292022-01-28 00:34:13 +0000161 print(pyaml.dump(roc.get_ipdomain()))
162
163 cameras = "menlo-4g-cameras"
164
165 mbr = roc.get_mbr(cameras)
166 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
167 roc.set_mbr(cameras, 5000000)
168 mbr = roc.get_mbr(cameras)
169 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
170 roc.set_mbr(cameras, 10000000)
171 mbr = roc.get_mbr(cameras)
172 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))