Skip to content

Commit be70eda

Browse files
Refactor apply_rec method by extracting command processors. (#275)
Signed-off-by: Andriy Kokhan <[email protected]>
1 parent be3427e commit be70eda

File tree

2 files changed

+178
-144
lines changed

2 files changed

+178
-144
lines changed
27.5 KB
Binary file not shown.

common/sai.py

Lines changed: 178 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,175 @@ def create_rec_alias(self, obj_type, attrs, key=None):
630630
def remove_rec_alias(self, obj_key):
631631
self.remove_alias(self.get_alias_by_key(obj_key))
632632

633+
def _process_create_command(self, rec):
634+
"""Process single object creation command ('c')."""
635+
attrs = []
636+
if len(rec) > 2:
637+
for attr in rec[2:]:
638+
attrs += attr.split('=')
639+
640+
# Update OIDs in the attributes
641+
for idx in range(1, len(attrs), 2):
642+
if "oid:" in attrs[idx]:
643+
attrs[idx] = self.rec2vid[attrs[idx]]
644+
645+
obj_key = self.__update_key(rec[0], rec[1])
646+
status, key = self.create(obj_key, attrs, False)
647+
if status != "SAI_STATUS_SUCCESS":
648+
return status
649+
650+
if "{" not in key:
651+
key_list = rec[1].split(":", 1)
652+
self.rec2vid[key_list[1]] = key
653+
654+
obj_type = obj_key.split(":")[0]
655+
self.create_rec_alias(obj_type, attrs, key)
656+
return status
657+
658+
def _process_bulk_create_command(self, record):
659+
"""Process bulk object creation command ('C')."""
660+
# record = [["action", "sai-object-type"], ["key", "attr1", "attr2"], ..., [key-n", "attr1", "attr2"]]
661+
bulk_keys = []
662+
bulk_attrs = []
663+
for idx, entry in enumerate(record[1:]):
664+
# New bulk entry
665+
attrs = []
666+
for attr in entry[1:]:
667+
attrs += attr.split('=')
668+
669+
# Update OIDs in the attributes
670+
for i in range(1, len(attrs), 2):
671+
if "oid:" in attrs[i] and attrs[i] != "oid:0x0":
672+
attrs[i] = self.rec2vid[attrs[i]]
673+
674+
# Convert into "sai-object-type:key"
675+
key = record[0][1] + ":" + record[idx + 1][0]
676+
# Update OIDs in the key
677+
key = self.__update_key(record[0][0], key)
678+
# Convert into ["sai-object-type", "key"]
679+
if ":" in key:
680+
key = key.split(":", 1)[1]
681+
682+
if type(key) is dict or key.startswith("{"):
683+
key = json.loads(key)
684+
bulk_keys.append(key)
685+
bulk_attrs.append(attrs)
686+
687+
if type(key) is dict or key.startswith("{"):
688+
self.bulk_create(record[0][1], bulk_keys, bulk_attrs)
689+
else:
690+
_, new_keys, _ = self.bulk_create(record[0][1], None, bulk_attrs, len(record)-1)
691+
for idx in range(0, len(new_keys)):
692+
if "oid:" in new_keys[idx]:
693+
self.rec2vid[record[idx + 1][0]] = new_keys[idx]
694+
695+
for idx in range(len(bulk_keys)):
696+
self.create_rec_alias(record[0][1], bulk_attrs[idx], bulk_keys[idx])
697+
698+
def _process_set_command(self, rec):
699+
"""Process single attribute set command ('s')."""
700+
data = rec[2].split('=')
701+
if "oid:" in data[1]:
702+
data[1] = self.rec2vid[data[1]]
703+
704+
self.set(self.__update_key(rec[0], rec[1]), data)
705+
706+
def _process_bulk_set_command(self, record):
707+
"""Process bulk attribute set command ('S')."""
708+
# record = [["action", "sai-object-type"], ["key", "attr"], ..., [key-n", "attr"]]
709+
bulk_keys = []
710+
bulk_attrs = []
711+
for idx, entry in enumerate(record[1:]):
712+
attr = entry[1].split('=')
713+
if "oid:" in attr[1] and attr[1] != "oid:0x0":
714+
attr[1] = self.rec2vid[attr[1]]
715+
716+
# Convert into "sai-object-type:key"
717+
key = record[0][1] + ":" + record[idx + 1][0]
718+
# Update OIDs in the key
719+
key = self.__update_key(record[0][0], key)
720+
# Convert into ["sai-object-type", "key"]
721+
if ":" in key:
722+
key = key.split(":", 1)[1]
723+
724+
if key.startswith("{"):
725+
key = json.loads(key)
726+
bulk_keys.append(key)
727+
bulk_attrs.append(attr)
728+
729+
self.bulk_set(record[0][1], bulk_keys, bulk_attrs)
730+
731+
def _process_remove_command(self, rec):
732+
"""Process single object removal command ('r')."""
733+
obj_key = self.__update_key(rec[0], rec[1])
734+
self.remove(obj_key)
735+
self.remove_rec_alias(obj_key)
736+
737+
def _process_bulk_remove_command(self, record):
738+
"""Process bulk object removal command ('R')."""
739+
# record = [["action", "sai-object-type"], ["key"], ..., [key-n"]]
740+
bulk_keys = []
741+
for idx, entry in enumerate(record[1:]):
742+
# Convert into "sai-object-type:key"
743+
key = record[0][1] + ":" + record[idx + 1][0]
744+
# Update OIDs in the key
745+
key = self.__update_key(record[0][0], key)
746+
# Convert into ["sai-object-type", "key"]
747+
if ":" in key:
748+
key = key.split(":", 1)[1]
749+
750+
if key.startswith("{"):
751+
key = json.loads(key)
752+
bulk_keys.append(key)
753+
754+
self.bulk_remove(record[0][1], bulk_keys)
755+
for key in bulk_keys:
756+
self.remove_rec_alias(key)
757+
758+
def _process_get_command(self, rec, oids):
759+
"""Process single attribute get command ('g')."""
760+
attrs = []
761+
if len(rec) > 2:
762+
for attr in rec[2:]:
763+
attrs += attr.split('=')
764+
765+
status, data = self.get(self.__update_key(rec[0], rec[1]), attrs, False)
766+
if status == "SAI_STATUS_SUCCESS":
767+
jdata = data.to_json()
768+
for idx in range(1, len(jdata), 2):
769+
if ":oid:" in jdata[idx]:
770+
oids += data.oids(idx)
771+
elif "oid:" in jdata[idx]:
772+
oids.append(data.oid(idx))
773+
return status
774+
775+
def _process_get_response_command(self, rec, oids):
776+
"""Process expected get response command ('G')."""
777+
attrs = []
778+
for attr in rec[2:]:
779+
attrs += attr.split('=')
780+
781+
G_oids = []
782+
for idx in range(1, len(attrs), 2):
783+
G_output = attrs[idx]
784+
785+
if ":oid:" in G_output:
786+
start_idx = G_output.find(":") + 1
787+
G_oids += G_output[start_idx:].split(",")
788+
elif "oid:" in G_output:
789+
G_oids.append(G_output)
790+
791+
assert len(oids) == len(G_oids), f"Expected data {oids}. Actual data {G_oids}"
792+
793+
for idx, oid in enumerate(G_oids):
794+
self.rec2vid[oid] = oids[idx]
795+
oids.clear()
796+
797+
def _process_expected_failure_command(self, rec, status):
798+
"""Process expected failure command ('E')."""
799+
assert status in [rec[1], "SAI_STATUS_SUCCESS"], \
800+
f"Expected fail reason is {rec[1]}. Actual fail reason is {status}"
801+
633802
def apply_rec(self, fname):
634803
# Since it's expected that sairedis.rec file contains a full configuration,
635804
# before we start, we must flush both RPC backend (Redis or Thrift server) and NPU state.
@@ -646,158 +815,23 @@ def apply_rec(self, fname):
646815
continue
647816

648817
if rec[0] == 'c':
649-
attrs = []
650-
if len(rec) > 2:
651-
for attr in rec[2:]:
652-
attrs += attr.split('=')
653-
654-
# Update OIDs in the attributes
655-
for idx in range(1, len(attrs), 2):
656-
if "oid:" in attrs[idx]:
657-
attrs[idx] = self.rec2vid[attrs[idx]]
658-
659-
obj_key = self.__update_key(rec[0], rec[1])
660-
status, key = self.create(obj_key, attrs, False)
661-
if status != "SAI_STATUS_SUCCESS":
662-
continue
663-
if "{" not in key:
664-
key_list = rec[1].split(":", 1)
665-
self.rec2vid[key_list[1]] = key
666-
667-
obj_type = obj_key.split(":")[0]
668-
self.create_rec_alias(obj_type, attrs, key)
669-
818+
status = self._process_create_command(rec)
670819
elif rec[0] == 'C':
671-
# record = [["action", "sai-object-type"], ["key", "attr1", "attr2"], ..., [key-n", "attr1", "attr2"]]
672-
bulk_keys = []
673-
bulk_attrs = []
674-
for idx, entry in enumerate(record[1:]):
675-
# New bulk entry
676-
attrs = []
677-
for attr in entry[1:]:
678-
attrs += attr.split('=')
679-
680-
# Update OIDs in the attributes
681-
for i in range(1, len(attrs), 2):
682-
if "oid:" in attrs[i] and attrs[i] != "oid:0x0":
683-
attrs[i] = self.rec2vid[attrs[i]]
684-
685-
# Convert into "sai-object-type:key"
686-
key = record[0][1] + ":" + record[idx + 1][0]
687-
# Update OIDs in the key
688-
key = self.__update_key(rec[0], key)
689-
# Convert into ["sai-object-type", "key"]
690-
if ":" in key:
691-
key = key.split(":", 1)[1]
692-
693-
if type(key) is dict or key.startswith("{"):
694-
key = json.loads(key)
695-
bulk_keys.append(key)
696-
bulk_attrs.append(attrs)
697-
698-
if type(key) is dict or key.startswith("{"):
699-
self.bulk_create(record[0][1], bulk_keys, bulk_attrs)
700-
else:
701-
_, new_keys, _ = self.bulk_create(record[0][1], None, bulk_attrs, len(record)-1)
702-
for idx in range(0, len(new_keys)):
703-
if "oid:" in new_keys[idx]:
704-
self.rec2vid[record[idx + 1][0]] = new_keys[idx]
705-
for idx in range(len(bulk_keys)):
706-
self.create_rec_alias(record[0][1], bulk_attrs[idx], bulk_keys[idx])
707-
820+
self._process_bulk_create_command(record)
708821
elif rec[0] == 's':
709-
data = rec[2].split('=')
710-
if "oid:" in data[1]:
711-
data[1] = self.rec2vid[data[1]]
712-
713-
self.set(self.__update_key(rec[0], rec[1]), data)
714-
822+
self._process_set_command(rec)
715823
elif rec[0] == 'S':
716-
# record = [["action", "sai-object-type"], ["key", "attr"], ..., [key-n", "attr"]]
717-
bulk_keys = []
718-
bulk_attrs = []
719-
for idx, entry in enumerate(record[1:]):
720-
attr = entry[1].split('=')
721-
if "oid:" in attr[1] and attr[1] != "oid:0x0":
722-
attr[1] = self.rec2vid[attr[1]]
723-
724-
# Convert into "sai-object-type:key"
725-
key = record[0][1] + ":" + record[idx + 1][0]
726-
# Update OIDs in the key
727-
key = self.__update_key(rec[0], key)
728-
# Convert into ["sai-object-type", "key"]
729-
if ":" in key:
730-
key = key.split(":", 1)[1]
731-
732-
if key.startswith("{"):
733-
key = json.loads(key)
734-
bulk_keys.append(key)
735-
bulk_attrs.append(attr)
736-
737-
self.bulk_set(record[0][1], bulk_keys, bulk_attrs)
738-
824+
self._process_bulk_set_command(record)
739825
elif rec[0] == 'r':
740-
obj_key = self.__update_key(rec[0], rec[1])
741-
self.remove(obj_key)
742-
self.remove_rec_alias(obj_key)
743-
826+
self._process_remove_command(rec)
744827
elif rec[0] == 'R':
745-
# record = [["action", "sai-object-type"], ["key"], ..., [key-n"]]
746-
bulk_keys = []
747-
for idx, entry in enumerate(record[1:]):
748-
# Convert into "sai-object-type:key"
749-
key = record[0][1] + ":" + record[idx + 1][0]
750-
# Update OIDs in the key
751-
key = self.__update_key(rec[0], key)
752-
# Convert into ["sai-object-type", "key"]
753-
if ":" in key:
754-
key = key.split(":", 1)[1]
755-
756-
if key.startswith("{"):
757-
key = json.loads(key)
758-
bulk_keys.append(key)
759-
760-
self.bulk_remove(record[0][1], bulk_keys)
761-
for key in bulk_keys:
762-
self.remove_rec_alias(key)
763-
828+
self._process_bulk_remove_command(record)
764829
elif rec[0] == 'g':
765-
attrs = []
766-
if len(rec) > 2:
767-
for attr in rec[2:]:
768-
attrs += attr.split('=')
769-
770-
status, data = self.get(self.__update_key(rec[0], rec[1]), attrs, False)
771-
if status == "SAI_STATUS_SUCCESS":
772-
jdata = data.to_json()
773-
for idx in range(1, len(jdata), 2):
774-
if ":oid:" in jdata[idx]:
775-
oids += data.oids(idx)
776-
elif "oid:" in jdata[idx]:
777-
oids.append(data.oid(idx))
830+
status = self._process_get_command(rec, oids)
778831
elif rec[0] == 'G':
779-
attrs = []
780-
for attr in rec[2:]:
781-
attrs += attr.split('=')
782-
783-
G_oids = []
784-
785-
for idx in range(1, len(attrs), 2):
786-
G_output = attrs[idx]
787-
788-
if ":oid:" in G_output:
789-
start_idx = G_output.find(":") + 1
790-
G_oids += G_output[start_idx:].split(",")
791-
elif "oid:" in G_output:
792-
G_oids.append(G_output)
793-
assert len(oids) == len(G_oids), f"Expected data {oids}. Actual data {G_oids}"
794-
795-
for idx, oid in enumerate(G_oids):
796-
self.rec2vid[oid] = oids[idx]
797-
oids = []
832+
self._process_get_response_command(rec, oids)
798833
elif rec[0] == 'E':
799-
# It's expected that the previous command has failed
800-
assert status in [rec[1], "SAI_STATUS_SUCCESS"], f"Expected fail reason is {rec[1]}. Actual fail reason is {status}"
834+
self._process_expected_failure_command(rec, status)
801835
else:
802836
print("Ignored line {}: {}".format(cnt, rec))
803837

0 commit comments

Comments
 (0)