srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # |
| 4 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 5 | # |
| 6 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 7 | # "License"); you may not use this file except in compliance with the |
| 8 | # License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.eclipse.org/legal/epl-v10.html |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. See the License for the specific language governing |
| 16 | # permissions and limitations under the License. |
| 17 | # |
| 18 | # Python script for querying REST API and displaying connected switches in |
| 19 | # a table |
| 20 | # |
| 21 | |
| 22 | #Importing modules |
| 23 | import re |
| 24 | import sys |
| 25 | import time |
| 26 | import json |
| 27 | import urllib2 |
| 28 | from sdncon.rest.views import do_switches, do_model_list, do_instance |
| 29 | |
| 30 | def show_switch_data(request): |
| 31 | |
| 32 | switches = json.loads(do_switches(request).content) |
| 33 | switchaliases = json.loads(do_instance(request, 'switch-alias').content) |
| 34 | switchconfig = json.loads(do_instance(request, "switch-config").content) |
| 35 | |
| 36 | # Dictionaries |
| 37 | sorteddict = [] |
| 38 | |
| 39 | # Step through master 'switches' list, extract entry for each dictionary. |
| 40 | for index_switches,value1_switches in enumerate(switches): |
| 41 | tempdict = {} |
| 42 | tempaliasdict = {} |
| 43 | tempconfigdict = {} |
| 44 | |
| 45 | # get needed entries in 'switches' |
| 46 | tempdict['dpid'] = value1_switches.get('dpid','') |
| 47 | tempdict['inetAddress'] = value1_switches.get('inetAddress','') |
| 48 | tempdict['connectedSince'] = value1_switches.get('connectedSince','') |
| 49 | |
| 50 | # get related entries in other JSON queries |
| 51 | for index_switchaliases,value1_switchaliases in enumerate(switchaliases): |
| 52 | if value1_switchaliases['switch'] == value1_switches['dpid']: |
| 53 | tempaliasdict['alias'] = value1_switchaliases.get('id','') |
| 54 | tempdict['alias'] = tempaliasdict.get('alias','') |
| 55 | |
| 56 | for index_switchconfig,value1_switchconfig in enumerate(switchconfig): |
| 57 | if value1_switchconfig['dpid'] == value1_switches['dpid']: |
| 58 | tempconfigdict['core-switch'] = value1_switchconfig.get('core-switch','') |
| 59 | tempconfigdict['tunnel-termination'] = value1_switchconfig.get('tunnel-termination','') |
| 60 | tempdict['core-switch'] = tempconfigdict.get('core-switch','') |
| 61 | tempdict['tunnel-termination'] = tempconfigdict.get('tunnel-termination','') |
| 62 | |
| 63 | # append to final sorted output. |
| 64 | sorteddict.append(tempdict) |
| 65 | sorteddict.reverse() |
| 66 | |
| 67 | result = '' |
| 68 | # Print table output |
| 69 | result += '<table id="showswitchoutput">' |
| 70 | result += '<tbody>' |
| 71 | result += '<tr><td>ID</td><td>Alias</td><td>Switch DPID</td><td>IP Address</td><td>Connected Since</td></tr>' |
| 72 | for index_output,value_output in enumerate(sorteddict): |
| 73 | formatIPresult = re.search('/(.*):',value_output['inetAddress']) |
| 74 | result += '<tr>' |
| 75 | result += ' <td>' + str(index_output + 1) + '</td>' |
| 76 | result += ' <td>' + value_output.get('alias','') + '</td>' |
| 77 | result += ' <td>' + value_output.get('dpid','') + '</td>' |
| 78 | result += ' <td>' + formatIPresult.group(1) + '</td>' |
| 79 | if value_output['connectedSince'] != '': |
| 80 | result += ' <td>' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime(value_output['connectedSince'] / float(1000))) + '</td>' |
| 81 | else: |
| 82 | result += ' <td>Disconnected</td>' |
| 83 | result += '</tr>' |
| 84 | result += '</tbody>' |
| 85 | result += '</table>' |
| 86 | |
| 87 | return result |
| 88 | |