Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_copp.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for sonic_copp
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_copp
17 version_added: "2.1.0"
18 notes:
19 - Tested against Enterprise SONiC Distribution by Dell Technologies.
20 - Supports C(check_mode).
21 short_description: Manage CoPP configuration on SONiC
22 description:
23 - This module provides configuration management of CoPP for devices running SONiC
24 author: "S. Talabi (@stalabi1)"
25 options:
26 config:
27 description:
28 - Specifies CoPP configurations
29 type: dict
30 suboptions:
31 copp_groups:
32 description:
33 - List of CoPP entries that comprise a CoPP group
34 type: list
35 elements: dict
36 suboptions:
37 copp_name:
38 description:
39 - Name of CoPP classifier
40 type: str
41 required: true
42 trap_priority:
43 description:
44 - CoPP trap priority
45 type: int
46 trap_action:
47 description:
48 - CoPP trap action
49 type: str
50 choices: ['copy', 'copy_cancel', 'deny', 'drop', 'forward', 'log', 'transit', 'trap']
51 queue:
52 description:
53 - CoPP queue ID
54 type: int
55 cir:
56 description:
57 - Committed information rate in bps or pps (packets per second)
58 type: str
59 cbs:
60 description:
61 - Committed bucket size in packets or bytes
62 type: str
63 copp_traps:
64 description:
65 - List of CoPP entries that comprise a CoPP trap
66 type: list
67 elements: dict
68 version_added: 3.1.0
69 suboptions:
70 name:
71 description:
72 - Name of CoPP trap
73 type: str
74 required: true
75 trap_protocol_ids:
76 description:
77 - Comma separated string of trap protocol IDs
78 type: str
79 trap_group:
80 description:
81 - Name of CoPP group
82 type: str
83 state:
84 description:
85 - The state of the configuration after module completion
86 type: str
87 choices: ['merged', 'deleted', 'replaced', 'overridden']
88 default: merged
89 """
90
91 EXAMPLES = """
92 # Using "merged" state
93 #
94 # Before state:
95 # -------------
96 #
97 # sonic# show copp actions
98 # (No "copp actions" configuration present)
99 # sonic# show copp classifiers
100 # (No "copp classifiers" configuration present)
101
102 - name: Merge CoPP configuration
103 dellemc.enterprise_sonic.sonic_copp:
104 config:
105 copp_groups:
106 - copp_name: 'copp-1'
107 trap_priority: 1
108 trap_action: 'drop'
109 queue: 1
110 cir: '45'
111 cbs: '45'
112 - copp_name: 'copp-2'
113 trap_priority: 2
114 trap_action: 'forward'
115 queue: 2
116 cir: '90'
117 cbs: '90'
118 copp_traps:
119 - name: 'trap-1'
120 trap_protocol_ids: 'id1,id2,id3'
121 trap_group: 'copp-1'
122 state: merged
123
124 # After state:
125 # ------------
126 #
127 # sonic# show copp actions
128 # CoPP action group copp-1
129 # trap-action drop
130 # trap-priority 1
131 # trap-queue 1
132 # police cir 45 cbs 45
133 # CoPP action group copp-2
134 # trap-action forward
135 # trap-priority 2
136 # trap-queue 2
137 # police cir 90 cbs 90
138 # sonic# show copp classifiers
139 # Class-map trap-1 match-type copp
140 # protocol id1
141 # protocol id2
142 # protocol id3
143
144
145 # Using "replaced" state
146 #
147 # Before state:
148 # -------------
149 #
150 # sonic# show copp actions
151 # CoPP action group copp-1
152 # trap-action drop
153 # trap-priority 1
154 # trap-queue 1
155 # police cir 45 cbs 45
156 # CoPP action group copp-2
157 # trap-action forward
158 # trap-priority 2
159 # trap-queue 2
160 # police cir 55 cbs 55
161 # sonic# show copp classifiers
162 # Class-map trap-1 match-type copp
163 # protocol id1
164 # protocol id2
165 # protocol id3
166
167 - name: Replace CoPP configuration
168 dellemc.enterprise_sonic.sonic_copp:
169 config:
170 copp_groups:
171 - copp_name: 'copp-1'
172 trap_priority: 2
173 trap_action: 'forward'
174 queue: 2
175 - copp_name: 'copp-3'
176 trap_priority: 3
177 trap_action: 'drop'
178 queue: 3
179 cir: '1000'
180 cbs: '1000'
181 copp_traps:
182 - name: 'trap-1'
183 trap_protocol_ids: 'id1'
184 trap_group: 'copp-2'
185 state: replaced
186
187 # After state:
188 # ------------
189 #
190 # sonic# show copp actions
191 # CoPP action group copp-1
192 # trap-action forward
193 # trap-priority 2
194 # trap-queue 2
195 # CoPP action group copp-2
196 # trap-action forward
197 # trap-priority 2
198 # trap-queue 2
199 # police cir 55 cbs 55
200 # CoPP action group copp-3
201 # trap-action drop
202 # trap-priority 3
203 # trap-queue 3
204 # police cir 1000 cbs 1000
205 # sonic# show copp classifiers
206 # Class-map trap-1 match-type copp
207 # protocol id1
208
209
210 # Using "overridden" state
211 #
212 # Before state:
213 # -------------
214 #
215 # sonic# show copp actions
216 # CoPP action group copp-1
217 # trap-action forward
218 # trap-priority 2
219 # trap-queue 2
220 # CoPP action group copp-3
221 # trap-action drop
222 # trap-priority 3
223 # trap-queue 3
224 # police cir 1000 cbs 1000
225 # Class-map trap-1 match-type copp
226 # protocol id1
227
228 - name: Override CoPP configuration
229 dellemc.enterprise_sonic.sonic_copp:
230 config:
231 copp_groups:
232 - copp_name: 'copp-4'
233 trap_priority: 4
234 trap_action: 'forward'
235 queue: 4
236 cir: 200
237 cbs: 200
238 state: overridden
239
240 # After state:
241 # ------------
242 #
243 # sonic# show copp actions
244 # CoPP action group copp-4
245 # trap-action forward
246 # trap-priority 4
247 # trap-queue 4
248 # police cir 200 cbs 200
249
250
251 # Using "deleted" state
252 #
253 # Before state:
254 # -------------
255 #
256 # sonic# show copp actions
257 # CoPP action group copp-1
258 # trap-action drop
259 # trap-priority 1
260 # trap-queue 1
261 # police cir 45 cbs 45
262 # CoPP action group copp-2
263 # trap-action forward
264 # trap-priority 2
265 # trap-queue 2
266 # police cir 90 cbs 90
267 # Class-map trap-1 match-type copp
268
269 - name: Delete CoPP configuration
270 dellemc.enterprise_sonic.sonic_copp:
271 config:
272 copp_groups:
273 - copp_name: 'copp-1'
274 cir: '45'
275 cbs: '45'
276 - copp_name: 'copp-2'
277 copp_traps:
278 - name: 'trap-1'
279 state: deleted
280
281 # After state:
282 # ------------
283 #
284 # sonic# show copp actions
285 # CoPP action group copp-1
286 # trap-action drop
287 # trap-priority 1
288 # trap-queue 1
289 """
290
291 RETURN = """
292 before:
293 description: The configuration prior to the module invocation.
294 returned: always
295 type: dict
296 after:
297 description: The configuration resulting from module invocation.
298 returned: when changed
299 type: dict
300 after_generated:
301 description: The configuration that would be generated by non-check mode module invocation.
302 returned: when C(check_mode)
303 type: dict
304 commands:
305 description: The set of commands pushed to the remote device.
306 returned: always
307 type: list
308 sample: ['command 1', 'command 2', 'command 3']
309 """
310
311
312 from ansible.module_utils.basic import AnsibleModule
313 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.copp.copp import CoppArgs
314 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.copp.copp import Copp
315
316
317 def main():
318 """
319 Main entry point for module execution
320
321 :returns: the result form module invocation
322 """
323 module = AnsibleModule(argument_spec=CoppArgs.argument_spec,
324 supports_check_mode=True)
325
326 result = Copp(module).execute_module()
327 module.exit_json(**result)
328
329
330 if __name__ == '__main__':
331 main()