Adjust to new roc api
Change-Id: I67dc8d45ec6421d957af697b2f650c66b7c73a3d
diff --git a/roc.py b/roc.py
index b193a2a..07273a5 100644
--- a/roc.py
+++ b/roc.py
@@ -9,23 +9,23 @@
# noinspection PyUnresolvedReferences
from requests.structures import CaseInsensitiveDict
import logging as log
-import pyaml
from datetime import datetime
from device import Device
-URL = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
-
-
-# URL = "https://roc.staging.aether.onlab.us/aether-roc-api/aether/v4.0.0/connectivity-service-v4/"
-
class Roc(object):
- def __init__(self, user, password):
+ def __init__(self, url, user, password, enterpriseId, siteId):
+ self.url = url
self.user = user
self.password = password
self.key = self.get_key()
+ self.enterpriseId = enterpriseId
+ self.siteId = siteId
+
+ self.enterprise = None
+ self.update()
def headers(self):
h = CaseInsensitiveDict()
@@ -33,32 +33,6 @@
h["Authorization"] = "Bearer " + self.key
return h
- def get_mbr(self, device_group):
- url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
- response = requests.get(url, headers=self.headers())
- if response.status_code != 200:
- log.error("Failed to get mbr, status_code: {}".format(response.status_code))
- return None
- mbr = json.loads(response.text)
- return mbr
-
- def set_mbr(self, device_group, mbr):
- log.info("Set {} mbr to {}".format(device_group, mbr))
- m = {'uplink': mbr}
- url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
- response = requests.post(url, headers=self.headers(), json=m)
-
- # If error, renew api key and try again
- if response.status_code != 201:
- log.info("Renew ROC api key")
- self.key = self.get_key()
- response = requests.post(url, headers=self.headers(), json=m)
- if response.status_code != 201:
- log.error("Failed to set mbr, device_group:{}, mbr:{}, status_code: {}".format(device_group, mbr,
- response.status_code))
- # noinspection PyUnresolvedReferences
- sys.exit()
-
def get_key(self):
url = "https://keycloak.opennetworking.org/auth/realms/master/protocol/openid-connect/token"
headers = CaseInsensitiveDict()
@@ -74,110 +48,63 @@
key = json.loads(response.text)['access_token']
return key
- def get_enterprise(self):
- url = URL + "enterprise"
+ def update(self):
+ url = self.url + "enterprises"
response = requests.get(url, headers=self.headers())
if response.status_code != 200:
log.error("get_enterprise() failed, status_code: {}".format(response.status_code))
return None
- return json.loads(response.text)
+
+ result = json.loads(response.text)
+ enterprises = result['enterprise']
+
+ for enterprise in enterprises:
+ for k, v in enterprise.items():
+ if k == 'enterprise-id' and v == self.enterpriseId:
+ self.enterprise = enterprise
def get_site(self):
- url = URL + "site"
- response = requests.get(url, headers=self.headers())
- if response.status_code != 200:
- log.error("get_site() failed, status_code: {}".format(response.status_code))
- return None
- return json.loads(response.text)
+ sites = self.enterprise['site']
+ for site in sites:
+ for k, v in site.items():
+ if k == 'site-id' and v == self.siteId:
+ return site
+ return None
- def get_upf(self):
- url = URL + "upf"
- response = requests.get(url, headers=self.headers())
- if response.status_code != 200:
- log.error("get_upf() failed, status_code: {}".format(response.status_code))
- return None
- return json.loads(response.text)
+ def devices(self):
+ site = self.get_site()
+ devices = site['device']
+ for device in devices:
+ yield device
- def get_devicegroups(self):
- url = URL + "device-group"
- try:
- response = requests.get(url, headers=self.headers(), timeout=10)
- except requests.ReadTimeout as e:
- log.error("ROC request timeout, error={}".format(e))
- sys.exit()
- if response.status_code != 200:
- log.info("Renew ROC api key")
- self.key = self.get_key()
- response = requests.get(url, headers=self.headers(), timeout=10)
- if response.status_code != 200:
- log.error("get_devicegroups() failed, status_code: {}".format(response.status_code))
- sys.exit()
- return None
- return json.loads(response.text)['device-group']
-
- def get_ipdomain(self):
- url = URL + "ip-domain"
- response = requests.get(url, headers=self.headers())
- if response.status_code != 200:
- log.error("get_ipdomain() failed, status_code: {}".format(response.status_code))
- return None
- return json.loads(response.text)['ip-domain']
-
- def get_devices(self):
- devices = {}
- for dg in self.get_devicegroups():
- for d in dg['imsis']:
- devices[d['imsi-id']] = d['imsi-range-from']
- return devices
+ def get_imsi(self, simId):
+ site = self.get_site()
+ sims = site['sim-card']
+ for sim in sims:
+ if sim['sim-id'] == simId:
+ return str(sim['imsi'])
+ return None
def update_devices(self, devices):
+ self.update()
new = {}
- dgs = self.get_devicegroups()
- if dgs is not None:
- for dg in dgs:
- for d in dg['imsis']:
- imsi_id = str(d['imsi-id'])
- imsi = str(d['imsi-range-from'])
- last_reachable = datetime.min
- if imsi_id in devices:
- last_reachable = devices[imsi_id].last_reachable
- new[imsi_id] = Device(imsi_id, imsi, last_reachable)
+ for d in self.devices():
+ imsi_id = str(d['device-id'])
+ last_reachable = datetime.min
+ imsi = self.get_imsi(d['sim-card'])
+ if imsi_id in devices:
+ last_reachable = devices[imsi_id].last_reachable
+ new[imsi_id] = Device(imsi_id, imsi, last_reachable)
return new
- def get_subnet(self):
- subnets = {}
- ip_domains = self.get_ipdomain()
- for ip_domain in ip_domains:
- subnets[ip_domain['id']] = ip_domain['subnet']
- return subnets
-
-
- def dump(self):
- print(pyaml.dump(self.get_enterprise()))
- print(pyaml.dump(self.get_site()))
- print(pyaml.dump(self.get_upf()))
- print(pyaml.dump(self.get_devicegroups()))
- print(pyaml.dump(self.get_ipdomain()))
-
-
if __name__ == '__main__':
- # use valid keycloak user/password
- roc = Roc("user", "password")
-
- print(pyaml.dump(roc.get_enterprise()))
- print(pyaml.dump(roc.get_site()))
- print(pyaml.dump(roc.get_upf()))
- print(pyaml.dump(roc.get_devicegroups()))
- print(pyaml.dump(roc.get_ipdomain()))
-
- cameras = "menlo-4g-cameras"
-
- mbr = roc.get_mbr(cameras)
- print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
- roc.set_mbr(cameras, 5000000)
- mbr = roc.get_mbr(cameras)
- print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
- roc.set_mbr(cameras, 10000000)
- mbr = roc.get_mbr(cameras)
- print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
+ url = "https://roc.menlo.aetherproject.org/aether-roc-api/aether/v2.0.0/connectivity-service-v2/"
+ user = sys.argv[1]
+ password = sys.argv[2]
+ enterprise = 'aether-onf'
+ site = 'menlo-4g'
+ roc = Roc(url, user, password, enterprise, site)
+ devices = roc.update_devices({})
+ for device in devices.values():
+ print(device)