forked from sonic-net/DASH
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ze Gan <[email protected]>
- Loading branch information
Showing
7 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "default_value.h" | ||
|
||
#include <map> | ||
|
||
std::vector<sai_attribute_t> merge_default_values( | ||
const std::vector<sai_attribute_t>& attrs, | ||
const std::vector<sai_attribute_t>& default_values) { | ||
|
||
std::vector<sai_attribute_t> merged_attrs; | ||
std::map<sai_attr_id_t, sai_attribute_t> attrs_map; | ||
|
||
for (const auto &attr : default_values) { | ||
attrs_map[attr.id] = attr; | ||
} | ||
|
||
for (const auto &attr : attrs) { | ||
attrs_map[attr.id] = attr; | ||
} | ||
|
||
for (const auto &pair : attrs_map) { | ||
merged_attrs.push_back(pair.second); | ||
} | ||
|
||
return merged_attrs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
extern "C" | ||
{ | ||
#include "sai.h" | ||
} | ||
|
||
|
||
std::vector<sai_attribute_t> merge_default_values( | ||
const std::vector<sai_attribute_t>& attrs, | ||
const std::vector<sai_attribute_t>& default_values); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Set default value block | ||
{ | ||
{% for sai_attr in table.sai_attributes %} | ||
|
||
/** | ||
* Set default value for SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }} | ||
*/ | ||
do { | ||
|
||
// attribute already exists on current attribute list | ||
if (std::find_if(attrs.begin(), attrs.end(), [](const sai_attribute_t &attr) { | ||
return attr.id == SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }}; | ||
}) != attrs.end()) | ||
{ | ||
break; | ||
} | ||
|
||
// conditional attributes should be populated by user | ||
|
||
{% if sai_attr.isreadonly == 'true' %} | ||
// can't be read only | ||
break; | ||
{% endif %} | ||
|
||
// can't be mandatory on create | ||
|
||
// there is no default value | ||
|
||
// is valid only | ||
{% if table.actions | length > 1 %} | ||
{% if sai_attr.param_actions | length == 0 %} | ||
// always set default attribute in this case, even if condition is not set | ||
// (see github discussion https://github.com/sonic-net/DASH/pull/547) | ||
{% else %} | ||
bool match_action = false; | ||
auto action_itr = find_if(attrs.begin(), attrs.end(), [](const sai_attribute_t &attr) { | ||
return attr.id == SAI_{{ table.name | upper }}_ATTR_ACTION; | ||
}); | ||
sai_int32_t action_value = SAI_{{ table.name | upper }}_ACTION_{{ table.actions[0].name | upper }}; | ||
if (action_itr != attrs.end()) | ||
{ | ||
action_value = action_itr->value.s32; | ||
} | ||
{% for action in sai_attr.param_actions %} | ||
match_action |= action_value == SAI_{{ table.name | upper }}_ACTION_{{ action | upper }}; | ||
{% endfor %} | ||
if (!match_action) | ||
{ | ||
break; | ||
} | ||
{% endif %} | ||
{% endif %} | ||
|
||
// set default value | ||
{% if sai_attr.default is defined %} | ||
sai_attribute_t a = {}; | ||
a.id = SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }}; | ||
{% if sai_attr.default and sai_attr.default != "empty" %} | ||
{% if sai_attr.type == "sai_ip_address_t" %} | ||
sai_deserialize_ip_address("{{ sai_attr.default }}", &a.value.ipaddr); | ||
{% elif sai_attr.type == "sai_uint32_t" %} | ||
a.value.u32 = {{ sai_attr.default }}; | ||
{% elif sai_attr.default == "vendor" %} | ||
// Hardcode vendor to zero | ||
{% else %} | ||
a.value.s32 = {{ sai_attr.default }}; | ||
{% endif %} | ||
{% endif %} | ||
DASH_LOG_NOTICE("adding SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }} with default value"); | ||
attrs.push_back(a); | ||
break; | ||
{% endif %} | ||
|
||
} while(0); | ||
|
||
{% endfor %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
// SAI_{{ table.name | upper }}_ACTION_{{ action.name | upper }} | ||
|
||
static std::vector<sai_attribute_t> set_default_{{ table.name }}_{{ action.name }}_attributes() { | ||
std::vector<sai_attribute_t> attrs; | ||
|
||
{% for sai_attr in table.sai_attributes %} | ||
{% if sai_attr.param_actions | length == 0 or action.name in sai_attr.param_actions %} | ||
{ | ||
{% include 'templates/imp/default_value_set.j2' %} | ||
} | ||
{% endif %} | ||
{% endfor %} | ||
|
||
return attrs; | ||
} | ||
|
||
static const std::vector<sai_attribute_t>& get_default_{{ table.name }}_{{ action.name }}_attributes() { | ||
static const std::vector<sai_attribute_t> attrs = set_default_{{ table.name }}_{{ action.name }}_attributes(); | ||
return attrs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
|
||
attrs = merge_default_values(attrs, get_default_{{ table.name }}_{{ action.name }}_attributes()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// set default value | ||
{% if sai_attr.default is defined %} | ||
sai_attribute_t a = {}; | ||
a.id = SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }}; | ||
{% if sai_attr.default and sai_attr.default != "empty" %} | ||
{% if sai_attr.type == "sai_ip_address_t" %} | ||
sai_deserialize_ip_address("{{ sai_attr.default }}", &a.value.ipaddr); | ||
{% elif sai_attr.type == "sai_uint32_t" %} | ||
a.value.u32 = {{ sai_attr.default }}; | ||
{% elif sai_attr.default == "vendor" %} | ||
// Hardcode vendor to zero | ||
{% else %} | ||
a.value.s32 = {{ sai_attr.default }}; | ||
{% endif %} | ||
{% endif %} | ||
DASH_LOG_NOTICE("adding SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }} with default value"); | ||
attrs.push_back(a); | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters