Cole Walker 3123d63cfb Fix unhandled exception when querying ptp status
Attempting to query the status of a non-existant ptp instance or
clockClass could result in an unhandled key error exception on the
ptptracking daemon.

Added logic to handle this case on the server and report the error to
the client.

Test plan:
PASS: Build and deploy notificationservice-base and
notificationclient-base
PASS: Test v1 and v2 status pulls, subscribe, list and delete.

Closes-bug: 1997995

Signed-off-by: Cole Walker <cole.walker@windriver.com>
Change-Id: I3bfe111dbd79475720ea1e710be2a23f45763ae6
2022-11-25 16:25:08 -05:00

78 lines
3.1 KiB
Python

#
# Copyright (c) 2022 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from pecan import expose, abort
from webob.exc import HTTPException, HTTPServerError
from datetime import datetime
import logging
import oslo_messaging
from notificationclientsdk.common.helpers import constants
from notificationclientsdk.common.helpers import subscription_helper
from notificationclientsdk.services.ptp import PtpService
from notificationclientsdk.exception import client_exception
from notificationclientsdk.common.helpers import log_helper
from sidecar.repository.notification_control import notification_control
LOG = logging.getLogger(__name__)
log_helper.config_logger(LOG)
class ResourceAddressController(object):
def __init__(self, resource_address):
self.resource_address = resource_address
@expose('json')
def CurrentState(self):
try:
# validate resource address
_, nodename, resource, optional, self.resource_address = \
subscription_helper.parse_resource_address(
self.resource_address)
if nodename == constants.WILDCARD_CURRENT_NODE:
nodename = notification_control.get_residing_nodename()
LOG.debug('Nodename to query: %s' % nodename)
if not notification_control.in_service_nodenames(nodename):
LOG.warning("Node {} is not available".format(nodename))
raise client_exception.NodeNotAvailable(nodename)
if resource not in constants.VALID_SOURCE_URI:
LOG.warning("Resource {} is not valid".format(resource))
raise client_exception.ResourceNotAvailable(resource, nodename)
ptpservice = PtpService(notification_control)
ptpstatus = ptpservice.query(nodename,
self.resource_address, optional)
LOG.debug('Got ptpstatus: %s' % ptpstatus)
for item in ptpstatus:
ptpstatus[item]['time'] = datetime.fromtimestamp(
ptpstatus[item]['time']).strftime(
'%Y-%m-%dT%H:%M:%S%fZ')
return ptpstatus
except client_exception.NodeNotAvailable as ex:
LOG.warning("{0}".format(str(ex)))
abort(404)
except client_exception.ResourceNotAvailable as ex:
LOG.warning("{0}".format(str(ex)))
abort(404)
except oslo_messaging.exceptions.MessagingTimeout as ex:
LOG.warning("Resource is not reachable:{0}".format(str(ex)))
abort(404)
except HTTPException as ex:
LOG.warning("Client side error:{0},{1}".format(type(ex), str(ex)))
# raise ex
abort(400)
except TypeError as ex:
LOG.error("Resource {0} not found on {1}".format(self.resource_address, nodename))
abort(404)
except HTTPServerError as ex:
LOG.error("Server side error:{0},{1}".format(type(ex), str(ex)))
# raise ex
abort(500)
except Exception as ex:
LOG.error("Exception:{0}@{1}".format(type(ex), str(ex)))
abort(500)