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