Skip to content

Commit

Permalink
Update endpoints_sdwan.yaml and sdwan.json File Format Changes (#63)
Browse files Browse the repository at this point in the history
* endpoint_sdwan and sdwan.json file format changes

* added update_rank.yaml condition

* removed unused variable

* installed pre-commit hook and fixed linting

* resolved duplicates issue
  • Loading branch information
mekarajesh authored Oct 29, 2024
1 parent 8657a05 commit 6553201
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
78 changes: 61 additions & 17 deletions nac_collector/cisco_client_sdwan.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,31 +372,75 @@ def get_feature_profiles(self, endpoint, endpoint_dict):
for item in data_loop:
profile_endpoint = endpoint["endpoint"] + "/" + str(item["profileId"])
response = self.get_request(self.base_url + profile_endpoint)

main_entry = {
"data": response.json(),
"endpoint": self.base_url + profile_endpoint,
}
l1_children = []
for k, v in response.json().items():
if k == "associatedProfileParcels":
for parcel in v:
parcel_id = parcel["parcelId"]
parcel_type = parcel["parcelType"]
new_endpoint = (
profile_endpoint + "/" + parcel_type + "/" + parcel_id
)
new_endpoint = profile_endpoint + "/" + parcel_type
response = self.get_request(self.base_url + new_endpoint)
self.log_response(new_endpoint, response)
data = response.json()

# endpoint_dict[endpoint["name"]]["items"].append(data)
endpoint_dict[endpoint["name"]].append(
{
"data": data,
"endpoint": new_endpoint
+ "/"
+ self.get_id_value(data),
}
)
for l1_item in response.json()["data"]:
self.log_response(new_endpoint, response)
response = self.get_request(
self.base_url + new_endpoint + "/" + l1_item["parcelId"]
)
data = response.json()
if not self.id_exists(l1_children, data["parcelId"]):
l1_children.append(
{
"data": data,
"endpoint": new_endpoint
+ "/"
+ self.get_id_value(data),
}
)
endpoint_dict[endpoint["name"]].append(
main_entry
if not l1_children
else {**main_entry, "children": l1_children}
)

for profile_parcel in endpoint_dict.get(endpoint.get("name"), []):
for associatedProfileParcel in profile_parcel.get("data", {}).get(
"associatedProfileParcels", []
):
subparcels = associatedProfileParcel.get("subparcels", [])
if isinstance(subparcels, list) and subparcels:
for subparcel in subparcels:
l2_parcel_type = subparcel.get("parcelType", "")[
len(associatedProfileParcel.get("parcelType", "")) :
].lstrip("/")
l2_new_endpoint = f"{self.base_url}{endpoint.get('endpoint', '')}/{profile_parcel.get('data', {}).get('profileId', '')}/{associatedProfileParcel.get('parcelType', '')}/{associatedProfileParcel.get('parcelId', '')}/{l2_parcel_type}"
l2_response = self.get_request(l2_new_endpoint)
self.log_response(l2_new_endpoint, l2_response)
for subparcel_item in l2_response.json().get("data", []):
subparcel_endpoint = f"{l2_new_endpoint}/{subparcel_item.get('parcelId', '')}"
subparcel_data = self.get_request(
subparcel_endpoint
).json()
for profile_parcel_item in profile_parcel.get(
"children", []
):
if profile_parcel_item.get("data", {}).get(
"parcelType"
) == associatedProfileParcel.get("parcelType"):
profile_parcel_item.setdefault(
"children", []
).append(
{
"data": subparcel_data,
"endpoint": subparcel_endpoint,
}
)
return endpoint_dict

def id_exists(self, l1_children, data_id):
return any(child["data"]["parcelId"] == data_id for child in l1_children)

@staticmethod
def get_id_value(i):
"""
Expand Down
35 changes: 34 additions & 1 deletion nac_collector/github_repo_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ def parent_children(self, endpoints_list):
"endpoint": base_endpoint,
"children": [child_entry],
}
if "%s" in endpoint:
for parent_map_key in parent_map:
children = parent_map[parent_map_key]["children"]
to_add = []
for l1_children in children:
if "%s" in l1_children["endpoint"]:
base_endpoint, child_path = l1_children["endpoint"].split(
"%s", 1
)
child_entry = {"name": name, "endpoint": child_path}
# Collect all child entries to be added
for child in children:
if base_endpoint.rstrip("/") == child[
"endpoint"
].rstrip("/"):
value_exists = any(
child_entry["endpoint"] in d.values()
for d in child.get("children", "")
)
if not value_exists:
to_add.append((child, child_entry))
# Add all collected child entries
for parent, child_entry in to_add:
if "children" in parent:
parent["children"].append(child_entry)
else:
parent["children"] = [child_entry]

# Now go through endpoints to fill out parent details
for endpoint_data in endpoints_list:
Expand All @@ -194,14 +221,20 @@ def parent_children(self, endpoints_list):
parent_map[endpoint]["name"] = name
else:
# This endpoint is not a parent; no children reference it
if "%v" not in endpoint:
if "%v" not in endpoint and "%s" not in endpoint:
# Standalone endpoint, add directly to modified_endpoints
modified_endpoints.append(endpoint_data)

# Add all valid parent-child structures to the modified_endpoints list
for _, parent_data in parent_map.items():
# Add to modified list only if it has children
if parent_data["children"]:
# Remove the entry of child object with %s
parent_data["children"] = [
child
for child in parent_data["children"]
if "%s" not in child.get("endpoint", "")
]
modified_endpoints.append(parent_data)

# Return the modified endpoints list
Expand Down

0 comments on commit 6553201

Please sign in to comment.