blob: 87ba152e2d628f0b544214bd3354b3db788d5ddc [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
Shad Ansari907b7712022-02-07 21:48:04 +00006import sys
Shad Ansari6fcfa292022-01-28 00:34:13 +00007import requests
8import json
Shad Ansarid88692c2022-02-01 22:47:43 +00009# noinspection PyUnresolvedReferences
Shad Ansari6fcfa292022-01-28 00:34:13 +000010from requests.structures import CaseInsensitiveDict
11import logging as log
12import pyaml
Shad Ansariae3903e2022-02-05 01:03:01 +000013from datetime import datetime
Shad Ansari6fcfa292022-01-28 00:34:13 +000014
Shad Ansarid88692c2022-02-01 22:47:43 +000015from device import Device
16
Shad Ansari6fcfa292022-01-28 00:34:13 +000017URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
18
19
20# URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
21
22
23class Roc(object):
24
25 def __init__(self, user, password):
26 self.user = user
27 self.password = password
28 self.key = self.get_key()
29
30 def headers(self):
31 h = CaseInsensitiveDict()
32 h["Content-Type"] = "application/json"
33 h["Authorization"] = "Bearer " + self.key
34 return h
35
36 def get_mbr(self, device_group):
37 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
38 response = requests.get(url, headers=self.headers())
39 if response.status_code != 200:
40 log.error("Failed to get mbr, status_code: {}".format(response.status_code))
41 return None
42 mbr = json.loads(response.text)
43 return mbr
44
45 def set_mbr(self, device_group, mbr):
46 log.info("Set {} mbr to {}".format(device_group, mbr))
47 m = {'uplink': mbr}
48 url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
49 response = requests.post(url, headers=self.headers(), json=m)
50
51 # If error, renew api key and try again
52 if response.status_code != 201:
53 log.info("Renew ROC api key")
54 self.key = self.get_key()
55 response = requests.post(url, headers=self.headers(), json=m)
56 if response.status_code != 201:
57 log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr,
58 response.status_code))
Shad Ansarid88692c2022-02-01 22:47:43 +000059 # noinspection PyUnresolvedReferences
Shad Ansari6fcfa292022-01-28 00:34:13 +000060 sys.exit()
61
62 def get_key(self):
63 url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
64 headers = CaseInsensitiveDict()
65 headers["Content-Type"] = "application/x-www-form-urlencoded"
66 data = {
67 'grant_type': 'password',
68 'client_id': 'aether-roc-gui',
69 'username': self.user,
70 'password': self.password,
71 'scope': 'openid profile email groups'
72 }
73 response = requests.post(url, data, headers)
74 key = json.loads(response.text)['access_token']
75 return key
76
77 def get_enterprise(self):
78 url = URL + "enterprise"
79 response = requests.get(url, headers=self.headers())
80 if response.status_code != 200:
81 log.error("get_enterprise() failed, status_code: {}".format(response.status_code))
82 return None
83 return json.loads(response.text)
84
85 def get_site(self):
86 url = URL + "site"
87 response = requests.get(url, headers=self.headers())
88 if response.status_code != 200:
89 log.error("get_site() failed, status_code: {}".format(response.status_code))
90 return None
91 return json.loads(response.text)
92
93 def get_upf(self):
94 url = URL + "upf"
95 response = requests.get(url, headers=self.headers())
96 if response.status_code != 200:
97 log.error("get_upf() failed, status_code: {}".format(response.status_code))
98 return None
99 return json.loads(response.text)
100
Shad Ansarid88692c2022-02-01 22:47:43 +0000101 def get_devicegroups(self):
Shad Ansari6fcfa292022-01-28 00:34:13 +0000102 url = URL + "device-group"
Shad Ansari907b7712022-02-07 21:48:04 +0000103 try:
104 response = requests.get(url, headers=self.headers(), timeout=10)
105 except requests.ReadTimeout as e:
106 log.error("ROC request timeout, error={}".format(e))
107 sys.exit()
Shad Ansari6fcfa292022-01-28 00:34:13 +0000108 if response.status_code != 200:
Shad Ansarid88692c2022-02-01 22:47:43 +0000109 log.error("get_devicegroups() failed, status_code: {}".format(response.status_code))
Shad Ansari6fcfa292022-01-28 00:34:13 +0000110 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000111 return json.loads(response.text)['device-group']
Shad Ansari6fcfa292022-01-28 00:34:13 +0000112
113 def get_ipdomain(self):
114 url = URL + "ip-domain"
115 response = requests.get(url, headers=self.headers())
116 if response.status_code != 200:
117 log.error("get_ipdomain() failed, status_code: {}".format(response.status_code))
118 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000119 return json.loads(response.text)['ip-domain']
120
Shad Ansarid88692c2022-02-01 22:47:43 +0000121 def get_devices(self):
Shad Ansari500f9a02022-02-04 21:15:24 +0000122 devices = {}
123 for dg in self.get_devicegroups():
124 for d in dg['imsis']:
125 devices[d['imsi-id']] = d['imsi-range-from']
126 return devices
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000127
Shad Ansarid88692c2022-02-01 22:47:43 +0000128 def update_devices(self, devices):
Shad Ansariae3903e2022-02-05 01:03:01 +0000129 new = {}
Shad Ansarid88692c2022-02-01 22:47:43 +0000130 dgs = self.get_devicegroups()
Shad Ansari907b7712022-02-07 21:48:04 +0000131 if dgs is not None:
132 for dg in dgs:
133 for d in dg['imsis']:
134 imsi_id = str(d['imsi-id'])
135 imsi = str(d['imsi-range-from'])
136 last_reachable = datetime.min
137 if imsi_id in devices:
138 last_reachable = devices[imsi_id].last_reachable
139 new[imsi_id] = Device(imsi_id, imsi, last_reachable)
Shad Ansariae3903e2022-02-05 01:03:01 +0000140 return new
Shad Ansarid88692c2022-02-01 22:47:43 +0000141
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000142 def get_subnet(self):
143 subnets = {}
144 ip_domains = self.get_ipdomain()
145 for ip_domain in ip_domains:
146 subnets[ip_domain['id']] = ip_domain['subnet']
147 return subnets
148
149
150 def dump(self):
151 print(pyaml.dump(self.get_enterprise()))
152 print(pyaml.dump(self.get_site()))
153 print(pyaml.dump(self.get_upf()))
Shad Ansarid88692c2022-02-01 22:47:43 +0000154 print(pyaml.dump(self.get_devicegroups()))
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000155 print(pyaml.dump(self.get_ipdomain()))
156
Shad Ansari6fcfa292022-01-28 00:34:13 +0000157
158
159if __name__ == '__main__':
160 # use valid keycloak user/password
161 roc = Roc("user", "password")
162
163 print(pyaml.dump(roc.get_enterprise()))
164 print(pyaml.dump(roc.get_site()))
165 print(pyaml.dump(roc.get_upf()))
Shad Ansarid88692c2022-02-01 22:47:43 +0000166 print(pyaml.dump(roc.get_devicegroups()))
Shad Ansari6fcfa292022-01-28 00:34:13 +0000167 print(pyaml.dump(roc.get_ipdomain()))
168
169 cameras = "menlo-4g-cameras"
170
171 mbr = roc.get_mbr(cameras)
172 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
173 roc.set_mbr(cameras, 5000000)
174 mbr = roc.get_mbr(cameras)
175 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
176 roc.set_mbr(cameras, 10000000)
177 mbr = roc.get_mbr(cameras)
178 print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))