|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +portconfig is the utility to show and change ECN configuration |
| 4 | +
|
| 5 | +usage: portconfig [-h] [-v] [-s] [-p PROFILE] [-gmin GREEN_MIN] |
| 6 | + [-gmax GREEN_MAX] [-ymin YELLOW_MIN] [-ymax YELLOW_MAX] |
| 7 | + [-rmin RED_MIN] [-rmax RED_MAX] [-vv] |
| 8 | +
|
| 9 | +optional arguments: |
| 10 | + -h --help show this help message and exit |
| 11 | + -v --version show program's version number and exit |
| 12 | + -vv --verbose verbose output |
| 13 | + -p' --port port name |
| 14 | + -s --speed port speed in Mbits |
| 15 | +""" |
| 16 | +from __future__ import print_function |
| 17 | + |
| 18 | +import os |
| 19 | +import sys |
| 20 | +import json |
| 21 | +import argparse |
| 22 | +import swsssdk |
| 23 | + |
| 24 | +PORT_TABLE_NAME = "PORT" |
| 25 | +PORT_SPEED_CONFIG_FIELD_NAME = "speed" |
| 26 | + |
| 27 | +class portconfig(object): |
| 28 | + """ |
| 29 | + Process aclstat |
| 30 | + """ |
| 31 | + def __init__(self, verbose, port): |
| 32 | + self.verbose = verbose |
| 33 | + |
| 34 | + # Set up db connections |
| 35 | + self.db = swsssdk.ConfigDBConnector() |
| 36 | + self.db.connect() |
| 37 | + # check whether table for this port exists |
| 38 | + port_tables = self.db.get_table(PORT_TABLE_NAME) |
| 39 | + if not port_tables.has_key(port): |
| 40 | + raise Exception("Invalid port specified") |
| 41 | + |
| 42 | + def list_params(self, port): |
| 43 | + # chack whether table for this port exists |
| 44 | + port_tables = self.db.get_table(PORT_TABLE_NAME) |
| 45 | + if port_tables.has_key(port): |
| 46 | + print(port_tables[port]) |
| 47 | + |
| 48 | + def set_speed(self, port, speed): |
| 49 | + if self.verbose: |
| 50 | + print("Setting speed %s on port %s" % (speed, port)) |
| 51 | + self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_SPEED_CONFIG_FIELD_NAME: speed}) |
| 52 | + |
| 53 | +def main(): |
| 54 | + parser = argparse.ArgumentParser(description='Set SONiC port parameters', |
| 55 | + version='1.0.0', |
| 56 | + formatter_class=argparse.RawTextHelpFormatter) |
| 57 | + parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None) |
| 58 | + parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False) |
| 59 | + parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None) |
| 60 | + parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False) |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + try: |
| 64 | + port = portconfig(args.verbose, args.port) |
| 65 | + if args.list: |
| 66 | + port.list_params(args.port) |
| 67 | + elif args.speed: |
| 68 | + port.set_speed(args.port, args.speed) |
| 69 | + else: |
| 70 | + parser.print_help() |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + except Exception as e: |
| 74 | + print(e.message, file=sys.stderr) |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments