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 | |
| 6 | import requests |
| 7 | import json |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 8 | # noinspection PyUnresolvedReferences |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 9 | from requests.structures import CaseInsensitiveDict |
| 10 | import logging as log |
| 11 | import pyaml |
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 | URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/" |
| 17 | |
| 18 | |
| 19 | # URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/" |
| 20 | |
| 21 | |
| 22 | class Roc(object): |
| 23 | |
| 24 | def __init__(self, user, password): |
| 25 | self.user = user |
| 26 | self.password = password |
| 27 | self.key = self.get_key() |
| 28 | |
| 29 | def headers(self): |
| 30 | h = CaseInsensitiveDict() |
| 31 | h["Content-Type"] = "application/json" |
| 32 | h["Authorization"] = "Bearer " + self.key |
| 33 | return h |
| 34 | |
| 35 | def get_mbr(self, device_group): |
| 36 | url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group) |
| 37 | response = requests.get(url, headers=self.headers()) |
| 38 | if response.status_code != 200: |
| 39 | log.error("Failed to get mbr, status_code: {}".format(response.status_code)) |
| 40 | return None |
| 41 | mbr = json.loads(response.text) |
| 42 | return mbr |
| 43 | |
| 44 | def set_mbr(self, device_group, mbr): |
| 45 | log.info("Set {} mbr to {}".format(device_group, mbr)) |
| 46 | m = {'uplink': mbr} |
| 47 | url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group) |
| 48 | response = requests.post(url, headers=self.headers(), json=m) |
| 49 | |
| 50 | # If error, renew api key and try again |
| 51 | if response.status_code != 201: |
| 52 | log.info("Renew ROC api key") |
| 53 | self.key = self.get_key() |
| 54 | response = requests.post(url, headers=self.headers(), json=m) |
| 55 | if response.status_code != 201: |
| 56 | log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr, |
| 57 | response.status_code)) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 58 | # noinspection PyUnresolvedReferences |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 59 | sys.exit() |
| 60 | |
| 61 | def get_key(self): |
| 62 | url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token" |
| 63 | headers = CaseInsensitiveDict() |
| 64 | headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 65 | data = { |
| 66 | 'grant_type': 'password', |
| 67 | 'client_id': 'aether-roc-gui', |
| 68 | 'username': self.user, |
| 69 | 'password': self.password, |
| 70 | 'scope': 'openid profile email groups' |
| 71 | } |
| 72 | response = requests.post(url, data, headers) |
| 73 | key = json.loads(response.text)['access_token'] |
| 74 | return key |
| 75 | |
| 76 | def get_enterprise(self): |
| 77 | url = URL + "enterprise" |
| 78 | response = requests.get(url, headers=self.headers()) |
| 79 | if response.status_code != 200: |
| 80 | log.error("get_enterprise() failed, status_code: {}".format(response.status_code)) |
| 81 | return None |
| 82 | return json.loads(response.text) |
| 83 | |
| 84 | def get_site(self): |
| 85 | url = URL + "site" |
| 86 | response = requests.get(url, headers=self.headers()) |
| 87 | if response.status_code != 200: |
| 88 | log.error("get_site() failed, status_code: {}".format(response.status_code)) |
| 89 | return None |
| 90 | return json.loads(response.text) |
| 91 | |
| 92 | def get_upf(self): |
| 93 | url = URL + "upf" |
| 94 | response = requests.get(url, headers=self.headers()) |
| 95 | if response.status_code != 200: |
| 96 | log.error("get_upf() failed, status_code: {}".format(response.status_code)) |
| 97 | return None |
| 98 | return json.loads(response.text) |
| 99 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 100 | def get_devicegroups(self): |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 101 | url = URL + "device-group" |
| 102 | response = requests.get(url, headers=self.headers()) |
| 103 | if response.status_code != 200: |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 104 | log.error("get_devicegroups() failed, status_code: {}".format(response.status_code)) |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 105 | return None |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 106 | return json.loads(response.text)['device-group'] |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 107 | |
| 108 | def get_ipdomain(self): |
| 109 | url = URL + "ip-domain" |
| 110 | response = requests.get(url, headers=self.headers()) |
| 111 | if response.status_code != 200: |
| 112 | log.error("get_ipdomain() failed, status_code: {}".format(response.status_code)) |
| 113 | return None |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 114 | return json.loads(response.text)['ip-domain'] |
| 115 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 116 | def get_devices(self): |
Shad Ansari | 500f9a0 | 2022-02-04 21:15:24 +0000 | [diff] [blame] | 117 | devices = {} |
| 118 | for dg in self.get_devicegroups(): |
| 119 | for d in dg['imsis']: |
| 120 | devices[d['imsi-id']] = d['imsi-range-from'] |
| 121 | return devices |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 122 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 123 | def update_devices(self, devices): |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame^] | 124 | new = {} |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 125 | dgs = self.get_devicegroups() |
| 126 | for dg in dgs: |
| 127 | for d in dg['imsis']: |
Shad Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame^] | 128 | imsi_id = str(d['imsi-id']) |
| 129 | imsi = str(d['imsi-range-from']) |
| 130 | last_reachable = datetime.min |
| 131 | if imsi_id in devices: |
| 132 | last_reachable = devices[imsi_id].last_reachable |
| 133 | new[imsi_id] = Device(imsi_id, imsi, last_reachable) |
| 134 | return new |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 135 | |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 136 | def get_subnet(self): |
| 137 | subnets = {} |
| 138 | ip_domains = self.get_ipdomain() |
| 139 | for ip_domain in ip_domains: |
| 140 | subnets[ip_domain['id']] = ip_domain['subnet'] |
| 141 | return subnets |
| 142 | |
| 143 | |
| 144 | def dump(self): |
| 145 | print(pyaml.dump(self.get_enterprise())) |
| 146 | print(pyaml.dump(self.get_site())) |
| 147 | print(pyaml.dump(self.get_upf())) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 148 | print(pyaml.dump(self.get_devicegroups())) |
Shad Ansari | 5b9d1f5 | 2022-01-29 01:42:45 +0000 | [diff] [blame] | 149 | print(pyaml.dump(self.get_ipdomain())) |
| 150 | |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | # use valid keycloak user/password |
| 155 | roc = Roc("user", "password") |
| 156 | |
| 157 | print(pyaml.dump(roc.get_enterprise())) |
| 158 | print(pyaml.dump(roc.get_site())) |
| 159 | print(pyaml.dump(roc.get_upf())) |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 160 | print(pyaml.dump(roc.get_devicegroups())) |
Shad Ansari | 6fcfa29 | 2022-01-28 00:34:13 +0000 | [diff] [blame] | 161 | print(pyaml.dump(roc.get_ipdomain())) |
| 162 | |
| 163 | cameras = "menlo-4g-cameras" |
| 164 | |
| 165 | mbr = roc.get_mbr(cameras) |
| 166 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
| 167 | roc.set_mbr(cameras, 5000000) |
| 168 | mbr = roc.get_mbr(cameras) |
| 169 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |
| 170 | roc.set_mbr(cameras, 10000000) |
| 171 | mbr = roc.get_mbr(cameras) |
| 172 | print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"])) |