mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Merge pull request #743 from ayyoob/new-master
fixed issues related reference device type flow and added per tenant based transport push notification
This commit is contained in:
commit
c0896a9650
@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration
|
||||
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
||||
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.ConfigProperties;
|
||||
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceTypeConfiguration;
|
||||
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.Property;
|
||||
import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.PushNotificationProvider;
|
||||
@ -113,9 +114,15 @@ public class DeviceTypeManagerService implements DeviceManagementService {
|
||||
if (pushNotificationProvider != null) {
|
||||
if (pushNotificationProvider.isFileBasedProperties()) {
|
||||
Map<String, String> staticProps = new HashMap<>();
|
||||
for (Property property : pushNotificationProvider.getConfigProperties().getProperty()) {
|
||||
ConfigProperties configProperties = pushNotificationProvider.getConfigProperties();
|
||||
if (configProperties != null) {
|
||||
List<Property> properties = configProperties.getProperty();
|
||||
if (properties != null && properties.size() > 0) {
|
||||
for (Property property : properties) {
|
||||
staticProps.put(property.getName(), property.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
pushNotificationConfig = new PushNotificationConfig(pushNotificationProvider.getType(),
|
||||
pushNotificationProvider.isScheduled(), staticProps);
|
||||
} else {
|
||||
|
||||
@ -46,20 +46,22 @@ public class MQTTNotificationStrategy implements NotificationStrategy {
|
||||
private String mqttAdapterName;
|
||||
private static final Log log = LogFactory.getLog(MQTTNotificationStrategy.class);
|
||||
private final PushNotificationConfig config;
|
||||
private final String providerTenantDomain;
|
||||
private static final Object lockObj = new Object();
|
||||
|
||||
public MQTTNotificationStrategy(PushNotificationConfig config) {
|
||||
this.config = config;
|
||||
OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration();
|
||||
adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE);
|
||||
mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME);
|
||||
adapterConfig.setName(mqttAdapterName);
|
||||
adapterConfig.setMessageFormat(MessageType.TEXT);
|
||||
|
||||
Map<String, String> configProperties = new HashMap<String, String>();
|
||||
if (config.getProperties() != null && config.getProperties().size() > 0) {
|
||||
String brokerUrl = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL);
|
||||
if (brokerUrl != null && !brokerUrl.isEmpty()) {
|
||||
configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, brokerUrl);
|
||||
}
|
||||
mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME);
|
||||
configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME,
|
||||
config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME));
|
||||
configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_PASSWORD,
|
||||
@ -70,12 +72,26 @@ public class MQTTNotificationStrategy implements NotificationStrategy {
|
||||
config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES));
|
||||
configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS,
|
||||
config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS));
|
||||
} else {
|
||||
mqttAdapterName = "mqtt.adapter." + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()
|
||||
.toLowerCase();
|
||||
}
|
||||
adapterConfig.setName(mqttAdapterName);
|
||||
adapterConfig.setStaticProperties(configProperties);
|
||||
try {
|
||||
synchronized (lockObj) {
|
||||
try {
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().isPolled(mqttAdapterName);
|
||||
} catch (OutputEventAdapterException e) {
|
||||
//event adapter not created
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig);
|
||||
}
|
||||
}
|
||||
} catch (OutputEventAdapterException e) {
|
||||
throw new InvalidConfigurationException("Error occurred while initializing MQTT output event adapter", e);
|
||||
}
|
||||
providerTenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -85,19 +101,47 @@ public class MQTTNotificationStrategy implements NotificationStrategy {
|
||||
|
||||
@Override
|
||||
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
|
||||
String adapterName = mqttAdapterName;
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
|
||||
if (!providerTenantDomain.equals(tenantDomain)) {
|
||||
//this is to handle the device type shared with all tenant mode.
|
||||
|
||||
adapterName = "mqtt.adapter." + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()
|
||||
.toLowerCase();
|
||||
try {
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().isPolled(adapterName);
|
||||
} catch (OutputEventAdapterException e) {
|
||||
//event adapter not created
|
||||
synchronized (lockObj) {
|
||||
OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration();
|
||||
adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE);
|
||||
adapterConfig.setMessageFormat(MessageType.TEXT);
|
||||
adapterConfig.setName(adapterName);
|
||||
Map<String, String> configProperties = new HashMap<String, String>();
|
||||
adapterConfig.setStaticProperties(configProperties);
|
||||
try {
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig);
|
||||
} catch (OutputEventAdapterException e1) {
|
||||
throw new PushNotificationExecutionFailedException
|
||||
("Error occurred while initializing MQTT output event adapter for shared tenant: "
|
||||
+ tenantDomain, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Operation operation = ctx.getOperation();
|
||||
Properties properties = operation.getProperties();
|
||||
if (properties != null && properties.get(MQTT_ADAPTER_TOPIC) != null) {
|
||||
Map<String, String> dynamicProperties = new HashMap<>();
|
||||
dynamicProperties.put("topic", (String) properties.get(MQTT_ADAPTER_TOPIC));
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties,
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties,
|
||||
operation.getPayLoad());
|
||||
} else {
|
||||
if (PolicyOperation.POLICY_OPERATION_CODE.equals(operation.getCode())) {
|
||||
PolicyOperation policyOperation = (PolicyOperation) operation;
|
||||
List<ProfileOperation> profileOperations = policyOperation.getProfileOperations();
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
|
||||
String deviceType = ctx.getDeviceId().getType();
|
||||
String deviceId = ctx.getDeviceId().getId();
|
||||
for (ProfileOperation profileOperation : profileOperations) {
|
||||
@ -106,7 +150,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy {
|
||||
+ deviceType + "/" + deviceId + "/" + profileOperation.getType()
|
||||
.toString().toLowerCase() + "/" + profileOperation.getCode().toLowerCase();
|
||||
dynamicProperties.put("topic", topic);
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties,
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties,
|
||||
profileOperation.getPayLoad());
|
||||
}
|
||||
|
||||
@ -119,7 +163,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy {
|
||||
if (operation.getPayLoad() == null) {
|
||||
operation.setPayLoad("");
|
||||
}
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties,
|
||||
MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties,
|
||||
operation.getPayLoad());
|
||||
|
||||
}
|
||||
|
||||
@ -115,4 +115,43 @@ public interface DeviceAccessAuthorizationAdminService {
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response isAuthorized(AuthorizationRequest authorizationRequest);
|
||||
|
||||
@POST
|
||||
@Path("/stat")
|
||||
@ApiOperation(
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "Check for device access authorization for stat\n",
|
||||
notes = "This is an internal API that can be used to check for authorization.",
|
||||
response = DeviceAuthorizationResult.class,
|
||||
tags = "Authorization Administrative Service",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:authorization:verify")
|
||||
})
|
||||
})
|
||||
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Authorized device list will be delivered to the requested services",
|
||||
response = DeviceAuthorizationResult.class),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n The specified resource does not exist."),
|
||||
@ApiResponse(
|
||||
code = 415,
|
||||
message = "Unsupported media type. \n The entity of the request was in a not supported format."),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n Server error occurred while checking the authorization" +
|
||||
" for a specified set of devices.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response isAuthorizedForStat(AuthorizationRequest authorizationRequest);
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ import javax.ws.rs.core.Response;
|
||||
title = "",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = "name", value = "DeviceTypePublisherAdminService"),
|
||||
@ExtensionProperty(name = "name", value = "DeviceAnalyticsArtifactUploaderAdminService"),
|
||||
@ExtensionProperty(name = "context", value = "/api/device-mgt/v1.0/admin/devicetype"),
|
||||
})
|
||||
}
|
||||
@ -62,7 +62,7 @@ import javax.ws.rs.core.Response;
|
||||
}
|
||||
)
|
||||
|
||||
public interface DeviceTypePublisherAdminService {
|
||||
public interface DeviceAnalyticsArtifactUploaderAdminService {
|
||||
|
||||
@POST
|
||||
@Path("/deploy/{type}")
|
||||
@ -36,6 +36,8 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Path("/admin/authorization")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ -43,6 +45,7 @@ import javax.ws.rs.core.Response;
|
||||
public class DeviceAccessAuthorizationAdminServiceImpl implements DeviceAccessAuthorizationAdminService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceAccessAuthorizationAdminServiceImpl.class);
|
||||
private static final String DEFAULT_STAT_PERMISSION = "/permission/admin/device-mgt/device/realtime_analytics";
|
||||
|
||||
@POST
|
||||
@Override
|
||||
@ -90,4 +93,14 @@ public class DeviceAccessAuthorizationAdminServiceImpl implements DeviceAccessAu
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/stat")
|
||||
@Override
|
||||
public Response isAuthorizedForStat(AuthorizationRequest authorizationRequest) {
|
||||
List<String> permissions = new ArrayList<>();
|
||||
permissions.add(DEFAULT_STAT_PERMISSION);
|
||||
authorizationRequest.setPermissions(permissions);
|
||||
return isAuthorized(authorizationRequest);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -37,13 +37,10 @@ import org.wso2.carbon.application.mgt.stub.upload.types.carbon.UploadedFileItem
|
||||
import org.wso2.carbon.base.ServerConfiguration;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.core.util.Utils;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypePublisherAdminService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceAnalyticsArtifactUploaderAdminService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.registry.core.Registry;
|
||||
import org.wso2.carbon.registry.core.Resource;
|
||||
import org.wso2.carbon.registry.core.ResourceImpl;
|
||||
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
@ -55,7 +52,6 @@ import javax.activation.DataHandler;
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
@ -73,7 +69,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Path("/admin/devicetype")
|
||||
public class DeviceTypePublisherAdminServiceImpl implements DeviceTypePublisherAdminService {
|
||||
public class DeviceAnalyticsArtifactUploaderAdminServiceImpl implements DeviceAnalyticsArtifactUploaderAdminService {
|
||||
|
||||
/**
|
||||
* required soap header for authorization
|
||||
@ -110,7 +106,7 @@ public class DeviceTypePublisherAdminServiceImpl implements DeviceTypePublisherA
|
||||
|
||||
private String tenantDomain;
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceTypePublisherAdminServiceImpl.class);
|
||||
private static final Log log = LogFactory.getLog(DeviceAnalyticsArtifactUploaderAdminServiceImpl.class);
|
||||
private static final String DEFAULT_RESOURCE_LOCATION = "/resources/devicetypes";
|
||||
private static final String CAR_FILE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" +
|
||||
File.separator + "resources" + File.separator + "devicetypes";
|
||||
@ -178,6 +174,9 @@ public class DeviceTypePublisherAdminServiceImpl implements DeviceTypePublisherA
|
||||
publishDynamicEventReceivers(type, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, receiverFileList);
|
||||
}
|
||||
}
|
||||
if (streamFileList != null) {
|
||||
publishDynamicEventStream(type, tenantDomain, streamFileList);
|
||||
}
|
||||
if (deployAnalyticsCapp(type, list)){
|
||||
return Response.status(Response.Status.BAD_REQUEST)
|
||||
.entity("\"Error, Artifact does not exist.\"").build();
|
||||
@ -40,7 +40,7 @@
|
||||
<ref bean="applicationManagementAdminService"/>
|
||||
<ref bean="deviceTypeManagementAdminService"/>
|
||||
<ref bean="deviceTypeManagementAdminService"/>
|
||||
<ref bean="deviceTypePublisherAdminServiceImpl"/>
|
||||
<ref bean="deviceAnalyticsArtifactUploaderAdminService"/>
|
||||
<ref bean="swaggerResource"/>
|
||||
</jaxrs:serviceBeans>
|
||||
<jaxrs:providers>
|
||||
@ -82,7 +82,7 @@
|
||||
<bean id="userManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.UserManagementAdminServiceImpl"/>
|
||||
<bean id="deviceTypeManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.DeviceTypeManagementServiceImpl"/>
|
||||
<bean id="deviceAccessAuthorizationAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.DeviceAccessAuthorizationAdminServiceImpl"/>
|
||||
<bean id="deviceTypePublisherAdminServiceImpl" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.DeviceTypePublisherAdminServiceImpl"/>
|
||||
<bean id="deviceAnalyticsArtifactUploaderAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.DeviceAnalyticsArtifactUploaderAdminServiceImpl"/>
|
||||
|
||||
<bean id="jsonProvider" class="org.wso2.carbon.device.mgt.jaxrs.common.GsonMessageBodyHandler"/>
|
||||
|
||||
|
||||
@ -2114,22 +2114,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
deviceIdentifiers.add(deviceIdentifier);
|
||||
if (init != null) {
|
||||
List<String> initialOperations = init.getOperations();
|
||||
|
||||
if (initialOperations != null) {
|
||||
for (String str : initialOperations) {
|
||||
CommandOperation operation = new CommandOperation();
|
||||
operation.setEnabled(true);
|
||||
operation.setType(Operation.Type.COMMAND);
|
||||
operation.setCode(str);
|
||||
try {
|
||||
deviceManagementProviderService.
|
||||
addOperation(deviceType,
|
||||
operation, deviceIdentifiers);
|
||||
deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifiers);
|
||||
} catch (OperationManagementException e) {
|
||||
throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(),
|
||||
e);
|
||||
throw new DeviceManagementException("Unable to add the operation for the device with the id: '"
|
||||
+ deviceIdentifier.getId(), e);
|
||||
} catch (InvalidDeviceException e) {
|
||||
throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(),
|
||||
e);
|
||||
throw new DeviceManagementException("Unable to find the device with the id: '"
|
||||
+ deviceIdentifier.getId(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user