improve device type configuring and event data publishing

try_it
Amalka Subasinghe 2 years ago
parent bf8efa35a7
commit d3ffba8e26

@ -425,5 +425,30 @@
<artifactId>io.entgra.application.mgt.core</artifactId> <artifactId>io.entgra.application.mgt.core</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.keymgt.extension</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.stream.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.receiver.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.publisher.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.output.adapter.rdbms</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -0,0 +1,99 @@
package org.wso2.carbon.device.mgt.jaxrs.beans;
import io.swagger.annotations.ApiModel;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import java.util.List;
@ApiModel(value = "DeviceConfig", description = "Device config")
public class DeviceConfig {
private String clientId;
private String clientSecret;
private String deviceId;
private String type;
private String accessToken;
private String refreshToken;
private String mqttGateway;
private String httpsGateway;
private String httpGateway;
private PlatformConfiguration platformConfiguration;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public String getMqttGateway() {
return mqttGateway;
}
public void setMqttGateway(String mqttGateway) {
this.mqttGateway = mqttGateway;
}
public String getHttpsGateway() {
return httpsGateway;
}
public void setHttpsGateway(String httpsGateway) {
this.httpsGateway = httpsGateway;
}
public String getHttpGateway() {
return httpGateway;
}
public void setHttpGateway(String httpGateway) {
this.httpGateway = httpGateway;
}
public PlatformConfiguration getPlatformConfiguration() {
return platformConfiguration;
}
public void setPlatformConfiguration(PlatformConfiguration platformConfiguration) {
this.platformConfiguration = platformConfiguration;
}
}

