Use new roc api for setting mbr

Change-Id: Ic48859c4afa2aeaa983554dd27a53193d0e9b0cf
diff --git a/person_detection/roc.py b/person_detection/roc.py
index 003d121..35680a4 100644
--- a/person_detection/roc.py
+++ b/person_detection/roc.py
@@ -3,22 +3,23 @@
 SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
 """
 
+import sys
 import requests
 import json
 from requests.structures import CaseInsensitiveDict
 import logging as log
 
 
-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.update()
 
     def headers(self):
         h = CaseInsensitiveDict()
@@ -26,8 +27,23 @@
         h["Authorization"] = "Bearer " + self.key
         return h
 
+    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
+
+        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_mbr(self, device_group):
-        url = URL + "vcs/vcs/vcs-{}/slice/mbr".format(device_group)
+        url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, 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))
@@ -37,8 +53,8 @@
 
     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)
+        m = {"downlink": 10000000, "uplink": mbr}
+        url = self.url + "enterprises/enterprise/{}/site/{}/device-group/{}/mbr".format(self.enterpriseId, self.siteId, device_group)
         response = requests.post(url, headers=self.headers(), json=m)
 
         # If error, renew api key and try again
@@ -67,16 +83,30 @@
 
 if __name__ == '__main__':
 
+    log.basicConfig(
+            format='%(asctime)s %(levelname)-8s %(message)s',
+            level=log.DEBUG,
+            datefmt='%Y-%m-%d %H:%M:%S',
+            stream=sys.stdout)
+    log.debug("Testing roc...")
+
     # use valid keycloak user/password
-    roc = Roc("user", "password")
+    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)
 
     cameras = "menlo-4g-cameras"
+    mbr = roc.get_mbr(cameras)
+    print("uplink mbr:{}, downlink mbr: {}".format(mbr["uplink"], mbr["downlink"]))
 
+    roc.set_mbr(cameras, 7000000)
     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"]))