Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/videoipath_automation_tool/apps/inventory/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from videoipath_automation_tool.apps.inventory.app.get_device import InventoryGetDeviceMixin
from videoipath_automation_tool.apps.inventory.inventory_api import InventoryAPI
from videoipath_automation_tool.apps.inventory.model.drivers import CustomSettings, CustomSettingsType, DriverLiteral
from videoipath_automation_tool.apps.inventory.model.global_snmp_config import SnmpConfiguration
from videoipath_automation_tool.apps.inventory.model.inventory_device import InventoryDevice
from videoipath_automation_tool.apps.inventory.model.inventory_device_configuration_compare import (
InventoryDeviceComparison,
)
from videoipath_automation_tool.apps.inventory.model.inventory_discovered_device import DiscoveredInventoryDevice
from videoipath_automation_tool.connector.models.response_rpc import ResponseRPC
from videoipath_automation_tool.connector.vip_connector import VideoIPathConnector
from videoipath_automation_tool.validators.device_id import validate_device_id

Expand Down Expand Up @@ -349,6 +351,83 @@ def parse_configuration(config: dict) -> InventoryDevice:
"""
return InventoryDevice.parse_configuration(config)

# --- Global SNMP Configuration Helpers ---
def get_global_snmp_config_id_by_label(self, label: str) -> Optional[str | List[str]]:
"""Method to get the global SNMP configuration id by label.
Note: If multiple SNMP configurations with the same label exist, a list of ids is returned.

Args:
label (str): Label of the SNMP configuration

Returns:
Optional[str | List[str]]: SNMP configuration id, None if not found, List of ids if multiple configurations with the same label exist
"""
return self._inventory_api.get_global_snmp_config_id_by_label(label=label)

def get_global_snmp_config_label_by_id(self, snmp_config_id: str) -> Optional[str]:
"""Method to get the global SNMP configuration label by id.

Args:
snmp_config_id (str): SNMP configuration id

Returns:
Optional[str]: SNMP configuration label, None if not found
"""
return self._inventory_api.get_global_snmp_config_label_by_id(snmp_config_id=snmp_config_id)

def get_all_global_snmp_config_ids(self) -> dict[str, str]:
"""Method to list all global SNMP configuration ids with their labels.

Returns:
dict: {snmp_config_id: snmp_config_label}
"""
return self._inventory_api.get_all_global_snmp_config_ids()

# --- Global SNMP Configuration CRUD Methods ---
def get_global_snmp_config(self, snmp_config_id: str) -> SnmpConfiguration:
"""Method to get a global SNMP configuration by id from VideoIPath-Inventory

Args:
snmp_config_id (str): SNMP configuration id

Returns:
GlobalSnmpConfig: Global SNMP configuration object
"""
return self._inventory_api.get_global_snmp_config(snmp_config_id=snmp_config_id)

def add_global_snmp_config(self, snmp_config: SnmpConfiguration) -> SnmpConfiguration:
"""Method to add a new global SNMP configuration

Args:
snmp_config (SnmpConfiguration): SNMP configuration object to add

Returns:
SnmpConfiguration: Added SNMP configuration object
"""
return self._inventory_api.add_global_snmp_config(snmp_config=snmp_config)

def update_global_snmp_config(self, snmp_config: SnmpConfiguration) -> SnmpConfiguration:
"""Method to update a global SNMP configuration

Args:
snmp_config (SnmpConfiguration): SNMP configuration object to update

Returns:
SnmpConfiguration: Updated SNMP configuration object
"""
return self._inventory_api.update_global_snmp_config(snmp_config=snmp_config)

def remove_global_snmp_config(self, snmp_config_id: str) -> ResponseRPC:
"""Method to remove a global SNMP configuration by id from VideoIPath-Inventory

Args:
snmp_config_id (str): SNMP configuration id

Returns:
ResponseRPC: Response object
"""
return self._inventory_api.remove_global_snmp_config(snmp_config_id=snmp_config_id)

# --- Deprecated Methods ---
@deprecated(
"This method is deprecated and will be removed in future versions.",
Expand Down
165 changes: 165 additions & 0 deletions src/videoipath_automation_tool/apps/inventory/inventory_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
)
from videoipath_automation_tool.apps.inventory.model.device_status import DeviceStatus
from videoipath_automation_tool.apps.inventory.model.drivers import CustomSettingsType, DriverLiteral
from videoipath_automation_tool.apps.inventory.model.global_snmp_config import SnmpConfiguration
from videoipath_automation_tool.apps.inventory.model.global_snmp_request_rpc import SnmpRequestRpc
from videoipath_automation_tool.apps.inventory.model.inventory_device import InventoryDevice
from videoipath_automation_tool.apps.inventory.model.inventory_discovered_device import DiscoveredInventoryDevice
from videoipath_automation_tool.apps.inventory.model.inventory_request_rpc import InventoryRequestRpc
Expand Down Expand Up @@ -664,6 +666,169 @@ def get_discovered_device(self, discovered_device_id: str) -> DiscoveredInventor
response.data["status"]["devman"]["discoveredDevices"]["_items"][0]
)

# --- Global SNMP Configuration Helpers ---
def get_global_snmp_config_id_by_label(self, label: str) -> Optional[str | List[str]]:
"""Method to get the global SNMP configuration id by label.
Note: If multiple SNMP configurations with the same label exist, a list of ids is returned.

Args:
label (str): Label of the SNMP configuration

