Change device type create api, to enable device type sharing with all tenants

To make use of this change pass isSharedWithAllTenants as true to the api by attaching to device type meta data by admin user
feature/appm-store/pbac
Yohan Avishke 5 years ago committed by Charitha Goonetilleke
parent c34811f0e7
commit af47d5f73a

@ -120,6 +120,8 @@ public interface DeviceEventManagementService {
@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")
@QueryParam("isSharedWithAllTenants") boolean isSharedWithAllTenants,
@ApiParam(name = "deviceTypeEvent", value = "Add the data to complete the DeviceTypeEvent object.", @ApiParam(name = "deviceTypeEvent", value = "Add the data to complete the DeviceTypeEvent object.",
required = true) required = true)
@Valid DeviceTypeEvent deviceTypeEvent); @Valid DeviceTypeEvent deviceTypeEvent);

@ -246,6 +246,10 @@ public interface DeviceTypeManagementAdminService {
code = 401, code = 401,
message = "Unauthorized.\n The unauthorized access to the requested resource.", message = "Unauthorized.\n The unauthorized access to the requested resource.",
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse(
code = 403,
message = "Forbidden.\n The resource is unavailable for current tenant.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found.\n The specified device does not exist", message = "Not Found.\n The specified device does not exist",

@ -200,6 +200,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
@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,
@Valid DeviceTypeEvent deviceTypeEvent) { @Valid DeviceTypeEvent deviceTypeEvent) {
TransportType transportType = deviceTypeEvent.getTransportType(); TransportType transportType = deviceTypeEvent.getTransportType();
EventAttributeList eventAttributes = deviceTypeEvent.getEventAttributeList(); EventAttributeList eventAttributes = deviceTypeEvent.getEventAttributeList();
@ -215,7 +216,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
String streamName = DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain); String streamName = DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain);
String streamNameWithVersion = streamName + ":" + Constants.DEFAULT_STREAM_VERSION; String streamNameWithVersion = streamName + ":" + Constants.DEFAULT_STREAM_VERSION;
publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes); publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes);
publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, deviceType); publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, isSharedWithAllTenants, deviceType);
if (!skipPersist) { if (!skipPersist) {
publishEventStore(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes); publishEventStore(streamName, Constants.DEFAULT_STREAM_VERSION, eventAttributes);
} }
@ -226,7 +227,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes); publishStreamDefinitons(streamName, Constants.DEFAULT_STREAM_VERSION, deviceType, eventAttributes);
publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, deviceType); publishEventReceivers(streamNameWithVersion, transportType, tenantDomain, isSharedWithAllTenants, deviceType);
} }
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
@ -520,7 +521,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
private void publishEventReceivers(String streamNameWithVersion, TransportType transportType private void publishEventReceivers(String streamNameWithVersion, TransportType transportType
, String requestedTenantDomain, String deviceType) , String requestedTenantDomain, boolean isSharedWithAllTenants, String deviceType)
throws RemoteException, UserStoreException, JWTClientException { throws RemoteException, UserStoreException, JWTClientException {
EventReceiverAdminServiceStub receiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub(); EventReceiverAdminServiceStub receiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub();
try { try {
@ -543,8 +544,13 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
BasicInputAdapterPropertyDto basicInputAdapterPropertyDtos[]; BasicInputAdapterPropertyDto basicInputAdapterPropertyDtos[];
if (transportType == TransportType.MQTT) { if (transportType == TransportType.MQTT) {
basicInputAdapterPropertyDtos = new BasicInputAdapterPropertyDto[3]; basicInputAdapterPropertyDtos = new BasicInputAdapterPropertyDto[3];
basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("topic", requestedTenantDomain String topic;
+ "/" + deviceType + "/+/events"); if (isSharedWithAllTenants) {
topic = "+/" + deviceType + "/+/events";
} else {
topic = requestedTenantDomain + "/" + deviceType + "/+/events";
}
basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("topic", topic);
basicInputAdapterPropertyDtos[1] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_TRANSFORMER_TYPE basicInputAdapterPropertyDtos[1] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_TRANSFORMER_TYPE
, MQTT_CONTENT_TRANSFORMER); , MQTT_CONTENT_TRANSFORMER);
basicInputAdapterPropertyDtos[2] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_VALIDATOR_TYPE basicInputAdapterPropertyDtos[2] = getBasicInputAdapterPropertyDto(MQTT_CONTENT_VALIDATOR_TYPE

@ -38,9 +38,12 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin;
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.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -110,7 +113,16 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen
@POST @POST
public Response addDeviceType(DeviceType deviceType) { public Response addDeviceType(DeviceType deviceType) {
if (deviceType != null && deviceType.getDeviceTypeMetaDefinition() != null) { if (deviceType != null && deviceType.getDeviceTypeMetaDefinition() != null) {
DeviceTypeMetaDefinition deviceTypeMetaDefinition = deviceType.getDeviceTypeMetaDefinition();
try { try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
if (deviceTypeMetaDefinition.isSharedWithAllTenants() &&
!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
String msg = "Invalid request, device type can only be shared with all the tenants " +
"only if the request is sent by the super tenant";
log.error(msg);
return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
}
if (DeviceMgtAPIUtils.getDeviceManagementService().getDeviceType(deviceType.getName()) != null) { if (DeviceMgtAPIUtils.getDeviceManagementService().getDeviceType(deviceType.getName()) != null) {
String msg = "Device type already available, " + deviceType.getName(); String msg = "Device type already available, " + deviceType.getName();
return Response.status(Response.Status.CONFLICT).entity(msg).build(); return Response.status(Response.Status.CONFLICT).entity(msg).build();

@ -47,6 +47,9 @@ import org.testng.IObjectFactory;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.ObjectFactory; import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -66,8 +69,9 @@ import static org.mockito.MockitoAnnotations.initMocks;
* This class holds the unit tests for the class {@link DeviceTypeManagementAdminService} * This class holds the unit tests for the class {@link DeviceTypeManagementAdminService}
*/ */
@PowerMockIgnore({"javax.ws.rs.*", "org.apache.log4j.*"}) @PowerMockIgnore({"javax.ws.rs.*", "org.apache.log4j.*"})
@SuppressStaticInitializationFor({"org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils"}) @SuppressStaticInitializationFor({"org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils",
@PrepareForTest({DeviceMgtAPIUtils.class, DeviceManagementProviderService.class}) "org.wso2.carbon.context.PrivilegedCarbonContext"})
@PrepareForTest({DeviceMgtAPIUtils.class, DeviceManagementProviderService.class, CarbonContext.class})
public class DeviceTypeManagementAdminServiceTest { public class DeviceTypeManagementAdminServiceTest {
private static final Log log = LogFactory.getLog(DeviceTypeManagementAdminService.class); private static final Log log = LogFactory.getLog(DeviceTypeManagementAdminService.class);
@ -77,6 +81,7 @@ public class DeviceTypeManagementAdminServiceTest {
private static final int TEST_DEVICE_TYPE_ID = 12345; private static final int TEST_DEVICE_TYPE_ID = 12345;
private static final int TEST_DEVICE_TYPE_ID_1 = 123452; private static final int TEST_DEVICE_TYPE_ID_1 = 123452;
private static final int TEST_DEVICE_TYPE_ID_2 = 121233452; private static final int TEST_DEVICE_TYPE_ID_2 = 121233452;
private PrivilegedCarbonContext context;
private DeviceTypeManagementAdminService deviceTypeManagementAdminService; private DeviceTypeManagementAdminService deviceTypeManagementAdminService;
private DeviceManagementProviderService deviceManagementProviderService; private DeviceManagementProviderService deviceManagementProviderService;
private DeviceTypeGeneratorService deviceTypeGeneratorService; private DeviceTypeGeneratorService deviceTypeGeneratorService;
@ -94,6 +99,8 @@ public class DeviceTypeManagementAdminServiceTest {
.mock(DeviceManagementProviderServiceImpl.class, Mockito.RETURNS_MOCKS); .mock(DeviceManagementProviderServiceImpl.class, Mockito.RETURNS_MOCKS);
this.deviceTypeGeneratorService = Mockito.mock(DeviceTypeGeneratorServiceImpl.class, Mockito.RETURNS_MOCKS); this.deviceTypeGeneratorService = Mockito.mock(DeviceTypeGeneratorServiceImpl.class, Mockito.RETURNS_MOCKS);
this.deviceTypeManagementAdminService = new DeviceTypeManagementAdminServiceImpl(); this.deviceTypeManagementAdminService = new DeviceTypeManagementAdminServiceImpl();
context = Mockito.mock(PrivilegedCarbonContext.class);
Mockito.doReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME).when(context).getTenantDomain();
} }
@Test(description = "Test get all the device types.") @Test(description = "Test get all the device types.")
@ -122,6 +129,8 @@ public class DeviceTypeManagementAdminServiceTest {
public void testAddDeviceTypeWithExistingName() throws DeviceManagementException { public void testAddDeviceTypeWithExistingName() throws DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(context);
DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE_1, TEST_DEVICE_TYPE_ID_1); DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE_1, TEST_DEVICE_TYPE_ID_1);
Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType); Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType);
Assert.assertNotNull(response, "The response should not be null"); Assert.assertNotNull(response, "The response should not be null");
@ -133,6 +142,8 @@ public class DeviceTypeManagementAdminServiceTest {
public void testAddDeviceTypeWithUnqualifiedName() throws DeviceManagementException { public void testAddDeviceTypeWithUnqualifiedName() throws DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(context);
Mockito.when(deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null); Mockito.when(deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null);
DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE_2, TEST_DEVICE_TYPE_ID_2); DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE_2, TEST_DEVICE_TYPE_ID_2);
Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType); Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType);
@ -148,6 +159,8 @@ public class DeviceTypeManagementAdminServiceTest {
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceTypeGeneratorService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceTypeGeneratorService"))
.toReturn(this.deviceTypeGeneratorService); .toReturn(this.deviceTypeGeneratorService);
PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(context);
Mockito.when(deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null); Mockito.when(deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null);
DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE, TEST_DEVICE_TYPE_ID); DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE, TEST_DEVICE_TYPE_ID);
Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType); Response response = this.deviceTypeManagementAdminService.addDeviceType(deviceType);
@ -171,6 +184,8 @@ public class DeviceTypeManagementAdminServiceTest {
public void testAddDeviceTypeWithException() throws DeviceManagementException { public void testAddDeviceTypeWithException() throws DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(context);
Mockito.when(this.deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenThrow(new Mockito.when(this.deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenThrow(new
DeviceManagementException()); DeviceManagementException());
DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE, TEST_DEVICE_TYPE_ID); DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE, TEST_DEVICE_TYPE_ID);

@ -18,6 +18,7 @@ public class DeviceTypeMetaDefinition {
private InitialOperationConfig initialOperationConfig; private InitialOperationConfig initialOperationConfig;
private License license; private License license;
private String description; private String description;
private boolean isSharedWithAllTenants;
public String getDescription() { public String getDescription() {
return description; return description;
@ -83,4 +84,12 @@ public class DeviceTypeMetaDefinition {
public void setLicense(License license) { public void setLicense(License license) {
this.license = license; this.license = license;
} }
public boolean isSharedWithAllTenants() {
return isSharedWithAllTenants;
}
public void setSharedWithAllTenants(boolean sharedWithAllTenants) {
isSharedWithAllTenants = sharedWithAllTenants;
}
} }

@ -112,7 +112,7 @@ public class HTTPDeviceTypeManagerService extends DeviceTypeManagerService imple
deviceTypeConfiguration.setPolicyMonitoring(policyMonitoring); deviceTypeConfiguration.setPolicyMonitoring(policyMonitoring);
ProvisioningConfig provisioningConfig = new ProvisioningConfig(); ProvisioningConfig provisioningConfig = new ProvisioningConfig();
provisioningConfig.setSharedWithAllTenants(false); provisioningConfig.setSharedWithAllTenants(deviceTypeMetaDefinition.isSharedWithAllTenants());
deviceTypeConfiguration.setProvisioningConfig(provisioningConfig); deviceTypeConfiguration.setProvisioningConfig(provisioningConfig);
PushNotificationConfig pushNotificationConfig = deviceTypeMetaDefinition.getPushNotificationConfig(); PushNotificationConfig pushNotificationConfig = deviceTypeMetaDefinition.getPushNotificationConfig();

Loading…
Cancel
Save