Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
dont add suffix in transformation results
Browse files Browse the repository at this point in the history
  • Loading branch information
sbasan committed Jul 3, 2024
1 parent a9ba557 commit 3f5cbf1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
75 changes: 47 additions & 28 deletions catalystwan/models/configuration/config_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,44 +306,63 @@ class ConfigTransformResult(BaseModel):
validation_alias="unsupportedConversionItems",
)

def add_suffix_to_names(self) -> None:
suffix = f"_{str(self.uuid)[:5]}"
parcel_name_lookup: Dict[str, List[AnyPolicyObjectParcel]] = {}
for config_group in self.ux2_config.config_groups:
config_group.header.origname = config_group.config_group.name
config_group.config_group.name += suffix
for topology_group in self.ux2_config.topology_groups:
topology_group.header.origname = topology_group.topology_group.name
topology_group.topology_group.name += suffix
for feature_profile in self.ux2_config.feature_profiles:
feature_profile.header.origname = feature_profile.feature_profile.name
feature_profile.feature_profile.name += suffix
@property
def suffix(self):
return f"_{str(self.uuid)[:5]}"

def create_policy_object_parcel_name_lookup(self) -> Dict[str, List[AnyPolicyObjectParcel]]:
lookup: Dict[str, List[AnyPolicyObjectParcel]] = {}
# parcel rename only for policy groups-of-interest which share global profile
for profile_parcel in self.ux2_config.profile_parcels:
profile_parcel.header.origname = profile_parcel.parcel.parcel_name
if profile_parcel.header.type in [t._get_parcel_type() for t in list_types(AnyPolicyObjectParcel)]:
# build lookup by parcel name to find duplicates
parcel = cast(AnyPolicyObjectParcel, profile_parcel.parcel)
name = profile_parcel.header.origname
if not parcel_name_lookup.get(name):
parcel_name_lookup[name] = [parcel]
if not lookup.get(name):
lookup[name] = [parcel]
else:
parcel_name_lookup[name].append(parcel)
lookup[name].append(parcel)
return lookup

def add_suffix_to_names(self) -> None:
for config_group in self.ux2_config.config_groups:
config_group.header.origname = config_group.config_group.name
config_group.config_group.name += self.suffix
for topology_group in self.ux2_config.topology_groups:
topology_group.header.origname = topology_group.topology_group.name
topology_group.topology_group.name += self.suffix
for feature_profile in self.ux2_config.feature_profiles:
feature_profile.header.origname = feature_profile.feature_profile.name
feature_profile.feature_profile.name += self.suffix

for name, parcels in parcel_name_lookup.items():
# policy object parcel names are restricted to 32 characters and needs to be unique
def resolve_conflicts_on_policy_object_parcel_names(self, use_suffix: bool = False) -> None:
# policy object parcel names are restricted to 32 characters and needs to be unique
# TODO: cleanup, also this works only for suffix len = 6 (eg. "_1afc9")
assert len(self.suffix) == 6
for name, parcels in self.create_policy_object_parcel_name_lookup().items():
maxlen = 32
for i, parcel in enumerate(parcels):
if i > 0:
# replace last 3-digit of suffix (can handle 4096 duplicates)
suffix_num = int(suffix[1:], 16) + i
parcel.parcel_name = name[:-3] + hex(suffix_num)[-3:]
continue
# add suffix
if len(name) >= (maxlen - 6):
name = name[maxlen - 7 :]
name = name + suffix
parcel.parcel_name = name
if use_suffix:
# dedicated conflict resolving when suffix is used (we increment suffix digit)
for i, parcel in enumerate(parcels):
if i > 0:
# replace last 3-digit of suffix (can handle 4096 duplicates)
suffix_num = int(self.suffix[1:], 16) + i
parcel.parcel_name = name[:-3] + hex(suffix_num)[-3:]
continue
# add suffix
if len(name) >= (maxlen - 6):
name = name[maxlen - 7 :]
name = name + self.suffix
parcel.parcel_name = name
else:
for i, parcel in enumerate(parcels):
if i > 0:
suffix_str = f"_{hex(i)[-1]}"
if len(name) > (maxlen - 2):
parcel.parcel_name = name[:-2] + suffix_str
else:
parcel.parcel_name = name + suffix_str

def add_failed_conversion_parcel(
self,
Expand Down
2 changes: 1 addition & 1 deletion catalystwan/utils/config_migration/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def run(self):
f.write(ux1.model_dump_json(exclude_none=True, by_alias=True, indent=4, warnings=False))

# transform to ux2 and dump to json file
_transform_result = transform(UX1Config.model_validate_json(open(self.ux1_dump).read()), self.progress)
_transform_result = transform(UX1Config.model_validate_json(open(self.ux1_dump).read()), True)
with open(self.ux2_dump, "w") as f:
f.write(_transform_result.model_dump_json(exclude_none=True, by_alias=True, indent=4, warnings=False))

Expand Down
3 changes: 2 additions & 1 deletion catalystwan/workflows/config_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def get_version_info(session: ManagerSession) -> VersionInfo:
return VersionInfo(platform=session._platform_version, sdk=PACKAGE_VERSION)


def transform(ux1: UX1Config, add_suffix: bool = True) -> ConfigTransformResult:
def transform(ux1: UX1Config, add_suffix: bool = False) -> ConfigTransformResult:
transform_result = ConfigTransformResult()
ux2 = UX2Config(version=ux1.version)
subtemplates_mapping = defaultdict(set)
Expand Down Expand Up @@ -500,6 +500,7 @@ def transform(ux1: UX1Config, add_suffix: bool = True) -> ConfigTransformResult:
transform_result.ux2_config = ux2
if add_suffix:
transform_result.add_suffix_to_names()
transform_result.resolve_conflicts_on_policy_object_parcel_names(add_suffix)
transform_result.ux2_config = UX2Config.model_validate(transform_result.ux2_config)
return transform_result

Expand Down

0 comments on commit 3f5cbf1

Please sign in to comment.