@ -39,8 +39,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices ;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices ;
import org.wso2.carbon.device.mgt.common.geo.service.Alert ;
import org.wso2.carbon.device.mgt.common.geo.service.Alert ;
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence ;
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence ;
import org.wso2.carbon.device.mgt.common.geo.service.Geo Service;
import org.wso2.carbon.device.mgt.common.geo.service.Geo LocationProvider Service;
import org.wso2.carbon.device.mgt.common.geo.service.Geo ServiceException;
import org.wso2.carbon.device.mgt.common.geo.service.Geo LocationBased ServiceException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder ;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder ;
import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub ;
import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub ;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient ;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient ;
@ -78,9 +78,9 @@ import static org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoSer
* This class will read events , set alerts , read alerts related to geo - fencing and it will
* This class will read events , set alerts , read alerts related to geo - fencing and it will
* use Registry as the persistence storage .
* use Registry as the persistence storage .
* /
* /
public class Geo Servc ieManager Impl implements Geo Service {
public class Geo LocationProvider Servic eImpl implements Geo LocationProvider Service {
private static Log log = LogFactory . getLog ( Geo ServcieManager Impl. class ) ;
private static Log log = LogFactory . getLog ( Geo LocationProviderService Impl. class ) ;
/ * *
/ * *
* required soap header for authorization
* required soap header for authorization
@ -110,7 +110,7 @@ public class GeoServcieManagerImpl implements GeoService {
private static final String SSLV3 = "SSLv3" ;
private static final String SSLV3 = "SSLv3" ;
@Override
@Override
public List < GeoFence > getWithinAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public List < GeoFence > getWithinAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
@ -151,14 +151,14 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
return fences ;
return fences ;
} catch ( RegistryException | IOException e ) {
} catch ( RegistryException | IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
identifier . getId ( ) , e ) ;
identifier . getId ( ) , e ) ;
}
}
}
}
@Override
@Override
public List < GeoFence > getExitAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public List < GeoFence > getExitAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
@ -199,7 +199,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
return fences ;
return fences ;
} catch ( RegistryException | IOException e ) {
} catch ( RegistryException | IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
identifier . getId ( ) , e ) ;
identifier . getId ( ) , e ) ;
}
}
@ -207,18 +207,18 @@ public class GeoServcieManagerImpl implements GeoService {
@Override
@Override
public boolean createGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType )
public boolean createGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
return saveGeoAlert ( alert , identifier , alertType , false ) ;
return saveGeoAlert ( alert , identifier , alertType , false ) ;
}
}
@Override
@Override
public boolean updateGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType )
public boolean updateGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
return saveGeoAlert ( alert , identifier , alertType , true ) ;
return saveGeoAlert ( alert , identifier , alertType , true ) ;
}
}
public boolean saveGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType , boolean isUpdate )
public boolean saveGeoAlert ( Alert alert , DeviceIdentifier identifier , String alertType , boolean isUpdate )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
Type type = new TypeToken < Map < String , String > > ( ) {
Type type = new TypeToken < Map < String , String > > ( ) {
} . getType ( ) ;
} . getType ( ) ;
@ -256,7 +256,7 @@ public class GeoServcieManagerImpl implements GeoService {
} else if ( GeoServices . ALERT_TYPE_TRAFFIC . equals ( alertType ) ) {
} else if ( GeoServices . ALERT_TYPE_TRAFFIC . equals ( alertType ) ) {
content = parseMap . get ( GeoServices . GEO_FENCE_GEO_JSON ) ;
content = parseMap . get ( GeoServices . GEO_FENCE_GEO_JSON ) ;
} else {
} else {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Unrecognized execution plan type: " + alertType + " while creating geo alert" ) ;
"Unrecognized execution plan type: " + alertType + " while creating geo alert" ) ;
}
}
@ -288,24 +288,24 @@ public class GeoServcieManagerImpl implements GeoService {
} else {
} else {
log . error ( "Execution plan validation failed: " + validationResponse ) ;
log . error ( "Execution plan validation failed: " + validationResponse ) ;
}
}
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while " + action + " geo " + alertType + " alert for " +
"Error occurred while " + action + " geo " + alertType + " alert for " +
identifier . getType ( ) + " with id: " + identifier . getId ( ) ) ;
identifier . getType ( ) + " with id: " + identifier . getId ( ) ) ;
}
}
return true ;
return true ;
} catch ( AxisFault axisFault ) {
} catch ( AxisFault axisFault ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Event processor admin service initialization failed while " + action + " geo alert '" +
"Event processor admin service initialization failed while " + action + " geo alert '" +
alertType + "' for " + identifier . getType ( ) + " " +
alertType + "' for " + identifier . getType ( ) + " " +
"device with id: " + identifier . getId ( ) , axisFault
"device with id: " + identifier . getId ( ) , axisFault
) ;
) ;
} catch ( IOException e ) {
} catch ( IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Event processor admin service failed while " + action + " geo alert '" +
"Event processor admin service failed while " + action + " geo alert '" +
alertType + "' for " + identifier . getType ( ) + " " +
alertType + "' for " + identifier . getType ( ) + " " +
"device with id: " + identifier . getId ( ) , e ) ;
"device with id: " + identifier . getId ( ) , e ) ;
} catch ( JWTClientException e ) {
} catch ( JWTClientException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"JWT token creation failed while " + action + " geo alert '" + alertType + "' for " +
"JWT token creation failed while " + action + " geo alert '" + alertType + "' for " +
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e ) ;
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e ) ;
} finally {
} finally {
@ -314,7 +314,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
private String getRegistryPath ( String alertType , DeviceIdentifier identifier , String queryName )
private String getRegistryPath ( String alertType , DeviceIdentifier identifier , String queryName )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
String path = "" ;
String path = "" ;
if ( GeoServices . ALERT_TYPE_WITHIN . equals ( alertType ) ) {
if ( GeoServices . ALERT_TYPE_WITHIN . equals ( alertType ) ) {
path = GeoServices . REGISTRY_PATH_FOR_ALERTS + GeoServices . ALERT_TYPE_WITHIN +
path = GeoServices . REGISTRY_PATH_FOR_ALERTS + GeoServices . ALERT_TYPE_WITHIN +
@ -335,7 +335,7 @@ public class GeoServcieManagerImpl implements GeoService {
path = GeoServices . REGISTRY_PATH_FOR_ALERTS + GeoServices . ALERT_TYPE_TRAFFIC +
path = GeoServices . REGISTRY_PATH_FOR_ALERTS + GeoServices . ALERT_TYPE_TRAFFIC +
"/" + identifier . getId ( ) + "/" + queryName ;
"/" + identifier . getId ( ) + "/" + queryName ;
} else {
} else {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Unrecognized execution plan type: " + alertType ) ;
"Unrecognized execution plan type: " + alertType ) ;
}
}
return path ;
return path ;
@ -351,7 +351,7 @@ public class GeoServcieManagerImpl implements GeoService {
@Override
@Override
public boolean removeGeoAlert ( String alertType , DeviceIdentifier identifier , String queryName )
public boolean removeGeoAlert ( String alertType , DeviceIdentifier identifier , String queryName )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
removeFromRegistry ( alertType , identifier , queryName ) ;
removeFromRegistry ( alertType , identifier , queryName ) ;
String executionPlanName = getExecutionPlanName ( alertType , queryName , identifier . getId ( ) ) ;
String executionPlanName = getExecutionPlanName ( alertType , queryName , identifier . getId ( ) ) ;
EventProcessorAdminServiceStub eventprocessorStub = null ;
EventProcessorAdminServiceStub eventprocessorStub = null ;
@ -360,14 +360,14 @@ public class GeoServcieManagerImpl implements GeoService {
eventprocessorStub . undeployActiveExecutionPlan ( executionPlanName ) ;
eventprocessorStub . undeployActiveExecutionPlan ( executionPlanName ) ;
return true ;
return true ;
} catch ( IOException e ) {
} catch ( IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Event processor admin service stub invocation failed while removing geo alert '" +
"Event processor admin service stub invocation failed while removing geo alert '" +
alertType +
alertType +
"': " + executionPlanName + " for " +
"': " + executionPlanName + " for " +
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e
) ;
) ;
} catch ( JWTClientException e ) {
} catch ( JWTClientException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"JWT token creation failed while removing geo alert '" + alertType + "': " +
"JWT token creation failed while removing geo alert '" + alertType + "': " +
executionPlanName + " for " +
executionPlanName + " for " +
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e
identifier . getType ( ) + " device with id:" + identifier . getId ( ) , e
@ -378,13 +378,13 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
private void removeFromRegistry ( String alertType , DeviceIdentifier identifier , String queryName )
private void removeFromRegistry ( String alertType , DeviceIdentifier identifier , String queryName )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
String path = "unknown" ;
String path = "unknown" ;
try {
try {
path = getRegistryPath ( alertType , identifier , queryName ) ;
path = getRegistryPath ( alertType , identifier , queryName ) ;
getGovernanceRegistry ( ) . delete ( path ) ;
getGovernanceRegistry ( ) . delete ( path ) ;
} catch ( RegistryException e ) {
} catch ( RegistryException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while removing " + alertType + " alert for " + identifier . getType ( ) +
"Error occurred while removing " + alertType + " alert for " + identifier . getType ( ) +
" device with id:" + identifier . getId ( ) + " from the path: " + path ) ;
" device with id:" + identifier . getId ( ) + " from the path: " + path ) ;
}
}
@ -437,7 +437,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
@Override
@Override
public String getSpeedAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public String getSpeedAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
try {
try {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
Resource resource = registry . get ( GeoServices . REGISTRY_PATH_FOR_ALERTS +
Resource resource = registry . get ( GeoServices . REGISTRY_PATH_FOR_ALERTS +
@ -455,7 +455,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
@Override
@Override
public String getProximityAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public String getProximityAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
try {
try {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
Resource resource = registry . get ( GeoServices . REGISTRY_PATH_FOR_ALERTS +
Resource resource = registry . get ( GeoServices . REGISTRY_PATH_FOR_ALERTS +
@ -479,7 +479,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
@Override
@Override
public List < GeoFence > getStationaryAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public List < GeoFence > getStationaryAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
@ -524,14 +524,14 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
return fences ;
return fences ;
} catch ( RegistryException | IOException e ) {
} catch ( RegistryException | IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
identifier . getId ( ) , e ) ;
identifier . getId ( ) , e ) ;
}
}
}
}
@Override
@Override
public List < GeoFence > getTrafficAlerts ( DeviceIdentifier identifier ) throws Geo ServiceException {
public List < GeoFence > getTrafficAlerts ( DeviceIdentifier identifier ) throws Geo LocationBased ServiceException {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
String registryPath = GeoServices . REGISTRY_PATH_FOR_ALERTS +
GeoServices . ALERT_TYPE_STATIONARY + "/" + identifier . getId ( ) + "/" ;
GeoServices . ALERT_TYPE_STATIONARY + "/" + identifier . getId ( ) + "/" ;
@ -571,30 +571,31 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
return fences ;
return fences ;
} catch ( RegistryException | IOException e ) {
} catch ( RegistryException | IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
"Error occurred while getting the geo alerts for " + identifier . getType ( ) + " with id: " +
identifier . getId ( ) , e ) ;
identifier . getId ( ) , e ) ;
}
}
}
}
private Registry getGovernanceRegistry ( ) throws Geo ServiceException {
private Registry getGovernanceRegistry ( ) throws Geo LocationBased ServiceException {
try {
try {
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( ) ;
return DeviceManagementDataHolder . getInstance ( ) . getRegistryService ( )
return DeviceManagementDataHolder . getInstance ( ) . getRegistryService ( )
. getGovernanceSystemRegistry (
. getGovernanceSystemRegistry (
tenantId ) ;
tenantId ) ;
} catch ( RegistryException e ) {
} catch ( RegistryException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error in retrieving governance registry instance: " +
"Error in retrieving governance registry instance: " +
e . getMessage ( ) , e ) ;
e . getMessage ( ) , e ) ;
}
}
}
}
private String parseTemplate ( String alertType , Map < String , String > parseMap ) throws GeoServiceException {
private String parseTemplate ( String alertType , Map < String , String > parseMap ) throws
GeoLocationBasedServiceException {
String templatePath = "alerts/Geo-ExecutionPlan-" + alertType + "_alert.siddhiql" ;
String templatePath = "alerts/Geo-ExecutionPlan-" + alertType + "_alert.siddhiql" ;
InputStream resource = getClass ( ) . getClassLoader ( ) . getResourceAsStream ( templatePath ) ;
InputStream resource = getClass ( ) . getClassLoader ( ) . getResourceAsStream ( templatePath ) ;
if ( resource = = null ) {
if ( resource = = null ) {
throw new Geo ServiceException( "Could not find template in path : " + templatePath ) ;
throw new Geo LocationBased ServiceException( "Could not find template in path : " + templatePath ) ;
}
}
try {
try {
//Read template
//Read template
@ -606,13 +607,13 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
return template ;
return template ;
} catch ( IOException e ) {
} catch ( IOException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while populating the template for the Within Alert" , e ) ;
"Error occurred while populating the template for the Within Alert" , e ) ;
}
}
}
}
private void updateRegistry ( String path , DeviceIdentifier identifier , Object content , Map < String , String > options )
private void updateRegistry ( String path , DeviceIdentifier identifier , Object content , Map < String , String > options )
throws Geo ServiceException {
throws Geo LocationBased ServiceException {
try {
try {
Registry registry = getGovernanceRegistry ( ) ;
Registry registry = getGovernanceRegistry ( ) ;
@ -624,7 +625,7 @@ public class GeoServcieManagerImpl implements GeoService {
}
}
registry . put ( path , newResource ) ;
registry . put ( path , newResource ) ;
} catch ( RegistryException e ) {
} catch ( RegistryException e ) {
throw new Geo ServiceException(
throw new Geo LocationBased ServiceException(
"Error occurred while setting the Within Alert for " + identifier . getType ( ) + " with id: " +
"Error occurred while setting the Within Alert for " + identifier . getType ( ) + " with id: " +
identifier . getId ( ) , e ) ;
identifier . getId ( ) , e ) ;
}
}