All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dell.cpsd.rcm.compliance.data.client.amqp.AmqpComplianceDataManager Maven / Gradle / Ivy

The newest version!
/**
 * Copyright © 2017 Dell Inc. or its subsidiaries.  All Rights Reserved.
 * Dell EMC Confidential/Proprietary Information
 */

package com.dell.cpsd.rcm.compliance.data.client.amqp;

import com.dell.cpsd.common.logging.ILogger;
import com.dell.cpsd.rcm.compliance.data.client.ComplianceDataServiceException;
import com.dell.cpsd.rcm.compliance.data.client.IComplianceDataConfiguration;
import com.dell.cpsd.rcm.compliance.data.client.amqp.consumer.IAmqpComplianceDataConsumer;
import com.dell.cpsd.rcm.compliance.data.client.amqp.consumer.IAmqpComplianceDataMessageHandler;
import com.dell.cpsd.rcm.compliance.data.client.amqp.producer.IAmqpComplianceDataProducer;
import com.dell.cpsd.rcm.compliance.data.client.callback.DeviceComplianceResponse;
import com.dell.cpsd.rcm.compliance.data.client.callback.ListSystemComplianceResponse;
import com.dell.cpsd.rcm.compliance.data.client.log.RCDSLoggingManager;
import com.dell.cpsd.rcm.compliance.data.client.log.RCDSMessageCode;
import com.dell.cpsd.rcm.compliance.data.client.model.DeviceComplianceData;
import com.dell.cpsd.rcm.compliance.data.client.model.SystemComplianceData;
import com.dell.cpsd.service.common.client.callback.IServiceCallback;
import com.dell.cpsd.service.common.client.callback.ServiceCallback;
import com.dell.cpsd.service.common.client.callback.ServiceError;
import com.dell.cpsd.service.common.client.callback.ServiceResponse;
import com.dell.cpsd.service.common.client.exception.ServiceTimeoutException;
import com.dell.cpsd.service.common.client.manager.AbstractServiceCallbackManager;
import com.dell.cpsd.service.common.client.task.ServiceTask;
import com.dell.cpsd.service.rcm.compliance.data.service.api.ComplianceDataErrorMessage;
import com.dell.cpsd.service.rcm.compliance.data.service.api.ConvergedSystem;
import com.dell.cpsd.service.rcm.compliance.data.service.api.Device;
import com.dell.cpsd.service.rcm.compliance.data.service.api.DeviceComplianceDataMessage;
import com.dell.cpsd.service.rcm.compliance.data.service.api.Group;
import com.dell.cpsd.service.rcm.compliance.data.service.api.SubComponent;
import com.dell.cpsd.service.rcm.compliance.data.service.api.SystemComplianceDataMessage;

import java.util.List;
import java.util.UUID;

