Spring CLI changes to work with onos-next - partial
diff --git a/cli/fmtcnv.py b/cli/fmtcnv.py
index 485dbf4..aca97ca 100644
--- a/cli/fmtcnv.py
+++ b/cli/fmtcnv.py
@@ -32,6 +32,7 @@
 import command
 import json
 
+onos = 1
 
 # Timezone constants
 class Utc(tzinfo):
@@ -67,10 +68,18 @@
     #result = command.sdnsh.store.rest_simple_request(query_url)
     entries = result
     entries = json.loads(result)
+    switchIdField = 'dpid'
+    if onos==2:
+        entries = entries['devices']
+        switchIdField = 'id'
     #eprint entries
     for switch in entries:
         #print switch
-        switches_dpid_name[switch.get("dpid")] = switch.get("stringAttributes").get('name')
+        if onos == 1:
+            switches_dpid_name[switch.get(switchIdField)] = switch.get("stringAttributes").get('name')
+        elif onos == 2:
+            switches_dpid_name[switch.get(switchIdField)] = switch.get(switchIdField)
+             
         #print switch.get("dpid")
         #print switch.get("stringAttributes").get('name')
     return switches_dpid_name
diff --git a/cli/midw.py b/cli/midw.py
index 0f973bc..7fb1397 100755
--- a/cli/midw.py
+++ b/cli/midw.py
@@ -264,7 +264,7 @@
         return
 
     for port in ports:
-        key_string = port['switch'] + "." + "%d" % port['portNumber']
+        key_string = port['switch'] + "." + "%s" % port['portNumber']
         switch_port_to_name_dict[key_string] = port['portName']
     fmtcnv.update_alias_dict("portNames", switch_port_to_name_dict)
 
diff --git a/cli/rest_to_model.py b/cli/rest_to_model.py
index ab7a2f7..e069ee3 100755
--- a/cli/rest_to_model.py
+++ b/cli/rest_to_model.py
@@ -185,27 +185,38 @@
         address_space_match = data.get('address-space')
         address_space_prefix = data.get('address-space__startswith')
 
+        if onos == 2:
+            entries = entries['hosts']
+            
         for entry in entries:
             if onos==0:
                 if len(entry['mac']) > 1:
                     raise error.CommandInternalError("host: >1 mac")
                 mac = entry['mac'][0]
                 lastseen = entry['lastSeen']
-            else:
+            elif onos==1:
                 mac = entry['mac']
                 lastseen = 0
+                hostid = '%s' % (mac)
+            elif onos==2:
+                mac = entry['mac']
+                hostid = entry['id']
+                lastseen = 0
 
+            ipFieldName = 'ipv4'
+            if onos == 2:
+                ipFieldName = 'ipAddresses'
             ips = None
             if not ip_match and not ip_prefix:
-                ipv4 = entry['ipv4']
+                ipv4 = entry[ipFieldName]
             elif ip_match:
-                ipv4 = [x for x in entry['ipv4'] if x == ip_match]
+                ipv4 = [x for x in entry[ipFieldName] if x == ip_match]
             elif ip_prefix:
-                ipv4 = [x for x in entry['ipv4'] if x.startswith(ip_prefix)]
+                ipv4 = [x for x in entry[ipFieldName] if x.startswith(ip_prefix)]
 
-            if len(entry['ipv4']):
-                ips = [{'ip-address' : entry['ipv4'], 'last-seen' : lastseen}]
-                    	    #for x in entry['ipv4'] ]
+            if len(entry[ipFieldName]):
+                ips = [{'ip-address' : x, 'last-seen' : lastseen}
+                    	    for x in entry[ipFieldName] ]
             aps = None
             switch = []
             port = []
@@ -215,12 +226,36 @@
                 attachPoint = 'attachmentPoint'
                 attachDpid = 'switchDPID'
                 attachPort = 'port'
-            else:
+            elif onos == 1:
                 attachPoint = 'attachmentPoints'
                 attachDpid = 'dpid'
                 attachPort = 'portNumber'
+            elif onos == 2:
+                attachPoint = 'location'
+                attachDpid = 'elementId'
+                attachPort = 'port'
 
