Skip to content

Commit

Permalink
Add buffer_init attribute to qos_buffer module (#444)
Browse files Browse the repository at this point in the history
* Add buffer_init attribute to qos_buffer module

* Remove unnecessary remove_empties

* Add back remove_empties in proper place

* Remove accidental copy and paste
  • Loading branch information
stalabi1 authored Aug 30, 2024
1 parent cf81523 commit d8275b6
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 135 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/444-add-buffer-init-attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- sonic_qos_buffer - Add 'buffer_init' attribute (https://github.com/ansible-collection/dellemc.enterprise_sonic/pull/444).
50 changes: 0 additions & 50 deletions playbooks/common_examples/qos_buffer_init.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, **kwargs):
argument_spec = {
'config': {
'options': {
'buffer_init': {'type': 'bool'},
'buffer_pools': {
'elements': 'dict',
'options': {
Expand Down
28 changes: 17 additions & 11 deletions plugins/module_utils/network/sonic/config/qos_buffer/qos_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.facts import Facts
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import (
to_request,
edit_config
edit_config_reboot
)

from copy import deepcopy


QOS_BUFFER_PATH = '/data/openconfig-qos:qos/openconfig-qos-buffer:buffer'
PATCH = 'patch'
BUFFER_INIT_PATH = '/operations/openconfig-qos-private:qos-buffer-config'
DELETE = 'delete'
TEST_KEYS = [
{'buffer_pools': {'name': ''}},
{'buffer_profiles': {'name': ''}}
]

TEST_KEYS_formatted_diff = [
{'buffer_pools': {'name': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}},
{'buffer_profiles': {'name': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}}
]

Expand Down Expand Up @@ -95,12 +95,11 @@ def execute_module(self):
if commands and len(requests) > 0:
if not self._module.check_mode:
try:
edit_config(self._module, to_request(self._module, requests))
edit_config_reboot(self._module, to_request(self._module, requests))
except ConnectionError as exc:
self._module.fail_json(msg=str(exc), code=exc.code)
pass
result['changed'] = True
result['commands'] = commands

changed_qos_buffer_facts = self.get_qos_buffer_facts()

result['before'] = existing_qos_buffer_facts
Expand Down Expand Up @@ -132,7 +131,7 @@ def set_config(self, existing_qos_buffer_facts):
:returns: the commands necessary to migrate the current configuration
to the desired configuration
"""
want = self._module.params['config']
want = remove_empties(self._module.params['config'])
have = existing_qos_buffer_facts
resp = self.set_state(want, have)
return to_list(resp)
Expand Down Expand Up @@ -186,7 +185,6 @@ def _state_deleted(self, want, have):
of the provided objects
"""
is_delete_all = False
want = remove_empties(want)

if not want:
commands = deepcopy(have)
Expand All @@ -203,11 +201,19 @@ def _state_deleted(self, want, have):
return commands, requests

def get_modify_qos_buffer_request(self, commands):
request = None
requests = []
buffer_dict = {}
buffer_init = commands.get('buffer_init')
buffer_pools = commands.get('buffer_pools')
buffer_profiles = commands.get('buffer_profiles')

if buffer_init is not None:
if buffer_init:
payload = {'openconfig-qos-private:input': {'operation': 'INIT'}}
else:
payload = {'openconfig-qos-private:input': {'operation': 'CLEAR'}}
requests.append({'path': BUFFER_INIT_PATH, 'method': 'post', 'data': payload})

if buffer_pools:
buffer_pool_list = []
for pool in buffer_pools:
Expand Down Expand Up @@ -256,9 +262,9 @@ def get_modify_qos_buffer_request(self, commands):

if buffer_dict:
payload = {'openconfig-qos-buffer:buffer': buffer_dict}
request = {'path': QOS_BUFFER_PATH, 'method': PATCH, 'data': payload}
requests.append({'path': QOS_BUFFER_PATH, 'method': 'patch', 'data': payload})

return request
return requests

def get_delete_qos_buffer_requests(self, commands, have, is_delete_all):
requests = []
Expand Down
52 changes: 24 additions & 28 deletions plugins/module_utils/network/sonic/facts/qos_buffer/qos_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
__metaclass__ = type

from copy import deepcopy

from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import (
utils,
)
Expand All @@ -25,6 +24,7 @@
to_request,
edit_config
)
from ansible.module_utils.connection import ConnectionError


class Qos_bufferFacts(object):
Expand Down Expand Up @@ -56,47 +56,43 @@ def populate_facts(self, connection, ansible_facts, data=None):
objs = []

if not data:
cfg = self.get_config(self._module)
data = self.update_qos_buffer(cfg)
objs = self.render_config(self.generated_spec, data)
data = self.update_qos_buffer(self._module)
objs = data
facts = {}
if objs:
params = utils.validate_config(self.argument_spec, {'config': remove_empties(objs)})
facts['qos_buffer'] = params['config']
params = utils.validate_config(self.argument_spec, {'config': objs})
facts['qos_buffer'] = remove_empties(params['config'])
ansible_facts['ansible_network_resources'].update(facts)
return ansible_facts

def render_config(self, spec, conf):
"""
Render config as dictionary structure and delete keys
from spec for null values
:param spec: The facts tree, generated from the argspec
:param conf: The configuration
:rtype: dictionary
:returns: The generated config
"""
return conf

def get_config(self, module):
def get_config(self, module, path, key_name):
"""Retrieve configuration from device"""
cfg = None
get_path = '/data/openconfig-qos:qos/openconfig-qos-buffer:buffer'
request = {'path': get_path, 'method': 'get'}
request = {'path': path, 'method': 'get'}

try:
response = edit_config(module, to_request(module, request))
if 'openconfig-qos-buffer:buffer' in response[0][1]:
cfg = response[0][1].get('openconfig-qos-buffer:buffer')
if key_name in response[0][1]:
cfg = response[0][1].get(key_name)
except ConnectionError as exc:
module.fail_json(msg=str(exc), code=exc.code)

return cfg

def update_qos_buffer(self, cfg):

def update_qos_buffer(self, module):
config_dict = {}
if cfg:
buffer_pools = cfg.get('buffer-pools')

path = '/data/sonic-switch:sonic-switch/SWITCH/SWITCH_LIST=switch/buffer_mode_lossless'
sonic_cfg = self.get_config(module, path, 'sonic-switch:buffer_mode_lossless')
if sonic_cfg:
config_dict['buffer_init'] = sonic_cfg
else:
config_dict['buffer_init'] = False

path = '/data/openconfig-qos:qos/openconfig-qos-buffer:buffer'
oc_cfg = self.get_config(module, path, 'openconfig-qos-buffer:buffer')
if oc_cfg:
buffer_pools = oc_cfg.get('buffer-pools')
if buffer_pools:
pool_list = buffer_pools.get('buffer-pool')
if pool_list:
Expand All @@ -116,7 +112,7 @@ def update_qos_buffer(self, cfg):
if buffer_pool_list:
config_dict['buffer_pools'] = buffer_pool_list

buffer_profiles = cfg.get('buffer-profiles')
buffer_profiles = oc_cfg.get('buffer-profiles')
if buffer_profiles:
profile_list = buffer_profiles.get('buffer-profile')
if profile_list:
Expand Down
12 changes: 6 additions & 6 deletions plugins/modules/sonic_qos_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
notes:
- Tested against Enterprise SONiC Distribution by Dell Technologies.
- Supports C(check_mode).
- The SONiC buffer initialization command must be executed before attempting to apply
- the configuration commands provided in this resource module. The buffer initialization will cause
- the switch to reboot. See the "playbooks/common_examples/QoS_buffer_init.yaml" file
- in this repo for an example playbook that initializes the buffers, waits for the reboot,
- then proceeds with execution of QoS buffer configuration commands. Alternatively, make use of the
- sonic_roce resource module to enable RoCEv2 default buffer configuration.
short_description: Manage QoS buffer configuration on SONiC
description:
- This module provides configuration management of QoS buffer for devices running SONiC
Expand All @@ -52,6 +46,11 @@
- QoS buffer configuration
type: dict
suboptions:
buffer_init:
description:
- Initialize QoS buffer based on system defaults
type: bool
version_added: 3.0.0
buffer_pools:
description:
- Buffer pools configuration
Expand Down Expand Up @@ -134,6 +133,7 @@
- name: Merge QoS buffer configuration
dellemc.enterprise_sonic.sonic_qos_buffer:
config:
buffer_init: true
buffer_pools:
- name: ingress_lossless_pool
xoff: 3500000
Expand Down
7 changes: 7 additions & 0 deletions tests/regression/roles/sonic_qos_buffer/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tests:
description: Configure QoS buffer
state: merged
input:
buffer_init: true
buffer_pools:
- name: ingress_lossless_pool
xoff: 2500000
Expand Down Expand Up @@ -64,3 +65,9 @@ tests:
- name: profile1
- name: profile2
- name: profile3

- name: test_case_05
description: Deinit buffer
state: merged
input:
buffer_init: false

This file was deleted.

6 changes: 0 additions & 6 deletions tests/regression/roles/sonic_qos_buffer/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
- set_fact:
base_cfg_path: "{{ playbook_dir + '/roles/' + role_name + '/' + 'templates/' }}"

- name: Preparation tests
include_tasks: preparation_tests.yaml

- name: "Test {{ module_name }} started ..."
include_tasks: tasks_template.yaml
loop: "{{ tests }}"

- name: "Cleanup tests {{ module_name }} started"
include_tasks: cleanup_tests.yaml

This file was deleted.

Loading

0 comments on commit d8275b6

Please sign in to comment.