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