fixed on config operation flow

merge-requests/1/head
ayyoob 8 years ago
parent 22a4716443
commit da68f8fac1

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>2.0.74-SNAPSHOT</version>
<version>2.0.75-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -22,7 +22,7 @@
<parent>
<artifactId>device-mgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>2.0.74-SNAPSHOT</version>
<version>2.0.75-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

@ -49,6 +49,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
@SwaggerDefinition(
@ -309,6 +310,70 @@ public interface DeviceAgentService {
value = "deviceId of the device")
@PathParam("deviceId") String deviceId);
@POST
@Path("/events/publish/data/{type}/{deviceId}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
consumes = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Publishing Events data only",
notes = "Publish events received by the device client to the WSO2 Data Analytics Server (DAS) using this API.",
tags = "Device Agent Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:device:publish-event")
})
}
)
@ApiResponses(
value = {
@ApiResponse(code = 200, message = "OK. \n Successfully published the event",
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 = 303,
message = "See Other. \n The source can be retrieved from the URL specified in the location header.",
responseHeaders = {
@ResponseHeader(
name = "Content-Location",
description = "The Source URL of the document.")}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error."),
@ApiResponse(
code = 415,
message = "Unsupported media type. \n The format of the requested entity was not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n " +
"Server error occurred while publishing events.")
})
Response publishEvents(
@ApiParam(
name = "payloadData",
value = "Information of the agent event to be published on DAS.")
@Valid
List<Object> payloadData,
@ApiParam(
name = "type",
value = "name of the device type")
@PathParam("type") String type,
@ApiParam(
name = "deviceId",
value = "deviceId of the device")
@PathParam("deviceId") String deviceId);
@GET
@Path("/pending/operations/{type}/{id}")
@ApiOperation(
@ -367,7 +432,7 @@ public interface DeviceAgentService {
@PathParam("id") String deviceId);
@GET
@Path("/last-pending/operation/{type}/{id}")
@Path("/next-pending/operation/{type}/{id}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
consumes = MediaType.APPLICATION_JSON,

@ -306,6 +306,106 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
}
}
@POST
@Path("/events/publish/data/{type}/{deviceId}")
@Override
public Response publishEvents(@Valid List<Object> payload, @PathParam("type") String type
, @PathParam("deviceId") String deviceId) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
try {
if (payload == null) {
String msg = "invalid payload structure";
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} else {
boolean authorized = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized
(new DeviceIdentifier(type, deviceId));
if (!authorized) {
String msg = "does not have permission to access the device.";
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
}
}
Object metaData[] = new Object[1];
metaData[0] = deviceId;
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
if (eventAttributeList == null) {
String streamName = DeviceMgtAPIUtils.getStreamDefinition(type, tenantDomain);
eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub();
EventStreamDefinitionDto eventStreamDefinitionDto = eventStreamAdminServiceStub.getStreamDefinitionDto(
streamName + ":" + Constants.DEFAULT_STREAM_VERSION);
if (eventStreamDefinitionDto == null) {
return Response.status(Response.Status.BAD_REQUEST).build();
} else {
EventStreamAttributeDto[] eventStreamAttributeDtos = eventStreamDefinitionDto.getPayloadData();
List<Attribute> attributes = new ArrayList<>();
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
}
if (payload.size() != attributes.size()) {
String msg = "payload does not match with the stream definition";
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
eventAttributeList = new EventAttributeList();
eventAttributeList.setList(attributes);
DeviceMgtAPIUtils.getDynamicEventCache().put(type, eventAttributeList);
}
}
int i = 0;
Object[] payloadData = new Object[eventAttributeList.getList().size()];
for (Attribute attribute : eventAttributeList.getList()) {
if (attribute.getType() == AttributeType.INT) {
payloadData[i] = ((Double) payload.get(i)).intValue();
} else if (attribute.getType() == AttributeType.LONG) {
payloadData[i] = ((Double) payload.get(i)).longValue();
} else {
payloadData[i] = payload.get(i);
}
i++;
}
if (DeviceMgtAPIUtils.getEventPublisherService().publishEvent(DeviceMgtAPIUtils.getStreamDefinition(type
, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
, Constants.DEFAULT_STREAM_VERSION, metaData
, null, payloadData)) {
return Response.status(Response.Status.OK).build();
} else {
String msg = "Error occurred while publishing the event.";
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (DataPublisherConfigurationException e) {
String msg = "Error occurred while publishing the event.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (DeviceAccessAuthorizationException e) {
String msg = "Error occurred when checking for authorization";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (AxisFault e) {
log.error("failed to retrieve event definitions for tenantDomain:" + tenantDomain, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (RemoteException e) {
log.error("Failed to connect with the remote services:" + tenantDomain, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (JWTClientException e) {
log.error("Failed to generate jwt token for tenantDomain:" + tenantDomain, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (UserStoreException e) {
log.error("Failed to connect with the user store, tenantDomain: " + tenantDomain, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
if (eventStreamAdminServiceStub != null) {
try {
eventStreamAdminServiceStub.cleanup();
} catch (AxisFault axisFault) {
log.warn("Failed to clean eventStreamAdminServiceStub");
}
}
}
}
@GET
@Path("/pending/operations/{type}/{id}")
public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) {
@ -339,7 +439,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
}
@GET
@Path("/last-pending/operation/{type}/{id}")
@Path("/next-pending/operation/{type}/{id}")
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
try {
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {

@ -37,6 +37,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Path("/admin/device-types")
@Produces(MediaType.APPLICATION_JSON)
@ -44,6 +46,8 @@ import java.util.List;
public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagementAdminService {
private static final Log log = LogFactory.getLog(DeviceTypeManagementAdminServiceImpl.class);
private static final String DEVICETYPE_REGEX_PATTERN = "^[^ /]+$";
private static final Pattern patternMatcher = Pattern.compile(DEVICETYPE_REGEX_PATTERN);
@GET
@Override
@ -68,10 +72,18 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen
String msg = "Device type already available, " + deviceType.getName();
return Response.status(Response.Status.CONFLICT).entity(msg).build();
}
DeviceManagementService httpDeviceTypeManagerService = DeviceMgtAPIUtils.getDeviceTypeGeneratorService()
.populateDeviceManagementService(deviceType.getName(), deviceType.getDeviceTypeMetaDefinition());
DeviceMgtAPIUtils.getDeviceManagementService().registerDeviceType(httpDeviceTypeManagerService);
return Response.status(Response.Status.OK).build();
Matcher matcher = patternMatcher.matcher(deviceType.getName());
if(matcher.find()) {
DeviceManagementService httpDeviceTypeManagerService =
DeviceMgtAPIUtils.getDeviceTypeGeneratorService()
.populateDeviceManagementService(deviceType.getName(),
deviceType.getDeviceTypeMetaDefinition());
DeviceMgtAPIUtils.getDeviceManagementService().registerDeviceType(httpDeviceTypeManagerService);
return Response.status(Response.Status.OK).build();
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("device type name does not match the pattern "
+ DEVICETYPE_REGEX_PATTERN).build();
}
} catch (DeviceManagementException e) {
String msg = "Error occurred at server side while adding a device type.";
log.error(msg, e);

@ -178,6 +178,7 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl {
ois = new ObjectInputStream(bais);
configOperation = (ConfigOperation) ois.readObject();
configOperation.setStatus(status);
configOperation.setId(rs.getInt("OPERATION_ID"));
operations.add(configOperation);
}
} catch (IOException e) {

@ -229,6 +229,10 @@ $(document).ready(function () {
$(errorMsg).text("Unexpected error.");
$(errorMsgWrapper).removeClass("hidden");
}
if (jqXHR.status == 400) {
$(errorMsg).text("Device type name should not contain whitespaces.");
$(errorMsgWrapper).removeClass("hidden");
}
if (jqXHR.status == 409) {
$(errorMsg).text("Device type already exists");

@ -271,11 +271,10 @@
<code>curl -k -X GET {{httpsGateway}}/api/device-mgt/v1.0/device/agent/pending/operations/{{deviceType}}/deviceId
-H 'authorization: Bearer %accessToken%' -H 'content-type: application/json'</code>
</br>
Retrieve last pending operation:
<code>curl -k -X GET {{httpsGateway}}/api/device-mgt/v1.0/device/agent/last-pending/operation/{{deviceType}}/deviceId
Retrieve next pending operation:
<code>curl -k -X GET {{httpsGateway}}/api/device-mgt/v1.0/device/agent/next-pending/operations/{{deviceType}}/deviceId
-H 'authorization: Bearer %accessToken%' -H 'content-type: application/json'</code>
</br>
Update operation:
<code>curl -k -X PUT {{httpsGateway}}/api/device-mgt/v1.0/device/agent/operations/{{deviceType}}/deviceId
-H 'authorization: Bearer %accessToken%' -H 'content-type: application/json' -d '{"id": 1,"status": "COMPLETED", "payload": "this is my response"}'</code>

@ -52,7 +52,7 @@ public class AccessTokenGrantHandler extends AbstractAuthorizationGrantHandler {
public AccessTokenGrantHandler() {
try {
tokenValidator = OAuthValidatorFactory.getValidator();
// tokenValidator = OAuthValidatorFactory.getValidator();
} catch (IllegalArgumentException e) {
log.error("Failed to initialise Authenticator", e);
}

@ -44,14 +44,6 @@ public class OAuthAuthenticatorServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Starting Backend OAuthAuthenticator Framework Bundle");
}
try {
/* Registering BackendOAuthAuthenticator Service */
BundleContext bundleContext = componentContext.getBundleContext();
OauthAuthenticator oAuthAuthenticator = new OauthAuthenticator();
bundleContext.registerService(CarbonServerAuthenticator.class.getName(), oAuthAuthenticator, null);
} catch (Throwable e) {
log.error("Error occurred while initializing the bundle", e);
}
}
@SuppressWarnings("unused")

@ -22,14 +22,14 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>device-mgt-extensions-feature</artifactId>
<version>2.0.74-SNAPSHOT</version>
<version>2.0.75-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.http.feature</artifactId>
<packaging>pom</packaging>
<version>2.0.74-SNAPSHOT</version>
<version>2.0.75-SNAPSHOT</version>
<name>WSO2 Carbon - MQTT Based Push Notification Provider Feature</name>
<url>http://wso2.org</url>
<description>WSO2 Carbon - MQTT Based Push Notification Provider Feature</description>

@ -74,6 +74,7 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
OPERATION_ID INTEGER NOT NULL,
OPERATION_CONFIG BLOB DEFAULT NULL,
ENABLED BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION

@ -80,6 +80,9 @@
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.oauth.extensions:${carbon.device.mgt.version}
</bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.identity.authenticator.backend.oauth:${carbon.device.mgt.version}
</bundleDef>
</bundles>
<importFeatures>
<importFeatureDef>org.wso2.carbon.core.server:${carbon.kernel.version}

Loading…
Cancel
Save