From 51b7f8b206443c1503ea587690396e859afec9de Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 16 Oct 2019 07:02:38 +0530 Subject: [PATCH] Modify code to limit garbage collectible objects and create method to deploy adapters without custom mapping Removed creation of ArrayLists when a service is been called and added null validation to those lists in service layer. Developed code to deploy adapters when custom mapping is false and updated exception handling. --- .../beans/analytics/AdapterConfiguration.java | 2 +- .../AdapterMappingConfiguration.java | 10 +- .../jaxrs/beans/analytics/EventStream.java | 6 +- .../jaxrs/exception/BadRequestException.java | 21 +- .../InvalidExecutionPlanException.java | 2 +- ...alyticsArtifactsManagementServiceImpl.java | 404 ++++++++++++------ 6 files changed, 298 insertions(+), 147 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java index 5c0b820f41..1c07761073 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java @@ -27,7 +27,7 @@ import java.util.List; */ public class AdapterConfiguration { @ApiModelProperty(value = "Adapter properties list") - private List adapterProperties = new ArrayList<>(); + private List adapterProperties; @ApiModelProperty(value = "Custom-mapping flag") private boolean isCustomMappingEnabled; @ApiModelProperty(value = "Mapping configurations") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java index a9894f459e..6885c0a3c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java @@ -32,15 +32,15 @@ public class AdapterMappingConfiguration { @ApiModelProperty(value = "Input mapping for json,text and xml mappings") private String inputMappingString; @ApiModelProperty(value = "Input mapping for json,map and xml mappings") - private List inputMappingProperties = new ArrayList<>(); + private List inputMappingProperties; @ApiModelProperty(value = "Name-scape mapping for xml mapping") - private List namespaceMappingProperties = new ArrayList<>(); + private List namespaceMappingProperties; @ApiModelProperty(value = "Correlation mapping for wso2 mapping") - private List correlationMappingProperties = new ArrayList<>(); + private List correlationMappingProperties; @ApiModelProperty(value = "Payload mapping for wso2 mapping") - private List payloadMappingProperties = new ArrayList<>(); + private List payloadMappingProperties; @ApiModelProperty(value = "Meta mapping for wso2 mapping") - private List metaMappingProperties = new ArrayList<>(); + private List metaMappingProperties; public MessageFormat getMessageFormat() { return messageFormat; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java index ed166446f3..91a2db78e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java @@ -36,11 +36,11 @@ public class EventStream { @ApiModelProperty(value = "Stream description") private String description; @ApiModelProperty(value = "Meta attribute list") - private List metaData = new ArrayList<>(); + private List metaData; @ApiModelProperty(value = "Correlation attribute list") - private List correlationData = new ArrayList<>(); + private List correlationData; @ApiModelProperty(value = "Payload attribute list") - private List payloadData = new ArrayList<>(); + private List payloadData; @ApiModelProperty(value = "Stream definition" , notes = "use only when creating stream as a String") private String definition; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java index d97b78b6dd..6a09ee3af2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.jaxrs.exception; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -27,8 +28,26 @@ import javax.ws.rs.core.Response; * Custom exception class for wrapping BadRequest related exceptions. */ public class BadRequestException extends WebApplicationException { + private String message; + private static final long serialVersionUID = -24991723711391192L; public BadRequestException(ErrorResponse error) { super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); } -} \ No newline at end of file + + public BadRequestException(ErrorDTO errorDTO) { + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(errorDTO) + .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) + .build()); + message = errorDTO.getMessage(); + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java index cc094d3b77..8c453ba9cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java @@ -36,7 +36,7 @@ public class InvalidExecutionPlanException extends WebApplicationException { .entity(errorDTO) .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) .build()); - message = errorDTO.getDescription(); + message = errorDTO.getMessage(); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 5c4f686254..623d903400 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AdapterMappingConfiguration; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.MappingProperty; @@ -116,8 +115,12 @@ public class AnalyticsArtifactsManagementServiceImpl public Response deployEventDefinitionAsDto(@Valid EventStream stream) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { + validateStreamProperties(stream); deployStream(stream); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); @@ -187,6 +190,9 @@ public class AnalyticsArtifactsManagementServiceImpl } deployReceiver(receiver, customMapping, adapterConfiguration); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -256,6 +262,9 @@ public class AnalyticsArtifactsManagementServiceImpl } deployPublisher(publisher, customMapping, adapterConfiguration); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -283,8 +292,11 @@ public class AnalyticsArtifactsManagementServiceImpl @Valid SiddhiExecutionPlan plan) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { - publishSiddhiExecutionPlan(name, isEdited, plan.getDefinition()); + deploySiddhiExecutionPlan(name, isEdited, plan.getDefinition()); return Response.ok().build(); + } catch (InvalidExecutionPlanException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -315,14 +327,6 @@ public class AnalyticsArtifactsManagementServiceImpl List metaData = stream.getMetaData(); List payloadData = stream.getPayloadData(); List correlationData = stream.getCorrelationData(); - if (metaData.isEmpty() && correlationData.isEmpty() && payloadData.isEmpty()) { - String errMsg = String.format("Failed to validate Stream property attributes of %s:%s", - stream.getName(), stream.getVersion()); - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); - } try { eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); @@ -331,10 +335,15 @@ public class AnalyticsArtifactsManagementServiceImpl eventStreamDefinitionDto.setVersion(stream.getVersion()); eventStreamDefinitionDto.setNickName(stream.getNickName()); eventStreamDefinitionDto.setDescription(stream.getDescription()); - eventStreamDefinitionDto.setMetaData(addEventAttributesToDto(metaData)); - eventStreamDefinitionDto.setPayloadData(addEventAttributesToDto(payloadData)); - eventStreamDefinitionDto.setCorrelationData(addEventAttributesToDto(correlationData)); - + if (metaData != null) { + eventStreamDefinitionDto.setMetaData(addEventAttributesToDto(metaData)); + } + if (payloadData != null) { + eventStreamDefinitionDto.setPayloadData(addEventAttributesToDto(payloadData)); + } + if (correlationData != null) { + eventStreamDefinitionDto.setCorrelationData(addEventAttributesToDto(correlationData)); + } String streamId = stream.getName() + ":" + stream.getVersion(); if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamId) != null) { eventStreamAdminServiceStub.editEventStreamDefinitionAsDto(eventStreamDefinitionDto, streamId); @@ -344,7 +353,6 @@ public class AnalyticsArtifactsManagementServiceImpl } finally { cleanup(eventStreamAdminServiceStub); } - } /** @@ -360,8 +368,8 @@ public class AnalyticsArtifactsManagementServiceImpl private void deployReceiver(Adapter receiver, boolean customMapping, AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { - EventReceiverAdminServiceStub eventReceiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub(); - + EventReceiverAdminServiceStub eventReceiverAdminServiceStub = DeviceMgtAPIUtils + .getEventReceiverAdminServiceStub(); try { String receiverName = receiver.getAdapterName(); String adapterType = receiver.getAdapterType().toStringFormatted(); @@ -374,55 +382,95 @@ public class AnalyticsArtifactsManagementServiceImpl } BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos = addReceiverConfigToDto(adapterProperties); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - if (!messageFormat.toString().equals("wso2event")) { - EventMappingPropertyDto[] inputMappingPropertyDtos = - addReceiverMappingToDto(adapterMappingConfiguration.getInputMappingProperties()); - if (messageFormat.toString().equals("xml")) { - EventMappingPropertyDto[] namespaceMappingPropertyDtos = - addReceiverMappingToDto(adapterMappingConfiguration.getNamespaceMappingProperties()); - eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, null - , namespaceMappingPropertyDtos, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else { - if (messageFormat.toString().equals("map")) { - eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else if (messageFormat.toString().equals("text")) { - eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); + if (customMapping) { + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration + .getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + EventMappingPropertyDto[] inputMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getInputMappingProperties()); + if (messageFormat.toString().equals("xml")) { + EventMappingPropertyDto[] namespaceMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getNamespaceMappingProperties()); + eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null + , namespaceMappingPropertyDtos, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); } else { - eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); + if (messageFormat.toString().equals("map")) { + eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } else if (messageFormat.toString().equals("text")) { + eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } else { + eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } } + } else { + EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + + eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicInputAdapterPropertyDtos, true + , eventStreamWithVersion); } } else { - EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getCorrelationMappingProperties() - ); - EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getInputMappingProperties() - ); - EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getPayloadMappingProperties() - ); - - eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); + deployReceiverWithoutMapping(receiverName, eventStreamWithVersion, adapterType, + eventReceiverAdminServiceStub, basicInputAdapterPropertyDtos); } } finally { cleanup(eventReceiverAdminServiceStub); } } + /** + * To deploy receiver if custom mapping is false + * + * @param receiverName Name of the receiver + * @param eventStreamWithVersion Attached event stream of the receiver + * @param adapterType Adapter type name + * @param eventReceiverAdminServiceStub Stub to deploy receiver + * @param basicInputAdapterPropertyDtos DTO to attach receiver data + * @throws RemoteException Exception that may occur during a remote method call + */ + private void deployReceiverWithoutMapping(String receiverName, String eventStreamWithVersion + , String adapterType, EventReceiverAdminServiceStub eventReceiverAdminServiceStub + , BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos) + throws RemoteException { + switch (adapterType) { + case "iot-event": + case "wso2event": + eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null, null + , null, basicInputAdapterPropertyDtos, false + , eventStreamWithVersion); + break; + case "soap": + eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null, null + , null, basicInputAdapterPropertyDtos, false); + break; + default: + eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null + , basicInputAdapterPropertyDtos, false); + } + } + /** * Set data to a publisher dto and deploy dto through a stub * @@ -436,7 +484,8 @@ public class AnalyticsArtifactsManagementServiceImpl private void deployPublisher(Adapter publisher, boolean customMapping, AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { - EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils.getEventPublisherAdminServiceStub(); + EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils + .getEventPublisherAdminServiceStub(); try { String publisherName = publisher.getAdapterName(); String adapterType = publisher.getAdapterType().toStringFormatted(); @@ -451,53 +500,65 @@ public class AnalyticsArtifactsManagementServiceImpl BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos = addPublisherConfigToDto(adapterProperties); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - if (!messageFormat.toString().equals("wso2event")) { - if (!messageFormat.toString().equals("map")) { - if (messageFormat.toString().equals("xml")) { - eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); - } else if (messageFormat.toString().equals("text")) { - eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); + if (customMapping) { + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration + .getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + if (!messageFormat.toString().equals("map")) { + if (messageFormat.toString().equals("xml")) { + eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } else if (messageFormat.toString().equals("text")) { + eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } else { + eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } } else { - eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicOutputAdapterPropertyDtos, true); } } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getInputMappingProperties() + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + correlationMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + metaMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getMetaMappingProperties() ); - eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + payloadMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicOutputAdapterPropertyDtos, true + , eventStreamWithVersion); } } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] correlationMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getCorrelationMappingProperties() - ); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] metaMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getMetaMappingProperties() - ); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] payloadMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getPayloadMappingProperties() - ); - eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); + deployPublisherWithoutMapping(publisherName, eventStreamWithVersion, adapterType + , eventPublisherAdminServiceStub, basicOutputAdapterPropertyDtos); } } finally { @@ -505,6 +566,49 @@ public class AnalyticsArtifactsManagementServiceImpl } } + /** + * To deploy publisher if custom mapping is false + * + * @param publisherName Name of the publisher + * @param eventStreamWithVersion Attached event stream of the publisher + * @param adapterType Adapter type name + * @param eventPublisherAdminServiceStub Stub to deploy publisher + * @param basicOutputAdapterPropertyDtos DTO to attach publisher data + * @throws RemoteException Exception that may occur during a remote method call + */ + private void deployPublisherWithoutMapping(String publisherName, String eventStreamWithVersion + , String adapterType, EventPublisherAdminServiceStub eventPublisherAdminServiceStub + , BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos) + throws RemoteException { + switch (adapterType) { + case "wso2event": + case "ui": + case "secured-websocket": + eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , null, null, basicOutputAdapterPropertyDtos + , false, eventStreamWithVersion); + break; + case "soap": + eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , false); + break; + case "cassandra": + case "rdbms": + eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, false); + break; + default: + eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , false); + } + } + /** * Publish a siddhi execution plan using a stub * @@ -516,9 +620,9 @@ public class AnalyticsArtifactsManagementServiceImpl * @throws JWTClientException Exception that may occur during connecting to client store * @throws InvalidExecutionPlanException Exception that may occur if execution plan validation fails */ - private void publishSiddhiExecutionPlan(String name, boolean isEdited, - String plan) - throws RemoteException, UserStoreException, JWTClientException { + private void deploySiddhiExecutionPlan(String name, boolean isEdited, String plan) + throws RemoteException, UserStoreException, JWTClientException, + InvalidExecutionPlanException { EventProcessorAdminServiceStub eventProcessorAdminServiceStub = null; try { eventProcessorAdminServiceStub = DeviceMgtAPIUtils.getEventProcessorAdminServiceStub(); @@ -532,7 +636,6 @@ public class AnalyticsArtifactsManagementServiceImpl } else { ErrorDTO errorDTO = new ErrorDTO(); errorDTO.setMessage(validationResponse); - log.error(validationResponse); throw new InvalidExecutionPlanException(errorDTO); } } finally { @@ -541,20 +644,21 @@ public class AnalyticsArtifactsManagementServiceImpl } /** - * This will set payload of event attribute's mapping to the DTO + * Validate stream properties * - * @param attributes list of event attributes - * @return DTO with all the event attributes + * @param stream EventStream object */ - private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { - EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; - for (Attribute attribute : attributes) { - EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); - eventStreamAttributeDto.setAttributeName(attribute.getName()); - eventStreamAttributeDto.setAttributeType(attribute.getType().toString()); - + private void validateStreamProperties(EventStream stream) throws BadRequestException { + if ((stream.getMetaData() == null || stream.getMetaData().isEmpty()) && + (stream.getCorrelationData() == null || stream.getCorrelationData().isEmpty()) && + (stream.getPayloadData() == null || stream.getPayloadData().isEmpty())) { + String errMsg = String.format("Failed to validate Stream property attributes of %s:%s. " + + "Stream mapping can't be null or empty", + stream.getName(), stream.getVersion()); + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } - return eventStreamAttributeDtos; } /** @@ -562,13 +666,13 @@ public class AnalyticsArtifactsManagementServiceImpl * * @param adapterProperties Adapter payload attributes */ - private void validateAdapterProperties(List adapterProperties) { - if (adapterProperties.isEmpty()) { - String errMsg = "Invalid payload: event property attributes invalid!!!"; - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); + private void validateAdapterProperties(List adapterProperties) + throws BadRequestException { + if (adapterProperties == null) { + String errMsg = "Failed to validate adapter attributes. Adapter attributes can't be null"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } } @@ -583,25 +687,53 @@ public class AnalyticsArtifactsManagementServiceImpl * * @param adapterMappingConfiguration Adapter mapping attributes */ - private void validateAdapterMapping(AdapterMappingConfiguration adapterMappingConfiguration) { - - if (adapterMappingConfiguration.getInputMappingString() == null && - (adapterMappingConfiguration.getInputMappingProperties().isEmpty() && - adapterMappingConfiguration.getNamespaceMappingProperties().isEmpty()) && - ( - adapterMappingConfiguration.getCorrelationMappingProperties().isEmpty() && - adapterMappingConfiguration.getPayloadMappingProperties().isEmpty() && - adapterMappingConfiguration.getMetaMappingProperties().isEmpty() - ) - || adapterMappingConfiguration.getMessageFormat() == null) { - String errMsg = "Invalid payload: event mapping attributes invalid!!!"; - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); + private void validateAdapterMapping(AdapterMappingConfiguration adapterMappingConfiguration) + throws BadRequestException { + if (adapterMappingConfiguration == null) { + String errMsg = "Failed to validate adapter mapping attributes. " + + "Adapter mapping configuration can't be null"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); + } else if (adapterMappingConfiguration.getMessageFormat() == null || + ((adapterMappingConfiguration.getInputMappingString() == null) + && (adapterMappingConfiguration.getInputMappingProperties() == null || + adapterMappingConfiguration.getInputMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getNamespaceMappingProperties() == null || + adapterMappingConfiguration.getNamespaceMappingProperties().isEmpty())) + && + ((adapterMappingConfiguration.getCorrelationMappingProperties() == null || + adapterMappingConfiguration.getCorrelationMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getPayloadMappingProperties() == null || + adapterMappingConfiguration.getPayloadMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getMetaMappingProperties() == null || + adapterMappingConfiguration.getMetaMappingProperties().isEmpty())) + ) { + String errMsg = "Failed to validate adapter mapping attributes. " + + "Adapter mapping configuration invalid"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } } + /** + * This will set payload of event attribute's mapping to the DTO + * + * @param attributes list of event attributes + * @return DTO with all the event attributes + */ + private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { + EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; + for (int i = 0; i < attributes.size(); i++) { + EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); + eventStreamAttributeDto.setAttributeName(attributes.get(i).getName()); + eventStreamAttributeDto.setAttributeType(attributes.get(i).getType().toString()); + eventStreamAttributeDtos[i] = eventStreamAttributeDto; + } + return eventStreamAttributeDtos; + } + /** * This will set payload of receiver attributes to the DTO *