subnet and imsi endpoints

Change-Id: Ief1d09026c99559d12eb5c905e484ed9eb5f5e7e
diff --git a/README.md b/README.md
index e8095d4..3ebfa16 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
 # Network Diagnostics Application for Aether Edge
 
-## TBD
+python3 networkdiag.py --user=xxx --password=yyy
+
+curl http://localhost:3333/subnet
+curl http://localhost:3333/imsi
diff --git a/networkdiag.py b/networkdiag.py
index 7529eae..cb8bedb 100644
--- a/networkdiag.py
+++ b/networkdiag.py
@@ -7,25 +7,58 @@
 from flask import Flask, request
 from flask_restful import Resource, Api
 import logging as log
+from argparse import ArgumentParser, SUPPRESS
+
+from roc import Roc
 
 app = Flask(__name__)
 api = Api(app)
 
+global roc
 
-class Greeting(Resource):
+
+class Imsi(Resource):
     def get(self):
-        return 'Hello World!'
+        return roc.get_imsi()
+
+class Subnet(Resource):
+    def get(self):
+        return roc.get_subnet()
 
 
-api.add_resource(Greeting, '/')  # Route_1
+api.add_resource(Imsi, '/imsi')
+api.add_resource(Subnet, '/subnet')
+
+
+def build_argparser():
+    parser = ArgumentParser(add_help=False)
+    args = parser.add_argument_group('Options')
+    args.add_argument('-h', '--help',
+                      action='help',
+                      default=SUPPRESS,
+                      help='Show this help message and exit.')
+    args.add_argument("--user",
+                      help="ROC username",
+                      type=str)
+    args.add_argument("--password",
+                      help="ROC password",
+                      type=str)
+    return parser
+
 
 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)
+        format='%(asctime)s %(levelname)-8s %(message)s',
+        level=log.DEBUG,
+        datefmt='%Y-%m-%d %H:%M:%S',
+        stream=sys.stdout)
 
     log.info("Starting network-diag-app...")
 
+    args = build_argparser().parse_args()
+
+    roc = Roc(args.user, args.password)
+    #roc.dump()
+    print(roc.get_imsi())
+
     app.run('0.0.0.0', '3333')
diff --git a/roc.py b/roc.py
index fd45701..9d9478f 100644
--- a/roc.py
+++ b/roc.py
@@ -98,7 +98,7 @@
         if response.status_code != 200:
             log.error("get_devicegroup() failed, status_code: {}".format(response.status_code))
             return None
-        return json.loads(response.text)
+        return json.loads(response.text)['device-group']
 
     def get_ipdomain(self):
         url = URL + "ip-domain"
@@ -106,7 +106,31 @@
         if response.status_code != 200:
             log.error("get_ipdomain() failed, status_code: {}".format(response.status_code))
             return None
-        return json.loads(response.text)
+        return json.loads(response.text)['ip-domain']
+
+    def get_imsi(self):
+        imsis = {} # imsi-id:imsi
+        devicegroups = self.get_devicegroup()
+        for devicegroup in devicegroups:
+            for imsi in devicegroup['imsis']:
+                imsis[imsi['imsi-id']] = imsi['imsi-range-from']
+        return imsis
+
+    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_devicegroup()))
+        print(pyaml.dump(self.get_ipdomain()))
+
 
 
 if __name__ == '__main__':