Returns:
Optional[str | List[str]]: SNMP configuration id, None if not found, List of ids if multiple configurations with the same label exist
"""
if not label:
raise ValueError("Label must not be empty.")

escaped_label = urllib.parse.quote(label, safe="")
url = f"/rest/v2/data/config/system/snmp/*/* where descriptor.label='{escaped_label}' /*"
response = self.vip_connector.rest.get(url)
if response.data and response.data["config"]["system"]["snmp"]["session"]:
matches = response.data["config"]["system"]["snmp"]["session"]
if len(matches) == 1:
return list(matches.keys())[0]
elif len(matches) > 1:
self._logger.warning(
f"Multiple SNMP configurations found with label '{label}''. Returning all matching ids."
)
return list(matches.keys())
return None

def get_global_snmp_config_label_by_id(self, snmp_config_id: str) -> Optional[str]:
"""Method to get the global SNMP configuration label by id.

Args:
snmp_config_id (str): SNMP configuration id

Returns:
Optional[str]: SNMP configuration label, None if not found
"""
if not snmp_config_id:
raise ValueError("SNMP configuration id must not be empty.")

url = f"/rest/v2/data/config/system/snmp/session/{snmp_config_id}/descriptor/label"
response = self.vip_connector.rest.get(url, node_check=False)
if response.data and response.data["config"]["system"]["snmp"]["session"]:
if snmp_config_id in response.data["config"]["system"]["snmp"]["session"]:
return response.data["config"]["system"]["snmp"]["session"][snmp_config_id]["descriptor"]["label"]
return None

def get_all_global_snmp_config_ids(self) -> dict[str, str]:
"""Method to list all global SNMP configuration ids with their labels.

Returns:
dict: {snmp_config_id: snmp_config_label}
"""
url = "/rest/v2/data/config/system/snmp/*/*/descriptor/label"
response = self.vip_connector.rest.get(url)
if not response.data:
raise ValueError("Response data is empty.")

snmp_configs = response.data["config"]["system"]["snmp"]["session"]
return {
snmp_config_id: snmp_config["descriptor"]["label"] for snmp_config_id, snmp_config in snmp_configs.items()
}

# --- Global SNMP Configuration CRUD Methods ---
def get_global_snmp_config(self, snmp_config_id: str) -> SnmpConfiguration:
"""Method to get a global SNMP configuration by id from VideoIPath-Inventory

Args:
snmp_config_id (str): SNMP configuration id

Returns:
GlobalSnmpConfig: Global SNMP configuration object
"""
if not snmp_config_id:
raise ValueError("SNMP configuration id must not be empty.")

url = f"/rest/v2/data/config/system/snmp/session/{snmp_config_id}/**"
response = self.vip_connector.rest.get(url)
if not response.data:
raise ValueError("Response data is empty.")

return SnmpConfiguration.parse_from_dict(response.data["config"]["system"]["snmp"]["session"])

def add_global_snmp_config(self, snmp_config: SnmpConfiguration) -> SnmpConfiguration:
"""Method to add a new global SNMP configuration

Args:
snmp_config (SnmpConfiguration): SNMP configuration object to add

Returns:
SnmpConfiguration: Added SNMP configuration object
"""
if not snmp_config.id:
raise ValueError("SNMP configuration id must be set.")

self._logger.debug(f"Adding new global SNMP configuration with id '{snmp_config.id}'.")

existing_configs_label = self.get_global_snmp_config_label_by_id(snmp_config.id)
if existing_configs_label is not None:
raise ValueError(f"SNMP configuration with id '{snmp_config.id}' already exists. Please update it instead.")

body = SnmpRequestRpc()
body.add(snmp_config)

response = self.vip_connector.rpc.post("/api/updateSnmpConfig", body=body)

if response.header.status != "OK":
raise ValueError(f"Failed to add global SNMP configuration. Error: {response}")

return self.get_global_snmp_config(snmp_config_id=snmp_config.id)

def update_global_snmp_config(self, snmp_config: SnmpConfiguration) -> SnmpConfiguration:
"""Method to update a global SNMP configuration

Args:
snmp_config (SnmpConfiguration): SNMP configuration object to update

Returns:
SnmpConfiguration: Updated SNMP configuration object
"""
if not snmp_config.id:
raise ValueError("SNMP configuration id must be set.")

self._logger.debug(f"Updating global SNMP configuration with id '{snmp_config.id}'.")

existing_configs_label = self.get_global_snmp_config_label_by_id(snmp_config.id)
if existing_configs_label is None:
raise ValueError(f"SNMP configuration with id '{snmp_config.id}' does not exist. Please add it first.")

body = SnmpRequestRpc()
body.update(snmp_config)

response = self.vip_connector.rpc.post("/api/updateSnmpConfig", body=body)

if response.header.status != "OK":
raise ValueError(f"Failed to update global SNMP configuration. Error: {response}")

return self.get_global_snmp_config(snmp_config_id=snmp_config.id)

def remove_global_snmp_config(self, snmp_config_id: str) -> ResponseRPC:
"""Method to remove a global SNMP configuration by id from VideoIPath-Inventory

Args:
snmp_config_id (str): SNMP configuration id

Returns:
ResponseRPC: Response object
"""
if not snmp_config_id:
raise ValueError("SNMP configuration id must be set.")

self._logger.debug(f"Removing global SNMP configuration with id '{snmp_config_id}'.")

body = SnmpRequestRpc()
body.remove(snmp_config_id)

response = self.vip_connector.rpc.post("/api/updateSnmpConfig", body=body)

if response.header.status != "OK":
raise ValueError(f"Failed to remove global SNMP configuration. Error: {response}")

return response

# --- Deprecated Methods ---
@deprecated(
"The method `fetch_device_ids_list` is deprecated and will be removed in a future release. ",
Expand Down
Loading