blob: 6d9060be64960f3e76c5c20bf522cc05da46d9a8 [file] [log] [blame]
Shad Ansari6fcfa292022-01-28 00:34:13 +00001"""
Shad Ansarib046c152022-06-07 14:34:14 -07002SPDX-FileCopyrightText: 2022-present Intel Corporation
Shad Ansari6fcfa292022-01-28 00:34:13 +00003SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
Shad Ansarib046c152022-06-07 14:34:14 -07004SPDX-License-Identifier: Apache-2.0
Shad Ansari6fcfa292022-01-28 00:34:13 +00005"""
6
Shad Ansari907b7712022-02-07 21:48:04 +00007import sys
Shad Ansari6fcfa292022-01-28 00:34:13 +00008import requests
9import json
Shad Ansarid88692c2022-02-01 22:47:43 +000010# noinspection PyUnresolvedReferences
Shad Ansari6fcfa292022-01-28 00:34:13 +000011from requests.structures import CaseInsensitiveDict
12import logging as log
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 +000017
18class Roc(object):
19
Shad Ansari0508ddf2022-03-24 03:16:51 +000020 def __init__(self, url, user, password, enterpriseId, siteId):
21 self.url = url
Shad Ansari6fcfa292022-01-28 00:34:13 +000022 self.user = user
23 self.password = password
24 self.key = self.get_key()
Shad Ansari0508ddf2022-03-24 03:16:51 +000025 self.enterpriseId = enterpriseId
26 self.siteId = siteId
27
28 self.enterprise = None
29 self.update()
Shad Ansari6fcfa292022-01-28 00:34:13 +000030
31 def headers(self):
32 h = CaseInsensitiveDict()
33 h["Content-Type"] = "application/json"
34 h["Authorization"] = "Bearer " + self.key
35 return h
36
Shad Ansari6fcfa292022-01-28 00:34:13 +000037 def get_key(self):
38 url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
39 headers = CaseInsensitiveDict()
40 headers["Content-Type"] = "application/x-www-form-urlencoded"
41 data = {
42 'grant_type': 'password',
43 'client_id': 'aether-roc-gui',
44 'username': self.user,
45 'password': self.password,
46 'scope': 'openid profile email groups'
47 }
48 response = requests.post(url, data, headers)
49 key = json.loads(response.text)['access_token']
50 return key
51
Shad Ansari0508ddf2022-03-24 03:16:51 +000052 def update(self):
53 url = self.url + "enterprises"
Shad Ansari6fcfa292022-01-28 00:34:13 +000054 response = requests.get(url, headers=self.headers())
55 if response.status_code != 200:
56 log.error("get_enterprise() failed, status_code: {}".format(response.status_code))
57 return None
Shad Ansari0508ddf2022-03-24 03:16:51 +000058
59 result = json.loads(response.text)
60 enterprises = result['enterprise']
61
62 for enterprise in enterprises:
63 for k, v in enterprise.items():
64 if k == 'enterprise-id' and v == self.enterpriseId:
65 self.enterprise = enterprise
Shad Ansari6fcfa292022-01-28 00:34:13 +000066
67 def get_site(self):
Shad Ansari0508ddf2022-03-24 03:16:51 +000068 sites = self.enterprise['site']
69 for site in sites:
70 for k, v in site.items():
71 if k == 'site-id' and v == self.siteId:
72 return site
73 return None
Shad Ansari6fcfa292022-01-28 00:34:13 +000074
Shad Ansari0508ddf2022-03-24 03:16:51 +000075 def devices(self):
76 site = self.get_site()
77 devices = site['device']
78 for device in devices:
79 yield device
Shad Ansari6fcfa292022-01-28 00:34:13 +000080
Shad Ansari0508ddf2022-03-24 03:16:51 +000081 def get_imsi(self, simId):
82 site = self.get_site()
83 sims = site['sim-card']
84 for sim in sims:
85 if sim['sim-id'] == simId:
86 return str(sim['imsi'])
87 return None
Shad Ansari5b9d1f52022-01-29 01:42:45 +000088
Shad Ansarid88692c2022-02-01 22:47:43 +000089 def update_devices(self, devices):
Shad Ansari0508ddf2022-03-24 03:16:51 +000090 self.update()
Shad Ansariae3903e2022-02-05 01:03:01 +000091 new = {}
Shad Ansari0508ddf2022-03-24 03:16:51 +000092 for d in self.devices():
93 imsi_id = str(d['device-id'])
94 last_reachable = datetime.min
95 imsi = self.get_imsi(d['sim-card'])
96 if imsi_id in devices:
97 last_reachable = devices[imsi_id].last_reachable
98 new[imsi_id] = Device(imsi_id, imsi, last_reachable)
Shad Ansariae3903e2022-02-05 01:03:01 +000099 return new
Shad Ansarid88692c2022-02-01 22:47:43 +0000100
Shad Ansari6fcfa292022-01-28 00:34:13 +0000101
102if __name__ == '__main__':
Shad Ansari0508ddf2022-03-24 03:16:51 +0000103 url = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v2.0.0/connectivity-service-v2/"
104 user = sys.argv[1]
105 password = sys.argv[2]
106 enterprise = 'aether-onf'
107 site = 'menlo-4g'
108 roc = Roc(url, user, password, enterprise, site)
109 devices = roc.update_devices({})
110 for device in devices.values():
111 print(device)