99
1010class CommandProcessor :
1111 """
12- Allow setup scaled configurations with referenced objects.
13- Contain reference object cache.
14- When objects are referenced they are regenerating missing keys/oids from object cache
12+ Process SAI commands with object reference substitution.
13+
14+ This class enables setup of scaled configurations by maintaining a registry
15+ of created objects and substituting object references (variables starting with '$')
16+ with actual OIDs or keys during command processing.
17+
18+ Attributes:
19+ objects_registry: Dictionary mapping object names to their metadata
20+ sai: Reference to parent Sai instance
1521 """
1622
1723 class SubstitutionError (RuntimeError ):
18- ...
24+ """Raised when object reference substitution fails."""
25+ pass
1926
2027 def __init__ (self , sai : 'Sai' ):
28+ """
29+ Initialize CommandProcessor.
30+
31+ Args:
32+ sai: Parent Sai instance for API calls
33+ """
2134 self .objects_registry = {}
2235 self .sai = sai
2336
2437 def _substitute_from_object_registry (self , obj , * args , ** kwargs ):
38+ """
39+ Substitute a reference variable with its actual value from registry.
40+
41+ Args:
42+ obj: Object reference (string starting with '$') or regular value
43+ *args: Optional default value as positional argument
44+ **kwargs: Optional default value as 'default' keyword argument
45+
46+ Returns:
47+ Registry entry for the referenced object
48+
49+ Raises:
50+ SubstitutionError: If object is not found in registry and no default provided
51+ """
2552 if isinstance (obj , str ) and obj .startswith ('$' ):
2653 store_name = obj [1 :]
2754 try :
@@ -35,6 +62,19 @@ def _substitute_from_object_registry(self, obj, *args, **kwargs):
3562 raise self .SubstitutionError
3663
3764 def substitute_command_from_object_registry (self , command ):
65+ """
66+ Substitute all object references in a command with actual values.
67+
68+ Processes command dictionary and replaces:
69+ - '$VARIABLE' references in keys with actual OIDs/keys
70+ - '$VARIABLE' references in attributes with actual values
71+
72+ Args:
73+ command: Dictionary containing command with possible references
74+
75+ Returns:
76+ New command dictionary with all references substituted
77+ """
3878 substituted_command = {}
3979 store_name = command .get ("name" )
4080
@@ -86,7 +126,9 @@ def substitute_command_from_object_registry(self, command):
86126 return substituted_command
87127
88128 def process_command (self , command ):
89- '''
129+ """
130+ Process a single SAI command (create, set, get, or remove).
131+
90132 Command examples:
91133 {
92134 "name": "vip1"
@@ -105,7 +147,16 @@ def process_command(self, command):
105147 "type" : "SAI_OBJECT_TYPE_DASH_ACL_GROUP",
106148 "attributes" : [ "SAI_DASH_ACL_GROUP_ATTR_IP_ADDR_FAMILY", "SAI_IP_ADDR_FAMILY_IPV4" ]
107149 },
108- '''
150+
151+ Args:
152+ command: Dictionary with 'name', 'op', 'type', and optional 'key'/'attributes'
153+
154+ Returns:
155+ Created OID for 'create', attribute values for 'get', or operation status
156+
157+ Raises:
158+ AssertionError: If command format is invalid or operation fails
159+ """
109160 store_name = command .get ("name" )
110161 assert store_name , f"Invalid command { command } . Entry name is undefined"
111162
0 commit comments