Commit 9034dcd3 authored by Waleed Akbar's avatar Waleed Akbar
Browse files

feat: Minor improvements and added TODOs

- Add OperationFailedException for error handling
- Improve logging in PluggablesService
- Clean up imports in config_translator and testmessages
parent fd8fe619
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ from common.proto.pluggables_pb2 import (
    Pluggable, CreatePluggableRequest, ListPluggablesRequest, ListPluggablesResponse, 
    GetPluggableRequest, DeletePluggableRequest, ConfigurePluggableRequest)
from common.method_wrappers.ServiceExceptions import (
    NotFoundException, AlreadyExistsException, InvalidArgumentException)
    NotFoundException, AlreadyExistsException, InvalidArgumentException, OperationFailedException)
from common.tools.object_factory.ConfigRule import json_config_rule_set
from context.client.ContextClient import ContextClient
from device.client.DeviceClient import DeviceClient
@@ -46,7 +46,7 @@ class PluggablesServiceServicerImpl(PluggablesServiceServicer):
        """
        LOGGER.info(f"Configuring device {device_uuid}, pluggable index {pluggable_index}")
        
        # Step 1: Get the device from Context service (to extract IP address and determine template)
        # Step 1: Get the device from Context service (to extract IP address)
        try:
            device = self.context_client.GetDevice(DeviceId(device_uuid={'uuid': device_uuid}))  # type: ignore
            LOGGER.info(f"Retrieved device from Context: {device.name}")
@@ -73,7 +73,6 @@ class PluggablesServiceServicerImpl(PluggablesServiceServicer):
        elif device_address == '10.30.7.8':
            template_identifier = 'leaf'
        else:
            # Default to hub template if IP address cannot be determined
            LOGGER.warning(f"Cannot determine device type from IP address {device_address}, defaulting to hub template")
            raise InvalidArgumentException( 'Device IP address', device_address, extra_details='Unknown device IP adress')
        
@@ -153,7 +152,8 @@ class PluggablesServiceServicerImpl(PluggablesServiceServicer):
            LOGGER.info(f"Device {device_uuid} found in Context service: {device.name}")
        except grpc.RpcError as e:
            LOGGER.error(f"Device {device_uuid} not found in Context service: {e}")
            raise NotFoundException('Device', device_uuid, extra_details='Device must exist before creating pluggable')
            raise NotFoundException(
                'Device', device_uuid, extra_details='Device must exist before creating pluggable')
        
        # If initial_config is provided, push configuration to device
        if request.HasField('initial_config') and len(pluggable.config.dsc_groups) > 0:
@@ -162,9 +162,8 @@ class PluggablesServiceServicerImpl(PluggablesServiceServicer):
                self._push_config_to_device(device_uuid, pluggable_index, pluggable.config)
            except Exception as e:
                LOGGER.error(f"Failed to push initial config to device: {e}")
                raise 
                # We still create the pluggable in memory, but log the error
                # In production, you might want to raise the exception instead
                raise OperationFailedException(
                    'Push initial pluggable configuration', extra_details=str(e))
        
        self.pluggables[pluggable_key] = pluggable
        
@@ -214,7 +213,9 @@ class PluggablesServiceServicerImpl(PluggablesServiceServicer):
            LOGGER.info(f'No matching pluggable found: device={device_uuid}, index={pluggable_index}')
            raise NotFoundException('Pluggable', pluggable_key)
        
        # Remove pluggable config from device via DSCM driver
        # Remove pluggable config from device
        # TODO: Verify deletion works with actual hub and leaf devices
        # 
        try:
            pluggable = self.pluggables[pluggable_key]
            # Create empty config to trigger deletion
+2 −2
Original line number Diff line number Diff line
@@ -13,9 +13,9 @@
# limitations under the License.

import logging
from typing import Dict, Any, List, Optional
from typing import Dict, Any
from common.proto.pluggables_pb2 import PluggableConfig
from common.proto.context_pb2 import ConfigRule, ConfigActionEnum
from common.proto.context_pb2 import ConfigRule

LOGGER = logging.getLogger(__name__)

+3 −0
Original line number Diff line number Diff line
@@ -142,6 +142,9 @@ def create_get_pluggable_request(
# DeletePluggableRequest
###########################

# TODO: Both leaf and hub have a same jinja template for deleting pluggable config.
# The difference lies in the component name (channel-1 for hub, channel-1/3/5 for leaf).

def create_delete_pluggable_request(
    device_uuid: str,
    pluggable_index: int