@ -20,14 +20,18 @@ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.List;
/** /**
* This hold stats data record * This hold stats data record
*/ */
public class DeviceTypeEvent { public class DeviceTypeEvent {
private String eventName;
private EventAttributeList eventAttributes; private EventAttributeList eventAttributes;
private TransportType transport; private TransportType transport;
private String eventTopicStructure;
@ApiModelProperty(value = "Attributes related to device type event") @ApiModelProperty(value = "Attributes related to device type event")
@JsonProperty("eventAttributes") @JsonProperty("eventAttributes")
public EventAttributeList getEventAttributeList() { public EventAttributeList getEventAttributeList() {
@ -48,5 +52,25 @@ public class DeviceTypeEvent {
public void setTransportType(TransportType transport) { public void setTransportType(TransportType transport) {
this.transport = transport; this.transport = transport;
} }
@ApiModelProperty(value = "event topic structure")
@JsonProperty("eventTopicStructure")
public String getEventTopicStructure() {
return eventTopicStructure;
}
public void setEventTopicStructure(String eventTopicStructure) {
this.eventTopicStructure = eventTopicStructure;
}
@ApiModelProperty(value = "event topic name")
@JsonProperty("eventName")
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
} }

@ -29,6 +29,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List;
@SwaggerDefinition( @SwaggerDefinition(
info = @Info( info = @Info(
@ -69,64 +70,64 @@ import javax.ws.rs.core.Response;
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public interface DeviceEventManagementService { public interface DeviceEventManagementService {
// @POST @POST
// @Path("/{type}") @Path("/{type}")
// @ApiOperation( @ApiOperation(
// produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
// httpMethod = "POST", httpMethod = "POST",
// value = "Adding the Event Type Definition", value = "Adding the Event Type Definition",
// notes = "Add the event definition for a device.", notes = "Add the event definition for a device.",
// tags = "Device Event Management", tags = "Device Event Management",
// extensions = { extensions = {
// @Extension(properties = { @Extension(properties = {
// @ExtensionProperty(name = Constants.SCOPE, value = "perm:device-types:events") @ExtensionProperty(name = Constants.SCOPE, value = "perm:device-types:events")
// }) })
// } }
// ) )
// @ApiResponses( @ApiResponses(
// value = { value = {
// @ApiResponse( @ApiResponse(
// code = 200, code = 200,
// message = "OK. \n Successfully added the event defintion.", message = "OK. \n Successfully added the event defintion.",
// responseHeaders = { responseHeaders = {
// @ResponseHeader( @ResponseHeader(
// name = "Content-Type", name = "Content-Type",
// description = "The content type of the body"), description = "The content type of the body"),
// @ResponseHeader( @ResponseHeader(
// name = "ETag", name = "ETag",
// description = "Entity Tag of the response resource.\n" + description = "Entity Tag of the response resource.\n" +
// "Used by caches, or in conditional requests."), "Used by caches, or in conditional requests."),
// @ResponseHeader( @ResponseHeader(
// name = "Last-Modified", name = "Last-Modified",
// description = description =
// "Date and time the resource was last modified.\n" + "Date and time the resource was last modified.\n" +
// "Used by caches, or in conditional requests."), "Used by caches, or in conditional requests."),
// } }
// ), ),
// @ApiResponse( @ApiResponse(
// code = 400, code = 400,
// message = message =
// "Bad Request. \n"), "Bad Request. \n"),
// @ApiResponse( @ApiResponse(
// code = 406, code = 406,
// message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
// @ApiResponse( @ApiResponse(
// code = 500, code = 500,
// message = "Internal Server Error. \n Server error occurred while fetching the " + message = "Internal Server Error. \n Server error occurred while fetching the " +
// "list of supported device types.", "list of supported device types.",
// response = ErrorResponse.class) response = ErrorResponse.class)
// } }
// ) )
// Response deployDeviceTypeEventDefinition( Response deployDeviceTypeEventDefinition(
// @ApiParam(name = "type", value = "The device type, such as android, ios, and windows.") @ApiParam(name = "type", value = "The device type, such as android, ios, and windows.")
// @PathParam("type")String deviceType, @PathParam("type")String deviceType,
// @ApiParam(name = "skipPersist", value = "Is it required to persist the data or not") @ApiParam(name = "skipPersist", value = "Is it required to persist the data or not")
// @QueryParam("skipPersist") boolean skipPersist, @QueryParam("skipPersist") boolean skipPersist,
// @ApiParam(name = "isSharedWithAllTenants", value = "Should artifacts be available to all tenants") @ApiParam(name = "isSharedWithAllTenants", value = "Should artifacts be available to all tenants")
// @QueryParam("isSharedWithAllTenants") boolean isSharedWithAllTenants, @QueryParam("isSharedWithAllTenants") boolean isSharedWithAllTenants,
// @ApiParam(name = "deviceTypeEvent", value = "Add the data to complete the DeviceTypeEvent object.", @ApiParam(name = "deviceTypeEvents", value = "Add the data to complete the DeviceTypeEvent object.",
// required = true) required = true)
// @Valid DeviceTypeEvent deviceTypeEvent); @Valid List<DeviceTypeEvent> deviceTypeEvent);
@DELETE @DELETE
@Path("/{type}") @Path("/{type}")

@ -1059,6 +1059,82 @@ public interface DeviceManagementService {
@HeaderParam("If-Modified-Since") @HeaderParam("If-Modified-Since")
String ifModifiedSince); String ifModifiedSince);
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{type}/{id}/config")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting the Configuration of a Device",
notes = "Get the configuration of a device by specifying the device type and device identifier.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:details")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully fetched the configuration of the device.",
response = DeviceInfo.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. Empty body because the client already has the latest version" +
" of the requested resource.\n"),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found. \n Location data for the specified device was not found.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n " +
"Server error occurred while retrieving the device details.",
response = ErrorResponse.class)
})
Response getDeviceConfiguration(
@ApiParam(
name = "type",
value = "The device type name, such as ios, android, windows, or fire-alarm.",
required = true)
@PathParam("type")
@Size(max = 45)
String type,
@ApiParam(
name = "id",
value = "The device identifier of the device you want ot get details.",
required = true)
@PathParam("id")
@Size(max = 45)
String id,
@ApiParam(
name = "If-Modified-Since",
value = "Checks if the requested variant was modified, since the specified date-time. \n" +
"Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z. \n" +
"Example: Mon, 05 Jan 2014 15:10:00 +0200",
required = false)
@HeaderParam("If-Modified-Since")
String ifModifiedSince);
//device rename request would looks like follows //device rename request would looks like follows
//POST devices/type/virtual_firealarm/id/us06ww93auzp/rename //POST devices/type/virtual_firealarm/id/us06ww93auzp/rename
@POST @POST

@ -1,12 +1,30 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import edu.emory.mathcs.backport.java.util.Arrays;
import org.apache.axis2.AxisFault; import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Stub; import org.apache.axis2.client.Stub;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.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.Attribute;
import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType;
import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.DeviceTypeEvent; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.DeviceTypeEvent;
@ -15,26 +33,50 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.TransportType;
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceEventManagementService; 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.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; 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.EventPublisherAdminServiceCallbackHandler;
import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub; 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;
import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceCallbackHandler; import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceCallbackHandler;
import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; 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.BasicInputAdapterPropertyDto;
import org.wso2.carbon.event.receiver.stub.types.EventReceiverConfigurationDto; 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; import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub;
import org.wso2.carbon.event.stream.stub.types.EventStreamAttributeDto; import org.wso2.carbon.event.stream.stub.types.EventStreamAttributeDto;
import org.wso2.carbon.event.stream.stub.types.EventStreamDefinitionDto; import org.wso2.carbon.event.stream.stub.types.EventStreamDefinitionDto;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.DELETE; import javax.validation.Valid;
import javax.ws.rs.GET; import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
@ -173,65 +215,73 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
/** /**
* Deploy Event Stream, Receiver, Publisher and Store Configuration. * Deploy Event Stream, Receiver, Publisher and Store Configuration.
*/ */
// @POST @POST
// @Path("/{type}") @Path("/{type}")
// @Override @Override
// public Response deployDeviceTypeEventDefinition(@PathParam("type") String deviceType, public Response deployDeviceTypeEventDefinition(@PathParam("type") String deviceType,
// @QueryParam("skipPersist") boolean skipPersist, @QueryParam("skipPersist") boolean skipPersist,
// @QueryParam("isSharedWithAllTenants") boolean isSharedWithAllTenants, @QueryParam("isSharedWithAllTenants") boolean isSharedWithAllTenants,
// @Valid DeviceTypeEvent deviceTypeEvent) { @Valid List<DeviceTypeEvent> deviceTypeEvents) {
// TransportType transportType = deviceTypeEvent.getTransportType();
// EventAttributeList eventAttributes = deviceTypeEvent.getEventAttributeList();
// String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
// try { try {
// if (eventAttributes == null || eventAttributes.getList() == null || eventAttributes.getList().size() == 0 || for (DeviceTypeEvent deviceTypeEvent : deviceTypeEvents) {
// deviceType == null || transportType == null || TransportType transportType = deviceTypeEvent.getTransportType();
// !DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(deviceType)) { EventAttributeList eventAttributes = deviceTypeEvent.getEventAttributeList();
// String errorMessage = "Invalid Payload"; String eventName = deviceTypeEvent.getEventName();
// log.error(errorMessage);
// return Response.status(Response.Status.BAD_REQUEST).build();
// } if (eventAttributes == null || eventAttributes.getList() == null || eventAttributes.getList().size() == 0 ||
// String streamName = DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain); deviceType == null || transportType == null ||
// String streamNameWithVersion = streamName + ":" + Constants.DEFAULT_STREAM_VERSION; !DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(deviceType)) {
// publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes); String errorMessage = "Invalid Payload";
// publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, isSharedWithAllTenants, deviceType); log.error(errorMessage);
// if (!skipPersist) { return Response.status(Response.Status.BAD_REQUEST).build();
// publishEventStore(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes); }
// } String streamName = DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain, eventName);
// publishWebsocketPublisherDefinition(streamNameWithVersion, deviceType); publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes);
// try {
// PrivilegedCarbonContext.startTenantFlow(); String receiverName = getReceiverName(deviceType, tenantDomain, transportType, eventName);
// PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( publishEventReceivers(streamName, Constants.DEFAULT_STREAM_VERSION, transportType, tenantDomain,
// MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); isSharedWithAllTenants, deviceType, deviceTypeEvent.getEventTopicStructure(), receiverName);
// if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { if (!skipPersist) {
// publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes); String rdbmsPublisherName = getPublisherName(deviceType, tenantDomain, eventName) + "_rdbms_publisher";
// publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, isSharedWithAllTenants, deviceType); publishEventStore(streamName, Constants.DEFAULT_STREAM_VERSION, rdbmsPublisherName);
// } }
// } finally { String wsPublisherName = getPublisherName(deviceType, tenantDomain, eventName) + "_ws_publisher";
// PrivilegedCarbonContext.endTenantFlow(); publishWebsocketPublisherDefinition(streamName, Constants.DEFAULT_STREAM_VERSION, wsPublisherName);
// } try {
// return Response.ok().build(); PrivilegedCarbonContext.startTenantFlow();
// } catch (AxisFault e) { PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
// log.error("Failed to create event definitions for tenantDomain:" + tenantDomain, e); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
// } catch (RemoteException e) { publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes);
// log.error("Failed to connect with the remote services:" + tenantDomain, e); publishEventReceivers(streamName, Constants.DEFAULT_STREAM_VERSION, transportType, tenantDomain,
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); isSharedWithAllTenants, deviceType, deviceTypeEvent.getEventTopicStructure(), receiverName);
// } catch (JWTClientException e) { }
// log.error("Failed to generate jwt token for tenantDomain:" + tenantDomain, e); } finally {
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); PrivilegedCarbonContext.endTenantFlow();
// } catch (UserStoreException e) { }
// log.error("Failed to connect with the user store, tenantDomain: " + tenantDomain, e); }
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.ok().build();
// } catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
// log.error("Failed to access device management service, tenantDomain: " + tenantDomain, e); log.error("Failed to access device management service, tenantDomain: " + tenantDomain, e);
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
// } catch (EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException e) { } catch (MalformedStreamDefinitionException e) {
// log.error("Failed to create event store for, tenantDomain: " + tenantDomain + " deviceType" + deviceType, log.error("Failed while creating stream definition, tenantDomain: " + tenantDomain, e);
// e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
// 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);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (EventReceiverConfigurationException e) {
log.error("Failed while configuring event receiver, tenantDomain: " + tenantDomain, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
/** /**
* Delete device type specific artifacts from DAS. * Delete device type specific artifacts from DAS.
@ -498,158 +548,172 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
// return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); // return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
// } // }
// } // }
private void publishEventReceivers(String streamName, String version, TransportType transportType
, String requestedTenantDomain, boolean isSharedWithAllTenants, String deviceType,
private void publishEventReceivers(String streamNameWithVersion, TransportType transportType String eventTopicStructure, String receiverName) throws EventReceiverConfigurationException {
, String requestedTenantDomain, boolean isSharedWithAllTenants, String deviceType) EventReceiverService eventReceiverService = DeviceMgtAPIUtils.getEventReceiverService();
throws RemoteException, UserStoreException, JWTClientException {
EventReceiverAdminServiceStub receiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub();
try { try {
TransportType transportTypeToBeRemoved = TransportType.HTTP; // TransportType transportTypeToBeRemoved = TransportType.HTTP;
if (transportType == TransportType.HTTP) { // if (transportType == TransportType.HTTP) {
transportTypeToBeRemoved = TransportType.MQTT; // transportTypeToBeRemoved = TransportType.MQTT;
} // }
String eventRecieverNameTobeRemoved = getReceiverName(deviceType, requestedTenantDomain, transportTypeToBeRemoved); // String eventRecieverNameTobeRemoved = getReceiverName(deviceType, requestedTenantDomain, transportTypeToBeRemoved);
EventReceiverConfigurationDto eventReceiverConfigurationDto = receiverAdminServiceStub EventReceiverConfiguration eventReceiverConfiguration =
.getActiveEventReceiverConfiguration(eventRecieverNameTobeRemoved); eventReceiverService.getActiveEventReceiverConfiguration(receiverName);
if (eventReceiverConfigurationDto != null) { if (eventReceiverConfiguration != null) {
EventReceiverAdminServiceCallbackHandler eventReceiverAdminServiceCallbackHandler = eventReceiverService.undeployActiveEventReceiverConfiguration(receiverName);
new EventReceiverAdminServiceCallbackHandler() {
};
receiverAdminServiceStub.startundeployActiveEventReceiverConfiguration(eventRecieverNameTobeRemoved
, eventReceiverAdminServiceCallbackHandler);
} }
String adapterType = OAUTH_MQTT_ADAPTER_TYPE; InputEventAdapterConfiguration inputEventAdapterConfiguration = new InputEventAdapterConfiguration();
BasicInputAdapterPropertyDto basicInputAdapterPropertyDtos[]; Map<String, String> propertyMap = new HashMap<>();
if (transportType == TransportType.MQTT) { if (transportType == TransportType.MQTT) {
basicInputAdapterPropertyDtos = new BasicInputAdapterPropertyDto[3]; inputEventAdapterConfiguration.setType(OAUTH_MQTT_ADAPTER_TYPE);
String topic; String topic;
if (!StringUtils.isEmpty(eventTopicStructure)) {
if (isSharedWithAllTenants) {
topic = eventTopicStructure.replace("${deviceId}", "+")
.replace("${deviceType}", deviceType)
.replace("${tenantDomain}", "+");
} else {
topic = eventTopicStructure.replace("${deviceId}", "+")
.replace("${deviceType}", deviceType)
.replace("${tenantDomain}", requestedTenantDomain);
}
} else {
if (isSharedWithAllTenants) { if (isSharedWithAllTenants) {
topic = "+/" + deviceType + "/+/events"; topic = "+/" + deviceType + "/+/events";
} else { } else {
topic = requestedTenantDomain + "/" + deviceType + "/+/events"; topic = requestedTenantDomain + "/" + deviceType + "/+/events";
} }
basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("topic", topic); }
basicInputAdapterPropertyDtos[1] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_TRANSFORMER_TYPE propertyMap.put("topic", topic);
, MQTT_CONTENT_TRANSFORMER); propertyMap.put(MQTT_CONTENT_TRANSFORMER_TYPE, MQTT_CONTENT_TRANSFORMER);
basicInputAdapterPropertyDtos[2] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_VALIDATOR_TYPE propertyMap.put(MQTT_CONTENT_VALIDATOR_TYPE, MQTT_CONTENT_VALIDATOR);
, MQTT_CONTENT_VALIDATOR);
} else { } else {
adapterType = THRIFT_ADAPTER_TYPE; inputEventAdapterConfiguration.setType(THRIFT_ADAPTER_TYPE);
basicInputAdapterPropertyDtos = new BasicInputAdapterPropertyDto[1]; propertyMap.put("events.duplicated.in.cluster", "false");
basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("events.duplicated.in.cluster", "false");
} }
String eventRecieverName = getReceiverName(deviceType, requestedTenantDomain, transportType); inputEventAdapterConfiguration.setProperties(propertyMap);
if (receiverAdminServiceStub.getActiveEventReceiverConfiguration(eventRecieverName) == null) {
if (eventReceiverService.getActiveEventReceiverConfiguration(receiverName) == null) {
EventReceiverConfiguration configuration = new EventReceiverConfiguration();
configuration.setEventReceiverName(receiverName);
configuration.setToStreamName(streamName);
configuration.setToStreamVersion(version);
configuration.setFromAdapterConfiguration(inputEventAdapterConfiguration);
if (transportType == TransportType.MQTT) { if (transportType == TransportType.MQTT) {
receiverAdminServiceStub.deployJsonEventReceiverConfiguration(eventRecieverName, streamNameWithVersion JSONInputMapping jsonInputMapping = new JSONInputMapping();
, adapterType, null, basicInputAdapterPropertyDtos, false); jsonInputMapping.setCustomMappingEnabled(false);
configuration.setInputMapping(jsonInputMapping);
eventReceiverService.deployEventReceiverConfiguration(configuration);
} else { } else {
receiverAdminServiceStub.deployWso2EventReceiverConfiguration(eventRecieverName, streamNameWithVersion WSO2EventInputMapping wso2EventInputMapping = new WSO2EventInputMapping();
, adapterType, null, null, null, basicInputAdapterPropertyDtos, false, null); wso2EventInputMapping.setCustomMappingEnabled(false);
configuration.setInputMapping(wso2EventInputMapping);
eventReceiverService.deployEventReceiverConfiguration(configuration);
} }
} }
} finally { } catch (EventReceiverConfigurationException e) {
cleanup(receiverAdminServiceStub); log.error("Error while publishing event receiver" , e);
throw new EventReceiverConfigurationException(e);
} }
} }
private void publishStreamDefinitons(String streamName, String version, String deviceType private void publishStreamDefinitons(String streamName, String version, EventAttributeList eventAttributes)
, EventAttributeList eventAttributes) throws MalformedStreamDefinitionException, EventStreamConfigurationException {
throws RemoteException, UserStoreException, JWTClientException { EventStreamService eventStreamService = DeviceMgtAPIUtils.getEventStreamService();
EventStreamAdminServiceStub eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub();
try { try {
EventStreamDefinitionDto eventStreamDefinitionDto = new EventStreamDefinitionDto(); StreamDefinition streamDefinition = new StreamDefinition(streamName, version);
eventStreamDefinitionDto.setName(streamName);
eventStreamDefinitionDto.setVersion(version); List<org.wso2.carbon.databridge.commons.Attribute> payloadDataAttributes = new ArrayList<>();
EventStreamAttributeDto eventStreamAttributeDtos[] =
new EventStreamAttributeDto[eventAttributes.getList().size()];
EventStreamAttributeDto metaStreamAttributeDtos[] =
new EventStreamAttributeDto[1];
int i = 0;
for (Attribute attribute : eventAttributes.getList()) { for (Attribute attribute : eventAttributes.getList()) {
EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); payloadDataAttributes.add(new org.wso2.carbon.databridge.commons.Attribute(attribute.getName(),
eventStreamAttributeDto.setAttributeName(attribute.getName()); org.wso2.carbon.databridge.commons.AttributeType.valueOf(attribute.getType().name())));
eventStreamAttributeDto.setAttributeType(attribute.getType().toString()); }
eventStreamAttributeDtos[i] = eventStreamAttributeDto; streamDefinition.setPayloadData(payloadDataAttributes);
i++;
} List<org.wso2.carbon.databridge.commons.Attribute> metaDataAttributes = new ArrayList<>();
metaDataAttributes.add(new org.wso2.carbon.databridge.commons.Attribute(DEFAULT_DEVICE_ID_ATTRIBUTE,
EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); org.wso2.carbon.databridge.commons.AttributeType.STRING));
eventStreamAttributeDto.setAttributeName(DEFAULT_DEVICE_ID_ATTRIBUTE); streamDefinition.setMetaData(metaDataAttributes);
eventStreamAttributeDto.setAttributeType(AttributeType.STRING.toString());
metaStreamAttributeDtos[0] = eventStreamAttributeDto; if (eventStreamService.getStreamDefinition(streamDefinition.getStreamId()) != null) {
eventStreamDefinitionDto.setPayloadData(eventStreamAttributeDtos); eventStreamService.removeEventStreamDefinition(streamName, version);
eventStreamDefinitionDto.setMetaData(metaStreamAttributeDtos); eventStreamService.addEventStreamDefinition(streamDefinition);
String streamId = streamName + ":" + version;
if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamId) != null) {
eventStreamAdminServiceStub.editEventStreamDefinitionAsDto(eventStreamDefinitionDto, streamId);
} else { } else {
eventStreamAdminServiceStub.addEventStreamDefinitionAsDto(eventStreamDefinitionDto); eventStreamService.addEventStreamDefinition(streamDefinition);
} }
} finally {
cleanup(eventStreamAdminServiceStub); } catch (MalformedStreamDefinitionException e) {
log.error("Error while initializing stream definition " , e);
throw new MalformedStreamDefinitionException(e);
} catch (EventStreamConfigurationException e) {
log.error("Error while configuring stream definition " , e);
throw new EventStreamConfigurationException(e);
} }
} }
/*
// private void publishEventStore(String streamName, String version, EventAttributeList eventAttributes) */
// throws RemoteException, UserStoreException, JWTClientException,
// EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException { private void publishEventStore(String streamName, String version, String publisherName)
// EventStreamPersistenceAdminServiceStub eventStreamPersistenceAdminServiceStub = throws EventPublisherConfigurationException {
// DeviceMgtAPIUtils.getEventStreamPersistenceAdminServiceStub();
// try { EventPublisherService eventPublisherService = DeviceMgtAPIUtils.getEventPublisherService();
// AnalyticsTable analyticsTable = new AnalyticsTable();
// analyticsTable.setRecordStoreName(DEFAULT_EVENT_STORE_NAME);
// analyticsTable.setStreamVersion(version);
// analyticsTable.setTableName(streamName);
// analyticsTable.setMergeSchema(false);
// analyticsTable.setPersist(true);
// AnalyticsTableRecord analyticsTableRecords[] = new AnalyticsTableRecord[eventAttributes.getList().size() + 1];
// int i = 0;
// for (Attribute attribute : eventAttributes.getList()) {
// AnalyticsTableRecord analyticsTableRecord = new AnalyticsTableRecord();
// analyticsTableRecord.setColumnName(attribute.getName());
// analyticsTableRecord.setColumnType(attribute.getType().toString().toUpperCase());
// analyticsTableRecord.setFacet(false);
// analyticsTableRecord.setIndexed(false);
// analyticsTableRecord.setPersist(true);
// analyticsTableRecord.setPrimaryKey(false);
// analyticsTableRecord.setScoreParam(false);
// analyticsTableRecords[i] = analyticsTableRecord;
// i++;
// }
// AnalyticsTableRecord analyticsTableRecord = new AnalyticsTableRecord();
// analyticsTableRecord.setColumnName(DEFAULT_META_DEVICE_ID_ATTRIBUTE);
// analyticsTableRecord.setColumnType(AttributeType.STRING.toString().toUpperCase());
// analyticsTableRecord.setFacet(false);
// analyticsTableRecord.setIndexed(true);
// analyticsTableRecord.setPersist(true);
// analyticsTableRecord.setPrimaryKey(false);
// analyticsTableRecord.setScoreParam(false);
// analyticsTableRecords[i] = analyticsTableRecord;
// analyticsTable.setAnalyticsTableRecords(analyticsTableRecords);
// eventStreamPersistenceAdminServiceStub.addAnalyticsTable(analyticsTable);
// } finally {
// cleanup(eventStreamPersistenceAdminServiceStub);
// }
// }
private void publishWebsocketPublisherDefinition(String streamNameWithVersion, String deviceType)
throws RemoteException, UserStoreException, JWTClientException {
EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils
.getEventPublisherAdminServiceStub();
try { try {
String eventPublisherName = deviceType.trim().replace(" ", "_") + "_websocket_publisher"; if (eventPublisherService.getActiveEventPublisherConfiguration(publisherName) == null) {
if (eventPublisherAdminServiceStub.getActiveEventPublisherConfiguration(eventPublisherName) == null) { EventPublisherConfiguration configuration = new EventPublisherConfiguration();
eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(eventPublisherName configuration.setEventPublisherName(publisherName);
, streamNameWithVersion, DEFAULT_WEBSOCKET_PUBLISHER_ADAPTER_TYPE, null, null configuration.setFromStreamName(streamName);
, null, false); configuration.setFromStreamVersion(version);
MapOutputMapping mapOutputMapping = new MapOutputMapping();
mapOutputMapping.setCustomMappingEnabled(false);
configuration.setOutputMapping(mapOutputMapping);
OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration();
outputEventAdapterConfiguration.setType("rdbms");
Map<String, String> staticProperties = new HashMap<>();
staticProperties.put("datasource.name", "EVENT_DB");
staticProperties.put("execution.mode", "insert");
staticProperties.put("table.name", "table_" + publisherName.replace(".", ""));
outputEventAdapterConfiguration.setStaticProperties(staticProperties);
configuration.setProcessEnabled(true);
configuration.setToAdapterConfiguration(outputEventAdapterConfiguration);
eventPublisherService.deployEventPublisherConfiguration(configuration);
}
} catch (EventPublisherConfigurationException e) {
log.error("Error while publishing to rdbms store" , e);
throw new EventPublisherConfigurationException(e);
}
}
private void publishWebsocketPublisherDefinition(String streamName, String version, String publisherName)
throws EventPublisherConfigurationException {
EventPublisherService eventPublisherService = DeviceMgtAPIUtils.getEventPublisherService();
try {
if (eventPublisherService.getActiveEventPublisherConfiguration(publisherName) == null) {
EventPublisherConfiguration configuration = new EventPublisherConfiguration();
configuration.setEventPublisherName(publisherName);
configuration.setFromStreamName(streamName);
configuration.setFromStreamVersion(version);
JSONOutputMapping jsonOutputMapping = new JSONOutputMapping();
jsonOutputMapping.setCustomMappingEnabled(false);
configuration.setOutputMapping(jsonOutputMapping);
OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration();
outputEventAdapterConfiguration.setType("websocket-local");
configuration.setToAdapterConfiguration(outputEventAdapterConfiguration);
eventPublisherService.deployEventPublisherConfiguration(configuration);
} }
} finally { } catch (EventPublisherConfigurationException e) {
cleanup(eventPublisherAdminServiceStub); log.error("Error while publishing to websocket-local" , e);
throw new EventPublisherConfigurationException(e);
} }
} }
private BasicInputAdapterPropertyDto getBasicInputAdapterPropertyDto(String key, String value) { private BasicInputAdapterPropertyDto getBasicInputAdapterPropertyDto(String key, String value) {
@ -667,6 +731,13 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
return deviceType.replace(" ", "_").trim() + "-" + tenantDomain + "-" + transportType.toString() + "-receiver"; return deviceType.replace(" ", "_").trim() + "-" + tenantDomain + "-" + transportType.toString() + "-receiver";
} }
private String getReceiverName(String deviceType, String tenantDomain, TransportType transportType, String eventName) {
return eventName + "-" + getReceiverName(deviceType, tenantDomain, transportType);
}
private String getPublisherName(String tenantDomain, String deviceType, String eventName) {
return eventName + "_" + tenantDomain.replace(".", "_") + "_" + deviceType;
}
private void cleanup(Stub stub) { private void cleanup(Stub stub) {
if (stub != null) { if (stub != null) {

@ -37,33 +37,26 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.application.mgt.common.ApplicationInstallResponse;
import io.entgra.application.mgt.common.SubscriptionType;
import io.entgra.application.mgt.common.exception.SubscriptionManagementException;
import io.entgra.application.mgt.common.services.ApplicationManager; import io.entgra.application.mgt.common.services.ApplicationManager;
import io.entgra.application.mgt.common.services.SubscriptionManager;
import io.entgra.application.mgt.core.util.HelperUtil;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.wso2.carbon.apimgt.keymgt.extension.DCRResponse;
import org.wso2.carbon.apimgt.keymgt.extension.TokenRequest;
import org.wso2.carbon.apimgt.keymgt.extension.TokenResponse;
import org.wso2.carbon.apimgt.keymgt.extension.exception.KeyMgtException;
import org.wso2.carbon.apimgt.keymgt.extension.service.KeyMgtService;
import org.wso2.carbon.apimgt.keymgt.extension.service.KeyMgtServiceImpl;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.application.mgt.common.ApplicationInstallResponse; import org.wso2.carbon.device.mgt.common.*;
import io.entgra.application.mgt.common.SubscriptionType;
import io.entgra.application.mgt.common.exception.SubscriptionManagementException;
import io.entgra.application.mgt.common.services.SubscriptionManager;
import io.entgra.application.mgt.core.util.HelperUtil;
import org.wso2.carbon.device.mgt.common.DeviceFilters;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.OperationLogFilters;
import org.wso2.carbon.device.mgt.common.MDMAppConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo;
import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
@ -72,12 +65,8 @@ import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException; import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.UnAuthorizedException; import org.wso2.carbon.device.mgt.common.exceptions.*;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
@ -103,19 +92,10 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.*;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceCompliance;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationList;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean;
import org.wso2.carbon.device.mgt.jaxrs.beans.ComplianceDeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationRequest;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationUninstallation;
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
@ -128,29 +108,17 @@ import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerSer
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.validation.Valid; import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.DELETE; import javax.ws.rs.*;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -887,6 +855,100 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
return Response.status(Response.Status.OK).entity(deviceInfo).build(); return Response.status(Response.Status.OK).entity(deviceInfo).build();
}
@GET
@Path("/{type}/{id}/config")
@Override
public Response getDeviceConfiguration(
@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id,
@HeaderParam("If-Modified-Since") String ifModifiedSince) {
DeviceConfig deviceConfig = new DeviceConfig();
deviceConfig.setDeviceId(id);
deviceConfig.setType(type);
// find token validity time
DeviceManagementProviderService deviceManagementProviderService =
DeviceMgtAPIUtils.getDeviceManagementService();
int validityTime = 3600;
List<String> mqttTopicStructure = new ArrayList<>();
try {
DeviceType deviceType = deviceManagementProviderService.getDeviceType(type);
if (deviceType != null) {
if (deviceType.getDeviceTypeMetaDefinition().isLongLivedToken()) {
validityTime = Integer.MAX_VALUE;
}
mqttTopicStructure = deviceType.getDeviceTypeMetaDefinition().getMqttTopicStructures();
} else {
String msg = "Device not found, device id : " + id + ", device type : " + type;
log.error(msg);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving device, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
String applicationName = type.replace(" ", "").replace("_", "")
+ "_" + tenantDomain;
KeyMgtService keyMgtService = new KeyMgtServiceImpl();
try {
DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username,
"client_credentials", null, new String[] {"device_management"}, false, validityTime);
deviceConfig.setClientId(dcrResponse.getClientId());
deviceConfig.setClientSecret(dcrResponse.getClientSecret());
StringBuilder scopes = new StringBuilder("device_" + type.replace(" ", "")
.replace("_", "") + "_" + id);
for (String topic : mqttTopicStructure) {
if (topic.contains("<deviceId>")) {
topic = topic.replace("<deviceId>", id);
}
topic = topic.replace("/",":");
scopes.append(" perm:topic:sub:".concat(topic));
scopes.append(" perm:topic:pub:".concat(topic));
}
TokenRequest tokenRequest = new TokenRequest(dcrResponse.getClientId(), dcrResponse.getClientSecret(),
null, scopes.toString(), "client_credentials", null,
null, null, null, validityTime);
TokenResponse tokenResponse = keyMgtService.generateAccessToken(tokenRequest);
deviceConfig.setAccessToken(tokenResponse.getAccessToken());
deviceConfig.setRefreshToken(tokenResponse.getRefreshToken());
try {
deviceConfig.setPlatformConfiguration(deviceManagementProviderService.getConfiguration(type));
} catch (DeviceManagementException e) {
String msg = "Error occurred while reading platform configurations token, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
deviceConfig.setMqttGateway("tcp://" + System.getProperty("mqtt.broker.host") + ":" + System.getProperty("mqtt.broker.port"));
deviceConfig.setHttpGateway("http://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.http.port"));
deviceConfig.setHttpsGateway("https://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.https.port"));
} catch (KeyMgtException e) {
String msg = "Error occurred while creating oauth application, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (org.wso2.carbon.apimgt.keymgt.extension.exception.BadRequestException e) {
String msg = "Error occurred while generating token, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
return Response.status(Response.Status.OK).entity(deviceConfig).build();
} }
@GET @GET

@ -50,15 +50,12 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub; import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub;
import org.wso2.carbon.authenticator.stub.AuthenticationAdminStub;
import org.wso2.carbon.base.ServerConfiguration; import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.util.Utils; import org.wso2.carbon.core.util.Utils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
@ -95,8 +92,11 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventAttributeList;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub;
import org.wso2.carbon.event.publisher.core.EventPublisherService;
import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub; import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub;
import org.wso2.carbon.event.receiver.core.EventReceiverService;
import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub;
import org.wso2.carbon.event.stream.core.EventStreamService;
import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub; import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub;
import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO; import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
@ -111,11 +111,7 @@ import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.api.AuthorizationManager; import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager; import org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UIPermissionNode;
@ -128,11 +124,7 @@ import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.KeyManagementException; import java.security.*;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
@ -172,7 +164,7 @@ public class DeviceMgtAPIUtils {
private static KeyStore trustStore; private static KeyStore trustStore;
private static char[] keyStorePassword; private static char[] keyStorePassword;
// private static IntegrationClientService integrationClientService; // private static IntegrationClientService integrationClientService;
private static MetadataManagementService metadataManagementService; private static MetadataManagementService metadataManagementService;
private static OTPManagementService otpManagementService; private static OTPManagementService otpManagementService;
@ -581,6 +573,36 @@ public class DeviceMgtAPIUtils {
return geoService; return geoService;
} }
public static EventStreamService getEventStreamService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventStreamService
eventStreamService = (EventStreamService) ctx.getOSGiService(EventStreamService.class, null);
if (eventStreamService == null) {
throw new IllegalStateException("Event Stream Service has not been initialized.");
}
return eventStreamService;
}
public static EventReceiverService getEventReceiverService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventReceiverService
eventReceiverService = (EventReceiverService) ctx.getOSGiService(EventReceiverService.class, null);
if (eventReceiverService == null) {
throw new IllegalStateException("Event Receiver Service has not been initialized.");
}
return eventReceiverService;
}
public static EventPublisherService getEventPublisherService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventPublisherService
eventPublisherService = (EventPublisherService) ctx.getOSGiService(EventPublisherService.class, null);
if (eventPublisherService == null) {
throw new IllegalStateException("Event Receiver Service has not been initialized.");
}
return eventPublisherService;
}
public static AnalyticsDataAPI getAnalyticsDataAPI() { public static AnalyticsDataAPI getAnalyticsDataAPI() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
AnalyticsDataAPI analyticsDataAPI = AnalyticsDataAPI analyticsDataAPI =
@ -642,10 +664,13 @@ public class DeviceMgtAPIUtils {
// return eventsPublisherService; // return eventsPublisherService;
// } // }
public static String getStreamDefinition(String deviceType, String tenantDomain, String eventName) {
return getStreamDefinition(deviceType, tenantDomain) + "." + eventName;
}
public static String getStreamDefinition(String deviceType, String tenantDomain) { public static String getStreamDefinition(String deviceType, String tenantDomain) {
return STREAM_DEFINITION_PREFIX + tenantDomain + "." + deviceType.replace(" ", "."); return STREAM_DEFINITION_PREFIX + tenantDomain + "." + deviceType.replace(" ", ".");
} }
public static EventStreamAdminServiceStub getEventStreamAdminServiceStub() public static EventStreamAdminServiceStub getEventStreamAdminServiceStub()
throws AxisFault, UserStoreException, JWTClientException { throws AxisFault, UserStoreException, JWTClientException {
EventStreamAdminServiceStub eventStreamAdminServiceStub = new EventStreamAdminServiceStub( EventStreamAdminServiceStub eventStreamAdminServiceStub = new EventStreamAdminServiceStub(

@ -5,6 +5,7 @@ import org.wso2.carbon.device.mgt.common.InitialOperationConfig;
import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class DeviceTypeMetaDefinition { public class DeviceTypeMetaDefinition {
@ -19,6 +20,10 @@ public class DeviceTypeMetaDefinition {
private String description; private String description;
private boolean isSharedWithAllTenants; private boolean isSharedWithAllTenants;
private List<String> mqttTopicStructures;
private boolean longLivedToken = false;
public String getDescription() { public String getDescription() {
return description; return description;
} }
@ -83,4 +88,20 @@ public class DeviceTypeMetaDefinition {
public void setSharedWithAllTenants(boolean sharedWithAllTenants) { public void setSharedWithAllTenants(boolean sharedWithAllTenants) {
isSharedWithAllTenants = sharedWithAllTenants; isSharedWithAllTenants = sharedWithAllTenants;
} }
public List<String> getMqttTopicStructures() {
return mqttTopicStructures;
}
public void setMqttTopicStructures(List<String> mqttTopicStructures) {
this.mqttTopicStructures = mqttTopicStructures;
}
public boolean isLongLivedToken() {
return longLivedToken;
}
public void setLongLivedToken(boolean longLivedToken) {
this.longLivedToken = longLivedToken;
}
} }

@ -351,6 +351,11 @@
<artifactId>org.wso2.carbon.apimgt.keymgt.extension.api</artifactId> <artifactId>org.wso2.carbon.apimgt.keymgt.extension.api</artifactId>
<version>${carbon.device.mgt.version}</version> <version>${carbon.device.mgt.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.keymgt.extension</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<!-- Device Management dependencies --> <!-- Device Management dependencies -->
<!-- Task Management dependencies --> <!-- Task Management dependencies -->
@ -1446,6 +1451,31 @@
<artifactId>org.wso2.carbon.databridge.core</artifactId> <artifactId>org.wso2.carbon.databridge.core</artifactId>
<version>${carbon.analytics.common.version}</version> <version>${carbon.analytics.common.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.stream.core</artifactId>
<version>${carbon.analytics.common.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.receiver.core</artifactId>
<version>${carbon.analytics.common.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.publisher.core</artifactId>
<version>${carbon.analytics.common.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.core</artifactId>
<version>${carbon.analytics.common.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.output.adapter.rdbms</artifactId>
<version>${carbon.analytics.common.version}</version>
</dependency>
<!--<dependency>--> <!--<dependency>-->
<!--<groupId>org.wso2.carbon.commons</groupId>--> <!--<groupId>org.wso2.carbon.commons</groupId>-->
<!--<artifactId>org.wso2.carbon.databridge.commons</artifactId>--> <!--<artifactId>org.wso2.carbon.databridge.commons</artifactId>-->
@ -2074,8 +2104,8 @@
<carbon.api.mgt.version.range>[9.0.0, 10.0.0)</carbon.api.mgt.version.range> <carbon.api.mgt.version.range>[9.0.0, 10.0.0)</carbon.api.mgt.version.range>
<!-- Carbon Analytics --> <!-- Carbon Analytics -->
<carbon.analytics.common.version>5.1.37</carbon.analytics.common.version> <carbon.analytics.common.version>5.2.34</carbon.analytics.common.version>
<carbon.analytics.common.version.range>[5.1.3,6.0.0)</carbon.analytics.common.version.range> <carbon.analytics.common.version.range>[5.2.34,6.0.0)</carbon.analytics.common.version.range>
<carbon.analytics.version>1.3.25</carbon.analytics.version> <carbon.analytics.version>1.3.25</carbon.analytics.version>
<carbon.analytics.version.range>[1.3.0,2.0.0)</carbon.analytics.version.range> <carbon.analytics.version.range>[1.3.0,2.0.0)</carbon.analytics.version.range>
<carbon.event-processing.version>2.1.23</carbon.event-processing.version> <carbon.event-processing.version>2.1.23</carbon.event-processing.version>

Loading…
Cancel
Save