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 | # Python script to query link states from REST API |
| 19 | # |
| 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 | |
| 32 | def show_link_data(request): |
| 33 | |
| 34 | # Query JSON from API and load into dictionary |
| 35 | switches = json.loads(do_switches(request).content) |
| 36 | switchlinks = json.loads(do_links(request).content) |
| 37 | |
| 38 | # Dictionaries |
| 39 | sorteddict = [] |
| 40 | aliasDict = switchalias.aliasDict(request) |
| 41 | unsortdict = [] |
| 42 | statedict = {0: 'FORWARDING', 1: 'DOWN', 2: 'FORWARD', 3: 'BLOCK', 4: 'MASK'} |
| 43 | |
| 44 | # Step through master 'links' list, extract entry for each dictionary. |
| 45 | for index_links,value1_links in enumerate(switchlinks): |
| 46 | tempdict = {} |
| 47 | tempswitchdict = {} |
| 48 | |
| 49 | # get needed entries in 'links' |
| 50 | tempdict['src-switch'] = value1_links.get('src-switch','') |
| 51 | tempdict['src-port'] = value1_links.get('src-port','') |
| 52 | tempdict['src-port-state'] = value1_links.get('src-port-state','') |
| 53 | tempdict['dst-switch'] = value1_links.get('dst-switch','') |
| 54 | tempdict['dst-port'] = value1_links.get('dst-port','') |
| 55 | tempdict['dst-port-state'] = value1_links.get('dst-port-state','') |
| 56 | tempdict['type'] = value1_links.get('type','') |
| 57 | |
| 58 | # append to final sorted output. |
| 59 | unsortdict.append(tempdict) |
| 60 | |
| 61 | |
| 62 | sorteddict = sorted(unsortdict, key=lambda elem: "%s %02d" % (elem['src-switch'], elem['src-port'])) |
| 63 | |
| 64 | result = '' |
| 65 | # Print table output |
| 66 | result += '<table id="showlinkoutput" >' |
| 67 | result += '<tbody>' |
| 68 | result += '<tr><td>ID</td><td>Source Switch</td><td>Source Port</td><td>Source Port State</td><td>Destination Switch</td><td>Destination Port</td><td>Destination Port State</td><td>Connection Type</td></tr>' |
| 69 | for index_output,value_output in enumerate(sorteddict): |
| 70 | result += '<tr>' |
| 71 | result += ' <td>' + str(index_output + 1) + '</td>' |
| 72 | result += ' <td>' |
| 73 | result += aliasDict.get(value_output.get('src-switch', 'UNKNOWN'), value_output.get('src-switch', 'UNKNOWN')) |
| 74 | result += '</td>' |
| 75 | result += ' <td>' + str(value_output.get('src-port','')) + '</td>' |
| 76 | result += ' <td>' + statedict.get(value_output.get('src-port-state',0),'DOWN') + '</td>' |
| 77 | result += ' <td>' |
| 78 | result += aliasDict.get(value_output.get('dst-switch', 'UNKNOWN'), value_output.get('dst-switch', 'UNKNOWN')) |
| 79 | result += '</td>' |
| 80 | result += ' <td>' + str(value_output.get('dst-port','')) + '</td>' |
| 81 | result += ' <td>' + statedict.get(value_output.get('dst-port-state',0),'DOWN') + '</td>' |
| 82 | result += ' <td>' + value_output.get('type','') + '</td>' |
| 83 | result += '</tr>' |
| 84 | result += '</tbody>' |
| 85 | result += '</table>' |
| 86 | return result |
| 87 | |