-            if len(entry[attachPoint]):
+            if onos == 2:
+                aps = [{'switch' : entry[attachPoint][attachDpid], 
+                        'ingress-port' : entry[attachPoint][attachPort] }]
+                if not dpid_match and not dpid_prefix:
+                    switch = [entry[attachPoint][attachDpid]]
+                elif dpid_match:
+                    if entry[attachPoint][attachDpid] == dpid_match:
+                        switch = [entry[attachPoint][attachDpid]]
+                elif dpid_prefix:
+                    if entry[attachPoint][attachDpid].startswith(dpid_prefix):
+                        switch = [entry[attachPoint][attachDpid]]
+
+                if not port_match and not port_prefix:
+                    port = [entry[attachPoint][attachPort]]
+                elif port_match:
+                    if entry[attachPoint][attachPort] == port_match:
+                        port = [entry[attachPoint][attachPort]]
+                elif port_prefix:
+                    if entry[attachPoint][attachPort].startswith(port_prefix):
+                        port = [entry[attachPoint][attachPort]]
+            elif len(entry[attachPoint]):
                 aps = [{'switch' : x[attachDpid], 'ingress-port' : x[attachPort] }
                        for x in entry[attachPoint]]
 
@@ -250,9 +285,8 @@
                 if address_space_prefix and not address_space.startswith(address_space_prefix):
                     continue
 
-            if onos == 1:
-                id = '%s' % (mac)
-                result.append({'id'                : id,
+            if (onos == 1) or (onos == 2):
+                result.append({'id'                : hostid,
                            'mac'               : mac,
                            'ips'               : ips,
                            'ipv4'              : ipv4,
@@ -539,8 +573,13 @@
 
         # this synthetic obj_type's name is 'switches' in an attempt
         # to disabigutate it from 'class Switch'
+        if onos == 2:
+            entries = entries["devices"]
         for entry in entries:
-            dpid = entry['dpid']
+            if onos == 2:
+                dpid = entry['id']
+            else:
+                dpid = entry['dpid']
 
             if switch_match and switch_match != dpid:
                 continue
@@ -548,11 +587,23 @@
                 continue
 
             for p in entry['ports']:
-                portNumber = p['portNumber']
                 if onos == 0:
+                    portNumber = p['portNumber']
                     name = p['name']
-                else:
+                    state = p['state']
+                elif onos == 1:
+                    portNumber = p['portNumber']
                     name = p['stringAttributes']['name']
+                    state = p['state']
+                elif onos == 2:
+                    portNumber = p['port']
+                    if portNumber == 'local':
+                        portNumber = 0
+                    name = p['annotations']['portName']
+                    if p['isEnabled'] == 'True':
+                        state = 'up'
+                    else:
+                        state = 'down'
 
                 if name_match and name.lower() != name_match:
                     continue
@@ -565,7 +616,7 @@
                                     'switch'             : dpid,
                                     'portName'           : p['name'],
                                     'config'             : p['config'],
-                                    'state'              : p['state'],
+                                    'state'              : state,
                                     'advertisedFeatures' : p['advertisedFeatures'],
                                     'currentFeatures'    : p['currentFeatures'],
                                     'hardwareAddress'    : p['hardwareAddress'],
@@ -577,7 +628,7 @@
                                     'switch'             : dpid,
                                     'portName'           : name,
                                     'config'             : 0,
-                                    'state'              : p['state'],
+                                    'state'              : state,
                                     'advertisedFeatures' : 0,
                                     'currentFeatures'    : 0,
                                     'hardwareAddress'    : 0,
diff --git a/sdncon/rest/views.py b/sdncon/rest/views.py
index 94efc75..c2e0635 100755
--- a/sdncon/rest/views.py
+++ b/sdncon/rest/views.py
@@ -52,7 +52,7 @@
 controller_rest_ip = "127.0.0.1"
 controller_rest_port = "8080"
 
-onos = 1
+onos = 2
 
 
 def controller_url(*elements):
@@ -736,6 +736,23 @@
         url = controller_url("onos", "topology", "switches")
     else:
         url = controller_url("onos", "v1", "devices")
+        try:
+            response_text = urllib2.urlopen(url).read()
+            switch_entries = json.loads(response_text)
+            for entry in switch_entries.get('devices'):
+                url2 = controller_url("onos", "v1", "devices", entry['id'], "ports")
+                response_text2 = urllib2.urlopen(url2).read()
+                port_entries = json.loads(response_text2)
+                entry["ports"] = port_entries["ports"]
+            combined_response_text = json.dumps(switch_entries)
+            return HttpResponse(combined_response_text, JSON_CONTENT_TYPE)
+        except urllib2.HTTPError, e:
+            response_text = e.read()
+            response = simplejson.loads(response_text)
+            response['error_type'] = "SDNPlatformError"
+            return HttpResponse(content=simplejson.dumps(response), 
+                                status=e.code, 
+                                content_type=JSON_CONTENT_TYPE)
     if request.META['QUERY_STRING']:
         url += '?' + request.META['QUERY_STRING']
     return get_sdnplatform_response(url)