Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 1 | """ |
| 2 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 3 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 4 | """ |
| 5 | |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 6 | import sys |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 7 | import requests |
| 8 | import json |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 9 | # noinspection PyUnresolvedReferences |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 10 | from requests.structures import CaseInsensitiveDict |
| 11 | import logging as log |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 12 | from datetime import datetime |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 13 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 14 | from device import Device |
| 15 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 16 | |
| 17 | class Roc(object): |
| 18 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 19 | def __init__(self, url, user, password, enterpriseId, siteId): |
| 20 | self.url = url |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 21 | self.user = user |
| 22 | self.password = password |
| 23 | self.key = self.get_key() |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 24 | self.enterpriseId = enterpriseId |
| 25 | self.siteId = siteId |
| 26 | |
| 27 | self.enterprise = None |
| 28 | self.update() |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 29 | |
| 30 | def headers(self): |
| 31 | h = CaseInsensitiveDict() |
| 32 | h["Content-Type"] = "application/json" |
| 33 | h["Authorization"] = "Bearer " + self.key |
| 34 | return h |
| 35 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 36 | 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 Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 51 | def update(self): |
| 52 | url = self.url + "enterprises" |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 53 | 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 Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 57 | |
| 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 Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 65 | |
| 66 | def get_site(self): |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 67 | 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 Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 73 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 74 | def devices(self): |
| 75 | site = self.get_site() |
| 76 | devices = site['device'] |
| 77 | for device in devices: |
| 78 | yield device |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 79 | |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 80 | 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 Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 87 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 88 | def update_devices(self, devices): |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 89 | self.update() |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 90 | new = {} |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 91 | 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 Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 98 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 99 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
Shad Ansari | 0508ddf | 2022-03-24 03:16:51 +0000 | [diff] [blame] | 102 | 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) |