/**
 * This class is responsible for sending and processing messages to and from
 * the compliance data service.
 *
 * 

* Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved. * Dell EMC Confidential/Proprietary Information *

* * @version 1.0 * @since 1.0 */ public class AmqpComplianceDataManager extends AbstractServiceCallbackManager implements IAmqpComplianceDataMessageHandler { /* * The logger for this class. */ private static final ILogger LOGGER = RCDSLoggingManager.getLogger(AmqpComplianceDataManager.class); /* * The routing key for the compute result message queue used by the consumer */ private String routingKey = null; /* * The configuration used by this service manager */ private IComplianceDataConfiguration configuration = null; /* * The compliance data message producer */ private IAmqpComplianceDataProducer complianceDataProducer = null; /* * The compliance data message consumer */ private IAmqpComplianceDataConsumer complianceDataConsumer = null; /** * AmqpComplianceDataManager constructor. * * @param configuration The configuration used by this manager. * @throws IllegalArgumentException Thrown if the configuration is null. * @since 1.0 */ public AmqpComplianceDataManager(IComplianceDataConfiguration configuration) { super(); if (configuration == null) { throw new IllegalArgumentException("The configuration is not set."); } this.configuration = configuration; this.setComplianceDataConsumer(configuration.getComplianceDataConsumer()); this.setComplianceDataProducer(configuration.getComplianceDataProducer()); this.setRoutingKey(this.complianceDataConsumer.getRoutingKey()); this.complianceDataConsumer.setComplianceDataMessageHandler(this); } /** * This returns the compliance data for the specified device. * * @param deviceUuid The uid of the system. * @return The compliance data for the specified system. * @throws ComplianceDataServiceException Thrown if the request fails. * @throws ServiceTimeoutException Thrown if the request fails. * @since SINCE-TDB */ public DeviceComplianceResponse getDeviceComplianceData(final String deviceUuid, final long timeout) throws ComplianceDataServiceException, ServiceTimeoutException { nullCheck("device", deviceUuid); this.shutdownCheck(); // create a correlation identifier for the operation final String requestId = createRequestId(); final ServiceCallback callback = createCallback(DeviceComplianceResponse.class); // add the callback using the correlation identifier as key this.createAndAddServiceTask(requestId, callback, timeout); // publish the list system compliance message to the service try { this.complianceDataProducer.publishDeviceCompliance(deviceUuid, requestId, this.routingKey); } catch (Exception exception) { // remove the compute callback if the message cannot be published this.removeServiceTask(requestId); logAndThrowException(exception); } // wait from the response from the service this.waitForServiceCallback(callback, requestId, timeout); // check for a service error this.checkForServiceError(callback); // if there was no error, then return the result return callback.getServiceResponse(); } /** * This returns the compliance data for the specified system. * * @param systemUid The uid of the system. * @return The compliance data for the specified system. * @throws ComplianceDataServiceException Thrown if the request fails. * @throws ServiceTimeoutException Thrown if the request fails. * @since 1.0 */ public ListSystemComplianceResponse listSystemCompliance(final String systemUid, final long timeout) throws ComplianceDataServiceException, ServiceTimeoutException { nullCheck("system", systemUid); this.shutdownCheck(); // create a correlation identifier for the operation final String requestId = this.createRequestId(); final ServiceCallback callback = createCallback(ListSystemComplianceResponse.class); this.createAndAddServiceTask(requestId, callback, timeout); // publish the list system compliance message to the service try { this.complianceDataProducer.publishListSystemCompliance(systemUid, requestId, this.routingKey); } catch (Exception exception) { // remove the compute callback if the message cannot be published this.removeServiceTask(requestId); logAndThrowException(exception); } // wait from the response from the service this.waitForServiceCallback(callback, requestId, timeout); this.checkForServiceError(callback); // if there was no compute error, then return the compute result return callback.getServiceResponse(); } /** * This handles the processing of a SystemComplianceDataMessage. * * @param message The SystemComplianceDataMessage to process. * @since 1.0 */ public void handleSystemComplianceData(SystemComplianceDataMessage message) { if (message == null) { return; } final String correlationId = message.getCorrelationId(); final IServiceCallback callback = this.removeServiceCallback(correlationId); if (callback == null) { return; } final List systems = message.getSystems(); final List groups = message.getGroups(); final List devices = message.getDevices(); final List subComponents = message.getSubComponents(); final SystemComplianceData systemComplianceData = new SystemComplianceData(systems, groups, devices, subComponents); final ListSystemComplianceResponse response = new ListSystemComplianceResponse(correlationId, systemComplianceData); // TODO : Take the callback processing off the message thread try { ((ServiceCallback) callback).handleServiceResponse(response); } catch (Exception exception) { // log the exception thrown by the compute callback Object[] lparams = {"handleSystemComplianceData", exception.getMessage()}; LOGGER.error(RCDSMessageCode.ERROR_CALLBACK_FAIL_E.getMessageCode(), lparams, exception); } } /** * This handles the processing of a ComplianceDataErrorMessage. * * @param message The ComplianceDataErrorMessage to process. * @since 1.0 */ public void handleComplianceDataError(ComplianceDataErrorMessage message) { if (message == null) { return; } final String correlationId = message.getCorrelationId(); final IServiceCallback callback = this.removeServiceCallback(correlationId); if (callback == null) { return; } final String errorCode = message.getErrorCode(); final String errorMessage = message.getErrorMessage(); final ServiceError error = new ServiceError(correlationId, errorCode, errorMessage); // TODO : Take the callback processing off the message thread try { callback.handleServiceError(error); } catch (Exception exception) { // log the exception thrown by the callback Object[] lparams = {"handleServiceError", exception.getMessage()}; LOGGER.error(RCDSMessageCode.ERROR_CALLBACK_FAIL_E.getMessageCode(), lparams, exception); } } /** * This handles the processing of a DeviceComplianceDataMessage. * * @param message The DeviceComplianceDataMessage to process. * @since 1.0 */ @Override public void handleDeviceComplianceData(final DeviceComplianceDataMessage message) { if (message == null) { return; } final String correlationId = message.getCorrelationId(); final IServiceCallback callback = this.removeServiceCallback(correlationId); if (callback == null) { return; } final Device device = message.getDevice(); final List subcomponents = message.getSubComponents(); final List groups = message.getGroups(); final List systems = message.getSystems(); final DeviceComplianceData deviceComplianceData = new DeviceComplianceData(device, subcomponents, groups, systems); final DeviceComplianceResponse response = new DeviceComplianceResponse(correlationId, deviceComplianceData); // TODO : Take the callback processing off the message thread try { ((ServiceCallback) callback).handleServiceResponse(response); } catch (Exception exception) { // log the exception thrown by the compute callback Object[] lparams = {"handleDeviceComplianceData", exception.getMessage()}; LOGGER.error(RCDSMessageCode.ERROR_CALLBACK_FAIL_E.getMessageCode(), lparams, exception); } } /** * This releases any resources associated with this manager. * * @since 1.0 */ @Override public void release() { super.release(); this.configuration = null; } /** * This returns the routing key used by the service consumer to * consume responses or error messages. * * @return The routing key. * @since 1.0 */ public String getRoutingKey() { return this.routingKey; } /** * This sets the compute result routing key used by the service consumer to * consume responses or error messages. * * @param routingKey The routing key. * @throws IllegalArgumentException Thrown if the routing key is null. * @since 1.0 */ public void setRoutingKey(String routingKey) { if (routingKey == null) { throw new IllegalArgumentException("The routing key is not set."); } this.routingKey = routingKey; } /** * This returns the compliance data message producer. * * @return The compliance data message producer. * @since 1.0 */ public IAmqpComplianceDataProducer getComplianceDataProducer() { return this.complianceDataProducer; } /** * This sets the compliance data message producer. * * @param complianceDataProducer The compliance data producer. * @throws IllegalArgumentException Thrown if the producer is null. * @since 1.0 */ protected void setComplianceDataProducer(final IAmqpComplianceDataProducer complianceDataProducer) { if (complianceDataProducer == null) { throw new IllegalArgumentException("The compliance data producer is null."); } this.complianceDataProducer = complianceDataProducer; } /** * This returns the compliance data message consumer. * * @return The compliance data message consumer. * @since 1.0 */ public IAmqpComplianceDataConsumer getComplianceDataConsumer() { return this.complianceDataConsumer; } /** * This sets the compliance data message consumer. * * @param complianceDataConsumer The compliance data consumer. * @throws IllegalArgumentException Thrown if the consumer is null. * @since 1.0 */ protected void setComplianceDataConsumer(final IAmqpComplianceDataConsumer complianceDataConsumer) { if (complianceDataConsumer == null) { throw new IllegalArgumentException("The compliance data consumer is null."); } this.complianceDataConsumer = complianceDataConsumer; } /** * This sets the compliance data message consumer. * * @param requestId pass the correlation identifier for the operation * * @since 1.0 */ private void createAndAddServiceTask(final String requestId, final ServiceCallback callback, final long timeout) { // the infinite timeout is used for the task because it is handled with // this synchronous call. ServiceTask> task = new ServiceTask<>(requestId, callback, timeout); // add the compute callback using the correlation identifier as key this.addServiceTask(requestId, task); } /** * This logs the exception message and throws the ComplianceDataServiceException. * * @param exception pass the original exception * * @since 1.0 */ private void logAndThrowException(final Exception exception) throws ComplianceDataServiceException { Object[] lparams = {exception.getMessage()}; String lmessage = LOGGER.error(RCDSMessageCode.PUBLISH_MESSAGE_FAIL_E.getMessageCode(), lparams, exception); throw new ComplianceDataServiceException(lmessage, exception); } /** * This creates a correlation identifier for the operation. * * @since 1.0 */ private String createRequestId() { return UUID.randomUUID().toString(); } /** * This returns a ServiceCallback * . * @param callbackType pass the call back Type like ListSystemComplianceResponse.class * @since 1.0 */ private > ServiceCallback createCallback(final Class callbackType) { return new ServiceCallback(); } /** * This checks if there are errors in the ServiceCallback and throws the ComplianceDataServiceException. * * @param callback pass the ServiceCallback * @since 1.0 */ protected void checkForServiceError(final ServiceCallback callback) throws ComplianceDataServiceException { // check to see if a compute error has been handled by the manager ServiceError error = callback.getServiceError(); // throw a compute exception using the message in the compute error if (error != null) { throw new ComplianceDataServiceException(error.getErrorMessage()); } } /** * This logs the exception message and throws the ServiceTimeoutException. * * @param requestId pass the request id * * @since 1.0 */ protected void logAndThrowTimeoutException(final String requestId, final long timeLimit) throws ServiceTimeoutException { Object[] lparams = {requestId, "" + timeLimit}; String lmessage = LOGGER.error(RCDSMessageCode.MESSAGE_TIMEOUT_E.getMessageCode(), lparams); throw new ServiceTimeoutException(lmessage); } /** * This checks if the thread - ScheduledExecutorService is shutdown and throws the ComplianceDataServiceException. * * @since 1.0 */ protected void shutdownCheck() throws ComplianceDataServiceException { if (this.isShutDown()) { String lmessage = LOGGER.error(RCDSMessageCode.MANAGER_SHUTDOWN_E.getMessageCode()); throw new ComplianceDataServiceException(lmessage); } } /** * This performs a null check to ensure uuid is present and if absent throws the ComplianceDataServiceException. * * @param deviceType pass the deviceType * @param uuidToCheck pass the uuid * * @since 1.0 */ private void nullCheck(final String deviceType, String uuidToCheck) throws ComplianceDataServiceException { if (uuidToCheck == null) { throw new ComplianceDataServiceException("The " + deviceType + " uuid is null"); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy