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 tunnel 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_sdnplatform_tunnel_manager |
| 30 | |
| 31 | def show_tunnel_data(request): |
| 32 | |
| 33 | # Query JSON from API and load into dictionary |
| 34 | tunnels = json.loads(do_sdnplatform_tunnel_manager(request,'all').content) |
| 35 | |
| 36 | # Dictionaries |
| 37 | sorteddict = [] |
| 38 | aliasDict = switchalias.aliasDict(request) |
| 39 | unsortdict = [] |
| 40 | statedict = {0: 'FORWARDING', 1: 'DOWN', 2: 'FORWARD', 3: 'BLOCK', 4: 'MASK'} |
| 41 | |
| 42 | # Step through master 'tunnels' list, extract entry for each dictionary. |
| 43 | for index_tunnels,value1_tunnels in tunnels.iteritems(): |
| 44 | tempdict = {} |
| 45 | temptunneldict = {} |
| 46 | |
| 47 | # get needed entries in 'links' |
| 48 | tempdict['dpid'] = value1_tunnels.get('hexDpid','') |
| 49 | tempdict['tunnelEnabled'] = value1_tunnels.get('tunnelEnabled','') |
| 50 | tempdict['tunnelIPAddr'] = value1_tunnels.get('tunnelIPAddr','') |
| 51 | tempdict['tunnelActive'] = value1_tunnels.get('tunnelActive','') |
| 52 | tempdict['tunnelState'] = value1_tunnels.get('tunnelState','') |
| 53 | tempdict['tunnelIf'] = value1_tunnels.get('tunnelEndPointIntfName','') |
| 54 | tempdict['tunnelCapable'] = value1_tunnels.get('tunnelCapable','') |
| 55 | # append to final sorted output. |
| 56 | if value1_tunnels.get('tunnelCapable',''): |
| 57 | unsortdict.append(tempdict) |
| 58 | |
| 59 | sorteddict = sorted(unsortdict, key=lambda elem: "%s %s" % (elem['dpid'], elem['tunnelIPAddr'])) |
| 60 | |
| 61 | result = '' |
| 62 | # Print table output |
| 63 | result += '<table id="showtunneloutput" >' |
| 64 | result += '<tbody>' |
| 65 | result += '<tr><td>ID</td><td>Switch</td><td>Tunnel Source IP</td><td>Tunnel Interface</td><td>Tunnel Enabled</td><td>Tunnel State</td></tr>' |
| 66 | |
| 67 | for index_output,value_output in enumerate(sorteddict): |
| 68 | result += '<tr>' |
| 69 | result += ' <td>' + str(index_output + 1) + '</td>' |
| 70 | result += ' <td>' |
| 71 | result += aliasDict.get(value_output.get('dpid', 'UNKNOWN'), value_output.get('dpid', 'UNKNOWN')) |
| 72 | result += '</td>' |
| 73 | result += ' <td>' + str(value_output.get('tunnelIPAddr','')) + '</td>' |
| 74 | result += ' <td>' + value_output.get('tunnelIf','UNKNOWN') + '</td>' |
| 75 | result += ' <td>' + str(value_output.get('tunnelActive','')) + '</td>' |
| 76 | result += ' <td>' + value_output.get('tunnelState','') + '</td>' |
| 77 | result += '</tr>' |
| 78 | result += '</tbody>' |
| 79 | result += '</table>' |
| 80 | return result |
| 81 | |
| 82 | |