srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 4 | # |
| 5 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 6 | # "License"); you may not use this file except in compliance with the |
| 7 | # License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | # |
| 17 | # |
| 18 | # Build a simple JSON of the topology for use in the ForceDirected |
| 19 | # Visualization in the InfoVis Toolkit |
| 20 | # |
| 21 | |
| 22 | #Importing modules |
| 23 | import re |
| 24 | import sys |
| 25 | import time |
| 26 | import json |
| 27 | import urllib2 |
| 28 | import switchalias |
| 29 | from sdncon.rest.views import do_switches, do_model_list, do_instance, do_device, do_links |
| 30 | |
| 31 | def build_topology_data(request): |
| 32 | |
| 33 | # Query JSON from API and load into dictionary |
| 34 | switches = json.loads(do_switches(request).content) |
| 35 | devices = json.loads(do_device(request).content) |
| 36 | links = json.loads(do_links(request).content) |
| 37 | aliasDict = switchalias.aliasDict(request) |
| 38 | |
| 39 | # Dictionaries |
| 40 | parsedswitch = [] |
| 41 | parseddevices = [] |
| 42 | parsedlinks = [] |
| 43 | |
| 44 | # Step through master 'switches' list, extract entry for each dictionary. |
| 45 | for index_switches,value1_switches in enumerate(switches): |
| 46 | tempdict = {} |
| 47 | |
| 48 | # get needed entries in 'switches' |
| 49 | tempdict['dpid'] = value1_switches.get('dpid','') |
| 50 | tempdict['inetAddress'] = value1_switches.get('inetAddress','') |
| 51 | |
| 52 | # append to final sorted output. |
| 53 | parsedswitch.append(tempdict) |
| 54 | |
| 55 | # Step through master 'device' list, extract entry for each dictionary. |
| 56 | for index_devices,value1_devices in enumerate(devices): |
| 57 | tempdict = {} |
| 58 | |
| 59 | # get needed entries in 'devices' |
| 60 | for index_mac,value_mac in enumerate(value1_devices['mac']): |
| 61 | tempdict['mac'] = value_mac |
| 62 | tempdict['ipv4'] = value1_devices.get('ipv4','Unknown') |
| 63 | for index_switch2,value_switch2 in enumerate(value1_devices['attachmentPoint']): |
| 64 | switchconnlist = [] |
| 65 | switchtempdict = {} |
| 66 | if value_switch2.get('switchDPID', 'UNKNOWN') != '': |
| 67 | if value_switch2.get('port', 'UNKNOWN') != '': |
| 68 | switchtempdict['port'] = value_switch2['port'] |
| 69 | switchtempdict['DPID'] = value_switch2['switchDPID'] |
| 70 | switchconnlist.append(switchtempdict) |
| 71 | tempdict['attachments'] = switchconnlist |
| 72 | # append to final sorted output. |
| 73 | parseddevices.append(tempdict) |
| 74 | |
| 75 | |
| 76 | # Step through master 'links' list, extract entry for each dictionary. |
| 77 | for index_links,value1_links in enumerate(links): |
| 78 | tempdict = {} |
| 79 | |
| 80 | # get needed entries in 'links' |
| 81 | tempdict['src-switch'] = value1_links.get('src-switch','') |
| 82 | tempdict['src-port'] = value1_links.get('src-port','') |
| 83 | tempdict['src-port-state'] = value1_links.get('src-port-state','') |
| 84 | tempdict['dst-switch'] = value1_links.get('dst-switch','') |
| 85 | tempdict['dst-port'] = value1_links.get('dst-port','') |
| 86 | tempdict['dst-port-state'] = value1_links.get('dst-port-state','') |
| 87 | |
| 88 | # append to final sorted output. |
| 89 | parsedlinks.append(tempdict) |
| 90 | |
| 91 | #Begin puting the data in the JSON list |
| 92 | jsonoutput = [] |
| 93 | result = '' |
| 94 | |
| 95 | |
| 96 | # Print switches by themselves to handle orphaned switches. |
| 97 | for index_switch,value_switch in enumerate(parsedswitch): |
| 98 | adjacenciestmp = [] |
| 99 | datatmp = {} |
| 100 | |
| 101 | datatmp = { '$color': '#f15922', '$type': 'square', '$dim': '10'} |
| 102 | jsonoutput.append({'adjacencies': [], 'data': datatmp, 'id': value_switch['dpid'], 'name': aliasDict.get(value_switch['dpid'], value_switch['dpid'])}) |
| 103 | |
| 104 | |
| 105 | # Determine host -> Switch links |
| 106 | for index_devices,value_devices in enumerate(parseddevices): |
| 107 | adjacenciestmp = [] |
| 108 | datatmp = {} |
| 109 | |
| 110 | for index_adj,value_adj in enumerate(value_devices.get('attachments', '')): |
| 111 | adjacenciestmp.append({'nodeTo': str(value_adj['DPID']), 'nodeFrom': value_devices['mac'], 'data': { '$color': '#fdb813', '$lineWidth': '3' }}) |
| 112 | datatmp = { '$color': '#fdb813', '$type': 'circle', '$dim': '10'} |
| 113 | jsonoutput.append({'adjacencies': adjacenciestmp, 'data': datatmp, 'id': value_devices['mac'], 'name': value_devices['mac']}) |
| 114 | |
| 115 | # Determine Switch -> Switch links |
| 116 | for index_link,value_link in enumerate(parsedlinks): |
| 117 | adjacenciestmp = [] |
| 118 | datatmp = {} |
| 119 | adjacenciestmp.append({'nodeTo': str(value_link['src-switch']), 'nodeFrom': value_link['dst-switch'], 'data': { '$color': '#f15922', '$lineWidth': '3' }}) |
| 120 | datatmp = { '$color': '#f15922', '$type': 'square', '$dim': '10'} |
| 121 | jsonoutput.append({'adjacencies': adjacenciestmp, 'data': datatmp, 'id': value_link['dst-switch'], 'name': aliasDict.get(value_link['dst-switch'], value_link['dst-switch'])}) |
| 122 | |
| 123 | result += 'var json =' + json.dumps(jsonoutput, sort_keys=True, indent=4, separators=(',', ': ')) + ';' |
| 124 | return result |