diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/pom.xml new file mode 100644 index 00000000000..4271f6a8150 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/pom.xml @@ -0,0 +1,119 @@ + + + + + apimgt-extensions + org.wso2.carbon.devicemgt + 5.0.21-SNAPSHOT + + + 4.0.0 + io.entgra.device.mgt.core.apimgt.analytics.extension + bundle + Entgra - API mgt analytics extension + http://wso2.org + + + + org.wso2.carbon + org.wso2.carbon.core + + + org.wso2.carbon + org.wso2.carbon.utils + + + org.apache.velocity + velocity + 1.7 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/Abstract* + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + API Management Application Bundle + org.wso2.carbon.apimgt.application.extension.internal + + io.entgra.device.mgt.core.apimgt.analytics.extension.dto, + org.apache.velocity, + org.apache.velocity.app, + org.apache.velocity.context, + org.wso2.carbon.utils;version="[4.6,5)" + + + io.entgra.device.mgt.core.apimgt.analytics.extension.* + + + scribe;scope=compile|runtime;inline=false; + + + + + + org.jacoco + jacoco-maven-plugin + + ${basedir}/target/coverage-reports/jacoco-unit.exec + + + + jacoco-initialize + + prepare-agent + + + + jacoco-site + test + + report + + + ${basedir}/target/coverage-reports/jacoco-unit.exec + ${basedir}/target/coverage-reports/site + + + + + + + + diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/AnalyticsArtifactsDeployer.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/AnalyticsArtifactsDeployer.java new file mode 100644 index 00000000000..6731aaee3fe --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/AnalyticsArtifactsDeployer.java @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension; + +import io.entgra.device.mgt.core.apimgt.analytics.extension.dto.*; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventPublisherDeployerException; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventReceiverDeployerException; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventStreamDeployerException; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; + +import org.apache.velocity.runtime.RuntimeConstants; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.io.*; + +public class AnalyticsArtifactsDeployer { + + public static final String TEMPLATE_LOCATION = "repository" + File.separator + "resources" + File.separator + "io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/resources/iot-analytics-templates"; + public static final String EVENT_STREAM_LOCATION = "eventstreams"; + public static final String EVENT_PUBLISHER_LOCATION = "eventpublishers"; + public static final String EVENT_RECEIVER_LOCATION = "eventreceivers"; + public static final String EVENT_STREAM_TEMPLATE = TEMPLATE_LOCATION + File.separator + "event_stream.json.template"; + public static final String EVENT_PUBLISHER_TEMPLATE = TEMPLATE_LOCATION + File.separator + "event_publisher.xml.template"; + public static final String EVENT_RECEIVER_TEMPLATE = TEMPLATE_LOCATION + File.separator + "event_receiver.xml.template"; + + + public void deployEventStream(EventStreamData eventStreamData, int tenantId) throws EventStreamDeployerException { + try { + VelocityEngine ve = new VelocityEngine(); + ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome()); + ve.init(); + Template template = ve.getTemplate(EVENT_STREAM_TEMPLATE); + + VelocityContext context = populateContextForEventStreams(eventStreamData); + StringWriter writer = new StringWriter(); + template.merge(context, writer); + + String fileName = eventStreamData.getName() + "_" + eventStreamData.getVersion() + ".json"; + String fileLocation = null; + if (MultitenantConstants.SUPER_TENANT_ID == tenantId) { + fileLocation = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "deployment" + + File.separator + "server" + File.separator + EVENT_STREAM_LOCATION + File.separator + fileName; + } else { + fileLocation = CarbonUtils.getCarbonTenantsDirPath() + File.separator + tenantId + File.separator + + EVENT_STREAM_LOCATION + File.separator + fileName; + } + + PrintWriter printWriter = new PrintWriter(fileLocation, "UTF-8"); + printWriter.println(writer.toString()); + printWriter.close(); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + throw new EventStreamDeployerException("Error while persisting event stream definition ", e); + } + } + + public void deployEventPublisher(EventPublisherData eventPublisherData, int tenantId) throws EventPublisherDeployerException { + try { + VelocityEngine ve = new VelocityEngine(); + ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome()); + ve.init(); + Template template = ve.getTemplate(EVENT_PUBLISHER_TEMPLATE); + + VelocityContext context = populateContextForEventPublisher(eventPublisherData); + StringWriter writer = new StringWriter(); + template.merge(context, writer); + + String fileName = eventPublisherData.getName() + ".xml"; + String fileLocation = null; + if (MultitenantConstants.SUPER_TENANT_ID == tenantId) { + fileLocation = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "deployment" + + File.separator + "server" + File.separator + EVENT_PUBLISHER_LOCATION + File.separator + fileName; + } else { + fileLocation = CarbonUtils.getCarbonTenantsDirPath() + File.separator + tenantId + File.separator + + EVENT_PUBLISHER_LOCATION + File.separator + fileName; + } + + PrintWriter printWriter = new PrintWriter(fileLocation, "UTF-8"); + printWriter.println(writer.toString()); + printWriter.close(); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + throw new EventPublisherDeployerException("Error while persisting rdbms event publisher ", e); + } + } + + public void deployEventReceiver(EventReceiverData eventReceiverData, int tenantId) throws EventReceiverDeployerException { + try { + VelocityEngine ve = new VelocityEngine(); + ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome()); + ve.init(); + Template template = ve.getTemplate(EVENT_RECEIVER_TEMPLATE); + + VelocityContext context = populateContextForEventReceiver(eventReceiverData); + StringWriter writer = new StringWriter(); + template.merge(context, writer); + + String fileName = eventReceiverData.getName() + ".xml"; + String fileLocation = null; + if (MultitenantConstants.SUPER_TENANT_ID == tenantId) { + fileLocation = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "deployment" + + File.separator + "server" + File.separator + EVENT_RECEIVER_LOCATION + File.separator + fileName; + } else { + fileLocation = CarbonUtils.getCarbonTenantsDirPath() + File.separator + tenantId + File.separator + + EVENT_RECEIVER_LOCATION + File.separator + fileName; + } + + PrintWriter printWriter = new PrintWriter(fileLocation, "UTF-8"); + printWriter.println(writer.toString()); + printWriter.close(); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + throw new EventReceiverDeployerException("Error while persisting oauth mqtt event receiver ", e); + } + } + + private VelocityContext populateContextForEventStreams(EventStreamData eventStreamData) { + VelocityContext context = new VelocityContext(); + context.put("name", eventStreamData.getName()); + context.put("version", eventStreamData.getVersion()); + context.put("metaData", + eventStreamData.getMetaData() != null ? eventStreamData.getMetaData() : new MetaData("deviceId", "STRING")); + if (eventStreamData.getPayloadData() != null) { + context.put("properties", eventStreamData.getPayloadData()); + } + return context; + } + + private VelocityContext populateContextForEventPublisher(EventPublisherData eventPublisherData) { + VelocityContext context = new VelocityContext(); + + context.put("name", eventPublisherData.getName()); + context.put("streamName", eventPublisherData.getStreamName()); + context.put("streamVersion", eventPublisherData.getStreamVersion()); + context.put("properties", eventPublisherData.getPropertyList()); + context.put("eventAdapterType", eventPublisherData.getEventAdaptorType()); + context.put("customMappingType", eventPublisherData.getCustomMappingType()); + + return context; + } + + private VelocityContext populateContextForEventReceiver(EventReceiverData eventReceiverData) { + VelocityContext context = new VelocityContext(); + + context.put("name", eventReceiverData.getName()); + context.put("streamName", eventReceiverData.getStreamName()); + context.put("streamVersion", eventReceiverData.getStreamVersion()); + context.put("properties", eventReceiverData.getPropertyList()); + context.put("eventAdapterType", eventReceiverData.getEventAdapterType()); + context.put("customMappingType", eventReceiverData.getCustomMappingType()); + + return context; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventPublisherData.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventPublisherData.java new file mode 100644 index 00000000000..6d1620a5b90 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventPublisherData.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.dto; + +import java.util.List; + +public class EventPublisherData { + private String name; + private String streamVersion; + private String streamName; + + private List propertyList; + + private String eventAdaptorType; + + private String customMappingType; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getStreamVersion() { + return streamVersion; + } + + public void setStreamVersion(String streamVersion) { + this.streamVersion = streamVersion; + } + + public String getStreamName() { + return streamName; + } + + public void setStreamName(String streamName) { + this.streamName = streamName; + } + + public List getPropertyList() { + return propertyList; + } + + public void setPropertyList(List propertyList) { + this.propertyList = propertyList; + } + + public String getEventAdaptorType() { + return eventAdaptorType; + } + + public void setEventAdaptorType(String eventAdaptorType) { + this.eventAdaptorType = eventAdaptorType; + } + + public String getCustomMappingType() { + return customMappingType; + } + + public void setCustomMappingType(String customMappingType) { + this.customMappingType = customMappingType; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventReceiverData.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventReceiverData.java new file mode 100644 index 00000000000..764229c55a3 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventReceiverData.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.dto; + +import java.util.List; + +public class EventReceiverData { + + private String name; + private String streamVersion; + private String streamName; + + private String eventAdapterType; + + List propertyList; + + private String customMappingType; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getStreamVersion() { + return streamVersion; + } + + public void setStreamVersion(String streamVersion) { + this.streamVersion = streamVersion; + } + + public String getStreamName() { + return streamName; + } + + public void setStreamName(String streamName) { + this.streamName = streamName; + } + + public String getEventAdapterType() { + return eventAdapterType; + } + + public void setEventAdapterType(String eventAdapterType) { + this.eventAdapterType = eventAdapterType; + } + + public List getPropertyList() { + return propertyList; + } + + public void setPropertyList(List propertyList) { + this.propertyList = propertyList; + } + + public String getCustomMappingType() { + return customMappingType; + } + + public void setCustomMappingType(String customMappingType) { + this.customMappingType = customMappingType; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventStreamData.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventStreamData.java new file mode 100644 index 00000000000..a80520bef4b --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/EventStreamData.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.dto; + +import java.util.List; + +public class EventStreamData { + private String name; + private String version; + private MetaData metaData; + private List payloadData; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public MetaData getMetaData() { + return metaData; + } + + public void setMetaData(MetaData metaData) { + this.metaData = metaData; + } + + public List getPayloadData() { + return payloadData; + } + + public void setPayloadData(List payloadData) { + this.payloadData = payloadData; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/MetaData.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/MetaData.java new file mode 100644 index 00000000000..ba77d04413b --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/MetaData.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.dto; + +public class MetaData { + String name; + String type; + + public MetaData(String name, String type){ + this.setName(name); + this.setType(type); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/Property.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/Property.java new file mode 100644 index 00000000000..995cf2c5a85 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/dto/Property.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.apimgt.analytics.extension.dto; + +public class Property { + String name; + String value; + + public Property(String name, String value){ + this.setName(name); + this.setValue(value); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventPublisherDeployerException.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventPublisherDeployerException.java new file mode 100644 index 00000000000..2f924d0e7ba --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventPublisherDeployerException.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.exception; + +public class EventPublisherDeployerException extends Exception { + + private static final long serialVersionUID = -3151279311929070299L; + + public EventPublisherDeployerException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public EventPublisherDeployerException(String message, Throwable cause) { + super(message, cause); + } + + public EventPublisherDeployerException(String msg) { + super(msg); + } + + public EventPublisherDeployerException() { + super(); + } + + public EventPublisherDeployerException(Throwable cause) { + super(cause); + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventReceiverDeployerException.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventReceiverDeployerException.java new file mode 100644 index 00000000000..3e0d49e0883 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventReceiverDeployerException.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.exception; + +public class EventReceiverDeployerException extends Exception { + + private static final long serialVersionUID = -3151279311929070299L; + + public EventReceiverDeployerException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public EventReceiverDeployerException(String message, Throwable cause) { + super(message, cause); + } + + public EventReceiverDeployerException(String msg) { + super(msg); + } + + public EventReceiverDeployerException() { + super(); + } + + public EventReceiverDeployerException(Throwable cause) { + super(cause); + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventStreamDeployerException.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventStreamDeployerException.java new file mode 100644 index 00000000000..1bba4c8cfe9 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension/src/main/java/io/entgra/device/mgt/core/apimgt/analytics/extension/exception/EventStreamDeployerException.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.apimgt.analytics.extension.exception; + +public class EventStreamDeployerException extends Exception { + + private static final long serialVersionUID = -3151279311929070298L; + + public EventStreamDeployerException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public EventStreamDeployerException(String message, Throwable cause) { + super(message, cause); + } + + public EventStreamDeployerException(String msg) { + super(msg); + } + + public EventStreamDeployerException() { + super(); + } + + public EventStreamDeployerException(Throwable cause) { + super(cause); + } +} diff --git a/components/apimgt-extensions/pom.xml b/components/apimgt-extensions/pom.xml index ba99795a045..f2426c980f4 100644 --- a/components/apimgt-extensions/pom.xml +++ b/components/apimgt-extensions/pom.xml @@ -39,6 +39,7 @@ org.wso2.carbon.apimgt.annotations org.wso2.carbon.apimgt.keymgt.extension org.wso2.carbon.apimgt.keymgt.extension.api + io.entgra.device.mgt.core.apimgt.analytics.extension diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index f43128fc6cf..f552c5aebfd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -450,5 +450,10 @@ org.wso2.carbon.event.output.adapter.rdbms provided + + org.wso2.carbon.devicemgt + io.entgra.device.mgt.core.apimgt.analytics.extension + provided + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index 49cbdae59a8..23be4d88ebb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -1,30 +1,20 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import edu.emory.mathcs.backport.java.util.Arrays; +import io.entgra.device.mgt.core.apimgt.analytics.extension.AnalyticsArtifactsDeployer; +import io.entgra.device.mgt.core.apimgt.analytics.extension.dto.*; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventReceiverDeployerException; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventPublisherDeployerException; +import io.entgra.device.mgt.core.apimgt.analytics.extension.exception.EventStreamDeployerException; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Stub; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.velocity.util.ArrayListWrapper; -import org.json.JSONObject; -import org.opensaml.xml.signature.J; -import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminService; -import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException; -import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub; -import org.wso2.carbon.analytics.stream.persistence.stub.dto.AnalyticsTable; -import org.wso2.carbon.analytics.stream.persistence.stub.dto.AnalyticsTableRecord; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.databridge.commons.StreamDefinition; import org.wso2.carbon.databridge.commons.exception.MalformedStreamDefinitionException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.DeviceTypeEvent; @@ -34,24 +24,16 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceEventManagementService import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.event.input.adapter.core.InputEventAdapterConfiguration; -import org.wso2.carbon.event.input.adapter.core.MessageType; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; -import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; -import org.wso2.carbon.event.output.adapter.rdbms.RDBMSEventAdapter; -import org.wso2.carbon.event.output.adapter.rdbms.internal.ds.RDBMSEventAdapterServiceDS; -import org.wso2.carbon.event.processor.manager.core.EventProcessorManagementService; -import org.wso2.carbon.event.processor.manager.core.EventPublisherManagementService; import org.wso2.carbon.event.publisher.core.EventPublisherService; import org.wso2.carbon.event.publisher.core.config.EventPublisherConfiguration; import org.wso2.carbon.event.publisher.core.config.mapping.JSONOutputMapping; import org.wso2.carbon.event.publisher.core.config.mapping.MapOutputMapping; import org.wso2.carbon.event.publisher.core.exception.EventPublisherConfigurationException; -import org.wso2.carbon.event.publisher.core.internal.ds.EventPublisherServiceDS; import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceCallbackHandler; import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub; import org.wso2.carbon.event.receiver.core.EventReceiverService; import org.wso2.carbon.event.receiver.core.config.EventReceiverConfiguration; -import org.wso2.carbon.event.receiver.core.config.InputMapping; import org.wso2.carbon.event.receiver.core.config.mapping.JSONInputMapping; import org.wso2.carbon.event.receiver.core.config.mapping.WSO2EventInputMapping; import org.wso2.carbon.event.receiver.core.exception.EventReceiverConfigurationException; @@ -59,7 +41,6 @@ import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceCallbackHand import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; import org.wso2.carbon.event.receiver.stub.types.BasicInputAdapterPropertyDto; import org.wso2.carbon.event.receiver.stub.types.EventReceiverConfigurationDto; -import org.wso2.carbon.event.receiver.stub.types.InputAdapterConfigurationDto; import org.wso2.carbon.event.stream.core.EventStreamService; import org.wso2.carbon.event.stream.core.exception.EventStreamConfigurationException; import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub; @@ -71,7 +52,6 @@ import org.wso2.carbon.user.api.UserStoreException; import javax.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.Response; -import java.io.IOException; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.HashMap; @@ -225,6 +205,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { for (DeviceTypeEvent deviceTypeEvent : deviceTypeEvents) { TransportType transportType = deviceTypeEvent.getTransportType(); @@ -239,46 +220,101 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); } + // event stream String streamName = DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain, eventName); - publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes); - + AnalyticsArtifactsDeployer artifactsDeployer = new AnalyticsArtifactsDeployer(); + List props = new ArrayList<>(); + for (Attribute attribute : eventAttributes.getList()) { + props.add(new Property(attribute.getName(), attribute.getType().name())); + } + EventStreamData eventStreamData = new EventStreamData(); + eventStreamData.setName(streamName); + eventStreamData.setVersion(Constants.DEFAULT_STREAM_VERSION); + eventStreamData.setMetaData(new MetaData(DEFAULT_DEVICE_ID_ATTRIBUTE, "STRING")); + eventStreamData.setPayloadData(props); + artifactsDeployer.deployEventStream(eventStreamData, tenantId); + + // event receiver String receiverName = getReceiverName(deviceType, tenantDomain, transportType, eventName); - publishEventReceivers(streamName, Constants.DEFAULT_STREAM_VERSION, transportType, tenantDomain, - isSharedWithAllTenants, deviceType, deviceTypeEvent.getEventTopicStructure(), receiverName); + EventReceiverData receiverData = new EventReceiverData(); + receiverData.setName(receiverName); + receiverData.setStreamName(streamName); + receiverData.setStreamVersion(Constants.DEFAULT_STREAM_VERSION); + List propertyList = new ArrayList<>(); + if (transportType == TransportType.MQTT) { + receiverData.setEventAdapterType(OAUTH_MQTT_ADAPTER_TYPE); + propertyList.add(new Property(MQTT_CONTENT_TRANSFORMER_TYPE, MQTT_CONTENT_TRANSFORMER)); + propertyList.add(new Property(MQTT_CONTENT_VALIDATOR_TYPE, MQTT_CONTENT_VALIDATOR)); + String topic; + if (!StringUtils.isEmpty(deviceTypeEvent.getEventTopicStructure())) { + if (isSharedWithAllTenants) { + topic = deviceTypeEvent.getEventTopicStructure().replace("${deviceId}", "+") + .replace("${deviceType}", deviceType) + .replace("${tenantDomain}", "+"); + } else { + topic = deviceTypeEvent.getEventTopicStructure().replace("${deviceId}", "+") + .replace("${deviceType}", deviceType) + .replace("${tenantDomain}", tenantDomain); + } + } else { + if (isSharedWithAllTenants) { + topic = "+/" + deviceType + "/+/events"; + } else { + topic = tenantDomain + "/" + deviceType + "/+/events"; + } + } + propertyList.add(new Property("topic", topic)); + receiverData.setCustomMappingType("json"); + + } else { + receiverData.setEventAdapterType(THRIFT_ADAPTER_TYPE); + propertyList.add(new Property("events.duplicated.in.cluster", "false")); + receiverData.setCustomMappingType("wso2event"); + } + receiverData.setPropertyList(propertyList); + artifactsDeployer.deployEventReceiver(receiverData, tenantId); + if (!skipPersist) { + // rdbms event publisher String rdbmsPublisherName = getPublisherName(deviceType, tenantDomain, eventName) + "_rdbms_publisher"; - publishEventStore(streamName, Constants.DEFAULT_STREAM_VERSION, rdbmsPublisherName); + + EventPublisherData eventPublisherData = new EventPublisherData(); + eventPublisherData.setName(rdbmsPublisherName); + eventPublisherData.setStreamName(streamName); + eventPublisherData.setStreamVersion(Constants.DEFAULT_STREAM_VERSION); + eventPublisherData.setEventAdaptorType("rdbms"); + eventPublisherData.setCustomMappingType("map"); + List publisherProps = new ArrayList<>(); + publisherProps.add(new Property("datasource.name", "EVENT_DB")); + publisherProps.add(new Property("table.name", "table_" + rdbmsPublisherName.replace(".", ""))); + publisherProps.add(new Property("execution.mode", "insert")); + eventPublisherData.setPropertyList(publisherProps); + artifactsDeployer.deployEventPublisher(eventPublisherData, tenantId); } + + // web socket event publisher String wsPublisherName = getPublisherName(deviceType, tenantDomain, eventName) + "_ws_publisher"; - publishWebsocketPublisherDefinition(streamName, Constants.DEFAULT_STREAM_VERSION, wsPublisherName); - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( - MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); - if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { - publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes); - publishEventReceivers(streamName, Constants.DEFAULT_STREAM_VERSION, transportType, tenantDomain, - isSharedWithAllTenants, deviceType, deviceTypeEvent.getEventTopicStructure(), receiverName); - } - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } + EventPublisherData wsEventPublisherData = new EventPublisherData(); + wsEventPublisherData.setName(wsPublisherName); + wsEventPublisherData.setStreamName(streamName); + wsEventPublisherData.setStreamVersion(Constants.DEFAULT_STREAM_VERSION); + wsEventPublisherData.setEventAdaptorType("websocket-local"); + wsEventPublisherData.setCustomMappingType("json"); + artifactsDeployer.deployEventPublisher(wsEventPublisherData, tenantId); + } return Response.ok().build(); } catch (DeviceManagementException e) { log.error("Failed to access device management service, tenantDomain: " + tenantDomain, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (MalformedStreamDefinitionException e) { - log.error("Failed while creating stream definition, tenantDomain: " + tenantDomain, e); + } catch (EventStreamDeployerException e) { + log.error("Failed while deploying event stream definition, tenantDomain: " + tenantDomain, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (EventStreamConfigurationException e) { - log.error("Failed while configuring stream definition, tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (EventPublisherConfigurationException e) { - log.error("Failed while configuring event publisher, tenantDomain: " + tenantDomain, e); + } catch (EventPublisherDeployerException e) { + log.error("Failed while deploying event publisher, tenantDomain: " + tenantDomain, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (EventReceiverConfigurationException e) { - log.error("Failed while configuring event receiver, tenantDomain: " + tenantDomain, e); + } catch (EventReceiverDeployerException e) { + log.error("Failed while deploying event receiver, tenantDomain: " + tenantDomain, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/pom.xml b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/pom.xml new file mode 100644 index 00000000000..04466490367 --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/pom.xml @@ -0,0 +1,99 @@ + + + + + org.wso2.carbon.devicemgt + apimgt-extensions-feature + 5.0.21-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.device.mgt.core.apimgt.analytics.extension.feature + pom + Entgra - APIM Analytics Extension Feature + http://entgra.io + + + + org.wso2.carbon.devicemgt + io.entgra.device.mgt.core.apimgt.analytics.extension + + + + + + + maven-resources-plugin + 2.6 + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + io.entgra.device.mgt.core.apimgt.analytics.extension + ../../../features/etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + org.eclipse.equinox.p2.type.group:true + + + + + org.wso2.carbon.devicemgt:io.entgra.device.mgt.core.apimgt.analytics.extension:${carbon.device.mgt.version} + + + + + + + + + + diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/build.properties b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/build.properties new file mode 100644 index 00000000000..93c4b10701e --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom = true \ No newline at end of file diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_publisher.xml.template b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_publisher.xml.template new file mode 100644 index 00000000000..b103c8d1ac0 --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_publisher.xml.template @@ -0,0 +1,19 @@ + + + + + + + #foreach( $property in $properties ) + ${property.value} + #end + + \ No newline at end of file diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_receiver.xml.template b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_receiver.xml.template new file mode 100644 index 00000000000..c9b90877f07 --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_receiver.xml.template @@ -0,0 +1,20 @@ + + + + + #foreach( $property in $properties ) + ${property.value} + #end + + + + \ No newline at end of file diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_stream.json.template b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_stream.json.template new file mode 100644 index 00000000000..5f0b85ad3b0 --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/iot-analytics-templates/event_stream.json.template @@ -0,0 +1,26 @@ +{ + "streamId": "$name:$version", + "name": "$name", + "version": "$version", + "nickName": "", + "description": "", + "metaData": [ + { + "name": "${metaData.name}", + "type": "${metaData.type}" + } + ], + "correlationData": [], + "payloadData": [ +#foreach( $property in $properties ) + { + "name": "${property.name}", + "type": "${property.value}" + }, +#end + { + "name": "ts", + "type": "LONG" + } + ] +} \ No newline at end of file diff --git a/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/p2.inf b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/p2.inf new file mode 100644 index 00000000000..7c46f1cafc4 --- /dev/null +++ b/features/apimgt-extensions/io.entgra.device.mgt.core.apimgt.analytics.extension.feature/src/main/resources/p2.inf @@ -0,0 +1,2 @@ +instructions.configure = \ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.device.mgt.core.apimgt.analytics.extension_${feature.version}/iot-analytics-templates/,target:${installFolder}/../../resources/,overwrite:true);\ diff --git a/features/apimgt-extensions/pom.xml b/features/apimgt-extensions/pom.xml index 45a706e7a95..ecb17f4c159 100644 --- a/features/apimgt-extensions/pom.xml +++ b/features/apimgt-extensions/pom.xml @@ -37,6 +37,7 @@ org.wso2.carbon.apimgt.webapp.publisher.feature org.wso2.carbon.apimgt.application.extension.feature org.wso2.carbon.apimgt.keymgt.extension.feature + io.entgra.device.mgt.core.apimgt.analytics.extension.feature diff --git a/pom.xml b/pom.xml index 09590375ba1..978a200b234 100644 --- a/pom.xml +++ b/pom.xml @@ -284,6 +284,11 @@ org.wso2.carbon.apimgt.application.extension ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt + io.entgra.device.mgt.core.apimgt.analytics.extension + ${carbon.device.mgt.version} + org.wso2.carbon.devicemgt org.wso2.carbon.identity.jwt.client.extension