sonic-buildimage/dockers/docker-snmp/snmp_yml_to_configdb.py
Hua Liu a9b7a1facd
Replace swsssdk with swsscommon (#11215)
#### Why I did it
Update scripts in sonic-buildimage from py-swsssdk to swsscommon


#### How I did it
Change code to use swsscommon.

#### How to verify it
Pass all E2E test case

#### Which release branch to backport (provide reason below if selected)

<!--
- Note we only backport fixes to a release branch, *not* features!
- Please also provide a reason for the backporting below.
- e.g.
- [x] 202006
-->

- [ ] 201811
- [ ] 201911
- [ ] 202006
- [ ] 202012
- [ ] 202106
- [ ] 202111
- [ ] 202205

#### Description for the changelog
Update scripts in sonic-buildimage from py-swsssdk to swsscommon

#### Link to config_db schema for YANG module changes
<!--
Provide a link to config_db schema for the table for which YANG model
is defined
Link should point to correct section on https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md
-->

#### A picture of a cute animal (not mandatory but encouraged)
2022-07-11 10:01:10 +08:00

57 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import sys
import yaml
from sonic_py_common.logger import Logger
from swsscommon.swsscommon import ConfigDBConnector
db = ConfigDBConnector()
db.connect()
SYSLOG_IDENTIFIER = 'snmp_yml_to_configdb.py'
logger = Logger(SYSLOG_IDENTIFIER)
logger.set_min_log_priority_info()
snmp_comm_config_db = db.get_table('SNMP_COMMUNITY')
snmp_config_db_communities = snmp_comm_config_db.keys()
snmp_general_config_db = db.get_table('SNMP')
snmp_general_keys = snmp_general_config_db.keys()
full_snmp_comm_list = ['snmp_rocommunity', 'snmp_rocommunities', 'snmp_rwcommunity', 'snmp_rwcommunities']
if not os.path.exists('/etc/sonic/snmp.yml'):
logger.log_info('/etc/sonic/snmp.yml does not exist')
sys.exit(1)
with open('/etc/sonic/snmp.yml', 'r') as yaml_file:
yaml_snmp_info = yaml.load(yaml_file, Loader=yaml.FullLoader)
for comm_type in full_snmp_comm_list:
if comm_type in yaml_snmp_info.keys():
if comm_type.startswith('snmp_rocommunities'):
for community in yaml_snmp_info[comm_type]:
if community not in snmp_config_db_communities:
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"})
elif comm_type.startswith('snmp_rocommunity'):
community = yaml_snmp_info['snmp_rocommunity']
if community not in snmp_config_db_communities:
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"})
elif comm_type.startswith('snmp_rwcommunities'):
for community in yaml_snmp_info[comm_type]:
if community not in snmp_config_db_communities:
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RW"})
elif comm_type.startswith('snmp_rwcommunity'):
community = yaml_snmp_info['snmp_rwcommunity']
if community not in snmp_config_db_communities:
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RW"})
if yaml_snmp_info.get('snmp_location'):
if 'LOCATION' not in snmp_general_keys:
db.set_entry('SNMP', 'LOCATION', {'Location': yaml_snmp_info['snmp_location']})
else:
logger.log_info('snmp_location does not exist in snmp.yml file')
sys.exit(1)