blob: 07273a5488b9508dbbc3ecb4a0a7e2ffd13c1323 [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
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 +000016
17class Roc(object):
18
Shad Ansari0508ddf2022-03-24 03:16:51 +000019 def __init__(self, url, user, password, enterpriseId, siteId):
20 self.url = url
Shad Ansari6fcfa292022-01-28 00:34:13 +000021 self.user = user
22 self.password = password
23 self.key = self.get_key()
Shad Ansari0508ddf2022-03-24 03:16:51 +000024 self.enterpriseId = enterpriseId
25 self.siteId = siteId
26
27 self.enterprise = None
28 self.update()
Shad Ansari6fcfa292022-01-28 00:34:13 +000029
30 def headers(self):
31 h = CaseInsensitiveDict()
32 h["Content-Type"] = "application/json"
33 h["Authorization"] = "Bearer " + self.key
34 return h
35
Shad Ansari6fcfa292022-01-28 00:34:13 +000036 def get_key(self):
37 url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
38 headers = CaseInsensitiveDict()
39 headers["Content-Type"] = "application/x-www-form-urlencoded"
40 data = {
41 'grant_type': 'password',
42 'client_id': 'aether-roc-gui',
43 'username': self.user,
44 'password': self.password,
45 'scope': 'openid profile email groups'
46 }
47 response = requests.post(url, data, headers)
48 key = json.loads(response.text)['access_token']
49 return key
50
Shad Ansari0508ddf2022-03-24 03:16:51 +000051 def update(self):
52 url = self.url + "enterprises"
Shad Ansari6fcfa292022-01-28 00:34:13 +000053 response = requests.get(url, headers=self.headers())
54 if response.status_code != 200:
55 log.error("get_enterprise() failed, status_code: {}".format(response.status_code))
56 return None
Shad Ansari0508ddf2022-03-24 03:16:51 +000057
58 result = json.loads(response.text)
59 enterprises = result['enterprise']
60
61 for enterprise in enterprises:
62 for k, v in enterprise.items():
63 if k == 'enterprise-id' and v == self.enterpriseId:
64 self.enterprise = enterprise
Shad Ansari6fcfa292022-01-28 00:34:13 +000065
66 def get_site(self):
Shad Ansari0508ddf2022-03-24 03:16:51 +000067 sites = self.enterprise['site']
68 for site in sites:
69 for k, v in site.items():
70 if k == 'site-id' and v == self.siteId:
71 return site
72 return None
Shad Ansari6fcfa292022-01-28 00:34:13 +000073
Shad Ansari0508ddf2022-03-24 03:16:51 +000074 def devices(self):
75 site = self.get_site()
76 devices = site['device']
77 for device in devices:
78 yield device
Shad Ansari6fcfa292022-01-28 00:34:13 +000079
Shad Ansari0508ddf2022-03-24 03:16:51 +000080 def get_imsi(self, simId):
81 site = self.get_site()
82 sims = site['sim-card']
83 for sim in sims:
84 if sim['sim-id'] == simId:
85 return str(sim['imsi'])
86 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +000087
Shad Ansarid88692c2022-02-01 22:47:43 +000088 def update_devices(self, devices):
Shad Ansari0508ddf2022-03-24 03:16:51 +000089 self.update()
Shad Ansariae3903e2022-02-05 01:03:01 +000090 new = {}
Shad Ansari0508ddf2022-03-24 03:16:51 +000091 for d in self.devices():
92 imsi_id = str(d['device-id'])
93 last_reachable = datetime.min
94 imsi = self.get_imsi(d['sim-card'])
95 if imsi_id in devices:
96 last_reachable = devices[imsi_id].last_reachable
97 new[imsi_id] = Device(imsi_id, imsi, last_reachable)
Shad Ansariae3903e2022-02-05 01:03:01 +000098 return new
Shad Ansarid88692c2022-02-01 22:47:43 +000099
Shad Ansari6fcfa292022-01-28 00:34:13 +0000100
101if __name__ == '__main__':
Shad Ansari0508ddf2022-03-24 03:16:51 +0000102 url = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v2.0.0/connectivity-service-v2/"
103 user = sys.argv[1]
104 password = sys.argv[2]
105 enterprise = 'aether-onf'
106 site = 'menlo-4g'
107 roc = Roc(url, user, password, enterprise, site)
108 devices = roc.update_devices({})
109 for device in devices.values():
110 print(device)