Fix responses in PTP notification client
This commit provides better error handling and API responses for the PTP event pull and subcription API. Test Plan: PASS: Build notificationclient image PASS: Test error paths for event pull API PASS: Test error paths for add subscription API PASS: Test error paths for get subscription API Closes-Bug: 1997604 Closes-Bug: 1997605 Closes-Bug: 1997606 Change-Id: I7d3d668cd2963dc72d80bb29402026293a80dee6 Signed-off-by: Teresa Ho <teresa.ho@windriver.com>
This commit is contained in:
parent
e26071b94c
commit
dffee68f5b
notificationclient-base/docker/notificationclient-sidecar
notificationclientsdk
sidecar/controllers
@ -12,6 +12,7 @@ import logging
|
||||
from datetime import datetime
|
||||
from notificationclientsdk.common.helpers import constants
|
||||
from notificationclientsdk.common.helpers import log_helper
|
||||
from notificationclientsdk.exception import client_exception
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
log_helper.config_logger(LOG)
|
||||
@ -31,6 +32,8 @@ def notify(subscriptioninfo, notification, timeout=2, retry=3):
|
||||
response.raise_for_status()
|
||||
result = True
|
||||
return response
|
||||
except client_exception.InvalidResource as ex:
|
||||
raise ex
|
||||
except requests.exceptions.ConnectionError as errc:
|
||||
if retry > 0:
|
||||
LOG.warning("Retry notifying due to: {0}".format(str(errc)))
|
||||
@ -62,6 +65,8 @@ def format_notification_data(subscriptioninfo, notification):
|
||||
elif hasattr(subscriptioninfo, 'ResourceAddress'):
|
||||
_, _, resource_path, _, _ = parse_resource_address(
|
||||
subscriptioninfo.ResourceAddress)
|
||||
if resource_path not in constants.RESOURCE_ADDRESS_MAPPINGS.keys():
|
||||
raise client_exception.InvalidResource(resource_path)
|
||||
resource_mapped_value = constants.RESOURCE_ADDRESS_MAPPINGS[
|
||||
resource_path]
|
||||
formatted_notification = {resource_mapped_value: []}
|
||||
|
@ -27,8 +27,14 @@ class InvalidEndpoint(Exception):
|
||||
self.endpoint_uri = endpoint_uri
|
||||
|
||||
def __str__(self):
|
||||
return "Endpoint is invalid:{0}".format(self.endpoint_uri)
|
||||
return "Endpoint is invalid: {0}".format(self.endpoint_uri)
|
||||
|
||||
class InvalidResource(Exception):
|
||||
def __init__(self, resource):
|
||||
self.resource = resource
|
||||
|
||||
def __str__(self):
|
||||
return "Resource is invalid: {0}".format(self.resource)
|
||||
|
||||
class InvalidSubscription(Exception):
|
||||
def __init__(self, subscriptioninfo):
|
||||
|
@ -8,6 +8,7 @@ import oslo_messaging
|
||||
import logging
|
||||
import json
|
||||
import kombu
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
from notificationclientsdk.client.notificationservice \
|
||||
@ -244,6 +245,7 @@ class PtpService(object):
|
||||
ptpstatus))
|
||||
|
||||
# construct subscription entry
|
||||
timestamp = None
|
||||
if constants.PTP_V1_KEY in ptpstatus:
|
||||
timestamp = ptpstatus[constants.PTP_V1_KEY].get(
|
||||
'EventTimestamp', None)
|
||||
@ -273,11 +275,15 @@ class PtpService(object):
|
||||
subscription_helper.notify(subscription_dto2, node[1])
|
||||
LOG.info("Initial ptpstatus of {0} is delivered successfully"
|
||||
"".format(node[0]))
|
||||
except requests.exceptions.RequestException as ex:
|
||||
LOG.warning("initial ptpstatus is not delivered: {0}".format(
|
||||
str(ex)))
|
||||
raise client_exception.InvalidEndpoint(
|
||||
subscription_dto2.EndpointUri)
|
||||
except Exception as ex:
|
||||
LOG.warning("Initial ptpstatus of {0} is not delivered:{1}"
|
||||
"".format(node[0], str(ex)))
|
||||
raise client_exception.InvalidEndpoint(
|
||||
subscription_dto.EndpointUri)
|
||||
raise ex
|
||||
|
||||
try:
|
||||
# commit the subscription entry
|
||||
|
@ -57,8 +57,9 @@ class V2Controller(rest.RestController):
|
||||
def _lookup(self, primary_key, *remainder):
|
||||
if primary_key:
|
||||
if 'subscriptions' == primary_key.lower():
|
||||
return SubscriptionsControllerV2(), remainder
|
||||
else:
|
||||
if not remainder:
|
||||
return SubscriptionsControllerV2(), remainder
|
||||
elif remainder:
|
||||
if 'currentstate' == remainder[-1].lower():
|
||||
resource_address_array = remainder[:-1]
|
||||
resource_address = '/' + primary_key + '/' + '/'.join(resource_address_array)
|
||||
|
7
notificationclient-base/docker/notificationclient-sidecar/sidecar/controllers/v2/resource_address.py
7
notificationclient-base/docker/notificationclient-sidecar/sidecar/controllers/v2/resource_address.py
@ -39,17 +39,14 @@ class ResourceAddressController(object):
|
||||
LOG.debug('Nodename to query: %s' % nodename)
|
||||
if not notification_control.in_service_nodenames(nodename):
|
||||
LOG.warning("Node {} is not available".format(nodename))
|
||||
abort(404)
|
||||
raise client_exception.NodeNotAvailable(nodename)
|
||||
if resource not in constants.VALID_SOURCE_URI:
|
||||
LOG.warning("Resource {} is not valid".format(resource))
|
||||
abort(404)
|
||||
raise client_exception.ResourceNotAvailable(resource, nodename)
|
||||
ptpservice = PtpService(notification_control)
|
||||
ptpstatus = ptpservice.query(nodename,
|
||||
self.resource_address, optional)
|
||||
LOG.debug('Got ptpstatus: %s' % ptpstatus)
|
||||
# Change time from float to ascii format
|
||||
# ptpstatus['time'] = time.strftime('%Y-%m-%dT%H:%M:%SZ',
|
||||
# time.gmtime(ptpstatus['time']))
|
||||
for item in ptpstatus:
|
||||
ptpstatus[item]['time'] = datetime.fromtimestamp(
|
||||
ptpstatus[item]['time']).strftime(
|
||||
|
@ -65,8 +65,10 @@ class SubscriptionsControllerV2(rest.RestController):
|
||||
abort(int(str(err)))
|
||||
except client_exception.InvalidSubscription:
|
||||
abort(400)
|
||||
except client_exception.InvalidEndpoint:
|
||||
abort(400)
|
||||
except client_exception.InvalidEndpoint as ex:
|
||||
abort(400, str(ex))
|
||||
except client_exception.InvalidResource as ex:
|
||||
abort(400, str(ex))
|
||||
except client_exception.NodeNotAvailable:
|
||||
abort(404)
|
||||
except client_exception.ResourceNotAvailable:
|
||||
|
Loading…
x
Reference in New Issue
Block a user