Skip to content

Commit

Permalink
make sure peer with mirror interfaces get unsynced if main device gets
Browse files Browse the repository at this point in the history
updated in settings repo
  • Loading branch information
indy-independence committed Dec 11, 2024
1 parent 9ab8a36 commit b0021ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
39 changes: 37 additions & 2 deletions src/cnaas_nms/db/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

from cnaas_nms.app_settings import app_settings
from cnaas_nms.db.device import Device, DeviceType
from cnaas_nms.db.device_vars import expand_interface_settings
from cnaas_nms.db.exceptions import ConfigException, RepoStructureException
from cnaas_nms.db.git_worktrees import WorktreeError, find_templates_worktree_path, refresh_existing_templates_worktrees
from cnaas_nms.db.helper import MgmtdomainNotFoundError, find_mgmtdomain_peer
from cnaas_nms.db.job import Job, JobStatus
from cnaas_nms.db.joblock import Joblock, JoblockError
from cnaas_nms.db.session import redis_session, sqla_session
Expand All @@ -26,6 +28,7 @@
get_group_settings_asdict,
get_group_templates_branch,
get_groups,
get_settings,
rebuild_settings_cache,
)
from cnaas_nms.devicehandler.sync_history import add_sync_event
Expand Down Expand Up @@ -186,6 +189,30 @@ def reset_repo(local_repo: Repo, remote_repo_path: str):
local_repo.head.reset(index=True, working_tree=True)


def get_peer_with_mirror_interfaces(session, dev: Device) -> Optional[Device]:
"""Returns peer device of management domain if it has any ifclass mirror interfaces"""
logger = get_logger()
if dev.device_type == DeviceType.ACCESS:
return None
try:
peer_device = find_mgmtdomain_peer(session, dev)
except MgmtdomainNotFoundError:
return None
except Exception:
logger.exception("Error while finding peer device for mirrored interfaces")
return None

Check warning on line 203 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L194-L203

Added lines #L194 - L203 were not covered by tests

peer_settings, _ = get_settings(peer_device.hostname, peer_device.device_type, peer_device.model)

Check warning on line 205 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L205

Added line #L205 was not covered by tests

try:
for intf in expand_interface_settings(peer_settings["interfaces"]):
if intf["ifclass"] == "mirror":
return peer_device
except KeyError:
pass
return None

Check warning on line 213 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L207-L213

Added lines #L207 - L213 were not covered by tests


def _refresh_repo_task_settings(job_id: Optional[int] = None) -> str:
logger = get_logger()
local_repo_path = app_settings.SETTINGS_LOCAL
Expand Down Expand Up @@ -233,6 +260,9 @@ def _refresh_repo_task_settings(job_id: Optional[int] = None) -> str:
if dev:
dev.synchronized = False
add_sync_event(hostname, "refresh_settings", ret, job_id)
peer_device = get_peer_with_mirror_interfaces(session, dev)
if peer_device:
add_sync_event(peer_device.hostname, "refresh_settings", ret, job_id)

Check warning on line 265 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L263-L265

Added lines #L263 - L265 were not covered by tests
else:
logger.warn("Settings updated for unknown device: {}".format(hostname))

Expand Down Expand Up @@ -395,8 +425,13 @@ def settings_syncstatus(updated_settings: set) -> Tuple[Set[DeviceType], Set[str
elif basedir.startswith("devices"):
try:
hostname = filename.split(os.path.sep)[1]
if Device.valid_hostname(hostname):
unsynced_hostnames.add(hostname)
if not Device.valid_hostname(hostname):
continue
unsynced_hostnames.add(hostname)

Check warning on line 430 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L428-L430

Added lines #L428 - L430 were not covered by tests
# determine mirror device syncstatus
mirror_device: Optional[Device] = None
if mirror_device:
unsynced_hostnames.add(mirror_device.hostname)

Check warning on line 434 in src/cnaas_nms/db/git.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/git.py#L432-L434

Added lines #L432 - L434 were not covered by tests
except Exception as e:
logger.exception("Error in settings devices directory: {}".format(str(e)))
elif basedir.startswith("groups"):
Expand Down
8 changes: 6 additions & 2 deletions src/cnaas_nms/db/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from cnaas_nms.db.mgmtdomain import Mgmtdomain


class MgmtdomainNotFoundError(Exception):
pass


def canonical_mac(mac):
"""Return a standardized format of MAC-addresses for CNaaS to
store in databases etc."""
Expand All @@ -36,12 +40,12 @@ def find_mgmtdomain_one_device(session, device0: Device) -> Mgmtdomain:
.one_or_none()
)
if not mgmtdomain:
raise Exception("No mgmtdomain found for uplink device: {}".format(device0.hostname))
raise MgmtdomainNotFoundError("No mgmtdomain found for uplink device: {}".format(device0.hostname))

Check warning on line 43 in src/cnaas_nms/db/helper.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/helper.py#L43

Added line #L43 was not covered by tests
elif device0.device_type == DeviceType.ACCESS:
if device0.management_ip:
mgmtdomain = find_mgmtdomain_by_ip(session, IPv4Address(device0.management_ip))
else:
raise Exception("No mgmtdomain found for uplink device: {}".format(device0.hostname))
raise MgmtdomainNotFoundError("No mgmtdomain found for uplink device: {}".format(device0.hostname))

Check warning on line 48 in src/cnaas_nms/db/helper.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/helper.py#L48

Added line #L48 was not covered by tests
else:
raise Exception("Unexpected uplink device type: {}".format(device0.device_type))
return mgmtdomain
Expand Down

0 comments on commit b0021ae

Please sign in to comment.