Debian Codegraph @

examples/vendor/nokia/sros/config_mode.py

raw
1 import logging
2 import sys
3 import threading
4 import time
5 from ncclient import manager
6
7 # Device connection details
8 DEVICE_HOST = 'localhost'
9 DEVICE_PORT = 830
10 DEVICE_USER = 'admin'
11 DEVICE_PASS = 'admin'
12
13 # Configuration payload template for SNMP community string
14 EDIT_CONFIG_PAYLOAD = """
15 <config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
16 <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
17 <system>
18 <security>
19 <snmp>
20 <community>
21 <community-string>TestCommunity-{}</community-string>
22 <access-permissions>r</access-permissions>
23 <version>v2c</version>
24 </community>
25 </snmp>
26 </security>
27 </system>
28 </configure>
29 </config>
30 """
31
32
33 def create_ncclient_session(host, port, username, password, config_mode=None):
34 """
35 Creates and returns an ncclient manager session for connecting to the Nokia SR OS device.
36
37 :param host: Hostname or IP of the Nokia device
38 :param port: NETCONF port
39 :param username: Username for device authentication
40 :param password: Password for device authentication
41 :param config_mode: 'private' to use private candidate mode, None for default mode
42 :return: NETCONF manager session
43 """
44 device_params = {'name': 'sros'}
45 if config_mode:
46 device_params['config_mode'] = config_mode
47
48 return manager.connect(
49 host=host,
50 port=port,
51 username=username,
52 password=password,
53 hostkey_verify=False,
54 device_params=device_params
55 )
56
57
58 def configure_and_commit(session_id, config_mode=None):
59 """
60 Creates a NETCONF session, edits the configuration, and commits the changes.
61
62 :param session_id: Unique session identifier for logging
63 :param config_mode: 'private' for private candidate mode, None for default mode
64 """
65 try:
66 with create_ncclient_session(DEVICE_HOST, DEVICE_PORT, DEVICE_USER, DEVICE_PASS, config_mode=config_mode) as nc_session:
67 logging.info(f"Session {session_id} started with config_mode: {config_mode or 'default'}")
68
69 # Edit the device configuration using the payload
70 edit_payload = EDIT_CONFIG_PAYLOAD.format(session_id)
71 nc_session.edit_config(target='candidate', config=edit_payload)
72 logging.info(f"Session {session_id} successfully edited the configuration.")
73
74 # Commit the changes to the device
75 nc_session.commit(comment=f"Session {session_id} commit in {config_mode or 'default'} mode")
76 logging.info(f"Session {session_id} committed successfully in {config_mode or 'default'} mode.")
77
78 except Exception as e:
79 logging.error(f"Session {session_id} encountered an error: {e}")
80
81
82 def run_concurrent_sessions(session_ids, config_mode=None):
83 """
84 Runs multiple NETCONF sessions concurrently.
85
86 :param session_ids: List of session IDs to identify different threads
87 :param config_mode: 'private' to use private candidate mode, None for default mode
88 """
89 threads = []
90 for session_id in session_ids:
91 thread = threading.Thread(target=configure_and_commit, args=(session_id, config_mode))
92 threads.append(thread)
93 thread.start()
94
95 # Ensure all threads finish execution
96 for thread in threads:
97 thread.join()
98
99
100 def main():
101 LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
102 logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT)
103
104 logging.info("Starting test with concurrent sessions in default mode (expecting conflicts)...")
105
106 # Run test with concurrent sessions in default mode
107 run_concurrent_sessions([1, 2, 3, 4, 5])
108
109 logging.info("Now testing with concurrent sessions in private mode (no conflicts expected)...")
110
111 # Run test with concurrent sessions in private candidate mode
112 run_concurrent_sessions([6, 7, 8, 9], config_mode='private')
113
114 logging.info("Test completed.")
115
116
117 if __name__ == '__main__':
118 main()