diff --git a/config/main.py b/config/main.py index 5f7e41a36f..2d3d65f29b 100644 --- a/config/main.py +++ b/config/main.py @@ -6536,28 +6536,74 @@ def ntp(ctx): @ntp.command('add') @click.argument('ntp_ip_address', metavar='', required=True) +@click.argument('minpoll_maxpoll', metavar='[minpoll maxpoll ]', required=False, nargs=-1, type=click.Path()) @click.pass_context -def add_ntp_server(ctx, ntp_ip_address): +def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): """ Add NTP server IP """ + DEFAULT_MINPOLL = 6 + DEFAULT_MAXPOLL = 10 + MIN_POLL = 3 + MAX_POLL = 17 + + new_minpoll = DEFAULT_MINPOLL + new_maxpoll = DEFAULT_MAXPOLL + if ADHOC_VALIDATION: - if not clicommon.is_ipaddress(ntp_ip_address): + if not clicommon.is_ipaddress(ntp_ip_address): ctx.fail('Invalid IP address') - db = ValidatedConfigDBConnector(ctx.obj['db']) - ntp_servers = db.get_table("NTP_SERVER") - if ntp_ip_address in ntp_servers: - click.echo("NTP server {} is already configured".format(ntp_ip_address)) - return - else: - try: - db.set_entry('NTP_SERVER', ntp_ip_address, {'NULL': 'NULL'}) - except ValueError as e: - ctx.fail("Invalid ConfigDB. Error: {}".format(e)) - click.echo("NTP server {} added to configuration".format(ntp_ip_address)) - try: - click.echo("Restarting ntp-config service...") - clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) - except SystemExit as e: - ctx.fail("Restart service ntp-config failed with error {}".format(e)) + + if len(minpoll_maxpoll) != 0 and len(minpoll_maxpoll) != 4: + ctx.fail('Invalid minpoll and maxpoll parameters') + + if minpoll_maxpoll[0] != 'minpoll' and minpoll_maxpoll[2] != 'maxpoll': + ctx.fail('Invalid minpoll or maxpoll parameter') + + if len(minpoll_maxpoll) != 0: + new_minpoll = int(minpoll_maxpoll[1]) + new_maxpoll = int(minpoll_maxpoll[3]) + + if new_minpoll not in range(MIN_POLL, MAX_POLL + 1) or new_maxpoll not in range(MIN_POLL, MAX_POLL + 1): + ctx.fail('minpoll and maxpoll must be in the range 3-17') + + db = ValidatedConfigDBConnector(ctx.obj['db']) + ntp_server_entry = db.get_entry('NTP_SERVER', ntp_ip_address) + + # init the curr_minpoll & curr_maxpoll with default values + curr_minpoll = DEFAULT_MINPOLL + curr_maxpoll = DEFAULT_MAXPOLL + + if ntp_server_entry: + if ntp_server_entry.get('minpoll'): + curr_minpoll = int(ntp_server_entry.get('minpoll')) + if ntp_server_entry and ntp_server_entry.get('maxpoll'): + curr_maxpoll = int(ntp_server_entry.get('maxpoll')) + + if (curr_minpoll == new_minpoll) and (curr_maxpoll == new_maxpoll): + click.echo("NTP server {} is already configured".format(ntp_ip_address)) + return + # if create or update + if new_minpoll >= new_maxpoll: + ctx.fail("Invalid minpoll:{} and maxpoll:{}".format(curr_minpoll, curr_maxpoll)) + + serverInfo = { + "minpoll": new_minpoll, + "maxpoll": new_maxpoll + } + try: + if ntp_server_entry: + db.mod_entry('NTP_SERVER', ntp_ip_address, serverInfo) + else: + db.set_entry('NTP_SERVER', ntp_ip_address, serverInfo) + + except ValueError as e: + ctx.fail("Invalid ConfigDB. Error: {}".format(e)) + click.echo("NTP server {} minpoll {} maxpoll {} added to configuration".format(ntp_ip_address, new_minpoll, new_maxpoll)) + try: + click.echo("Restarting ntp-config service...") + clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) + except SystemExit as e: + ctx.fail("Restart service ntp-config failed with error {}".format(e)) + @ntp.command('del') @click.argument('ntp_ip_address', metavar='', required=True) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 429fe39bbf..bda1aef07e 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -7458,19 +7458,24 @@ This sub-section of commands is used to add or remove the configured NTP servers **config ntp add** This command is used to add a NTP server IP address to the NTP server list. Note that more that one NTP server IP address can be added in the device. - +User may optionally configure minpoll and maxpoll for each NTP server. Minpoll and maxpoll is in the range from 3-17. When not configured, minpoll has default value "6" and maxpoll has default value "10" - Usage: ``` - config ntp add + config ntp add [minpoll] [maxpoll] ``` - Example: ``` admin@sonic:~$ sudo config ntp add 9.9.9.9 - NTP server 9.9.9.9 added to configuration + NTP server 9.9.9.9 {'minpoll': 6, 'maxpoll': 10} added to configuration Restarting ntp-config service... ``` + ``` + admin@sonic:~# config ntp add 8.8.8.8 12 16 + NTP server 8.8.8.8 {'minpoll': 12, 'maxpoll': 16} added to configuration + Restarting ntp-config service... + **config ntp delete** This command is used to delete a configured NTP server IP address. @@ -9600,11 +9605,10 @@ This command displays the running configuration of the ntp module. - Example: ``` - admin@sonic:~$ show runningconfiguration ntp NTP Servers - ------------- - 1.1.1.1 - 2.2.2.2 + ----------------------------- + 8.8.8.8 minpoll 12 maxpoll 16 + 9.9.9.9 minpoll 6 maxpoll 10 ``` **show runningconfiguration syslog** diff --git a/show/main.py b/show/main.py index 35303b6eda..2700dbeb30 100755 --- a/show/main.py +++ b/show/main.py @@ -1511,8 +1511,11 @@ def ntp(verbose): data = ntp_file.readlines() for line in data: if line.startswith("server "): - ntp_server = line.split(" ")[1] - ntp_servers.append(ntp_server) + info = line.split(" ") + ntp_server = info[1] + minpoll = info[4] + maxpoll = info[6] + ntp_servers.append(ntp_server + " minpoll " + minpoll + " maxpoll " + maxpoll) ntp_dict['NTP Servers'] = ntp_servers print(tabulate(ntp_dict, headers=list(ntp_dict.keys()), tablefmt="simple", stralign='left', missingval=""))