From 790f91b3b71b04f10d5ec254e60a599f0dd28d23 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Tue, 9 Jan 2018 10:12:15 +0530 Subject: [PATCH] Add method overloads --- .../impl/GeoLocationBasedServiceImpl.java | 23 +++ .../service/GeoLocationProviderService.java | 3 + .../GeoLocationProviderServiceImpl.java | 152 ++++++++++++++++++ .../app/pages/cdmf.page.devices/devices.js | 3 +- 4 files changed, 180 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 487b367b63..7890b463fc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -185,6 +185,29 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } } + + @Path("alerts/{alertType}") + @POST + @Consumes("application/json") + @Produces("application/json") + public Response createGeoAlertsForGeoDashboard(Alert alert, @PathParam("alertType") String alertType) { + try { + // this is the user who initiates the request + String authorizedUser = MultitenantUtils.getTenantAwareUsername( + CarbonContext.getThreadLocalCarbonContext().getUsername() + ); + + GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); + geoService.createGeoAlert(alert, alertType); + return Response.ok().build(); + } catch (GeoLocationBasedServiceException e) { + String error = "Error occurred while creating " + alertType + "alert"; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + + @Path("alerts/{alertType}/{deviceType}/{deviceId}") @PUT @Consumes("application/json") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java index 4c9e02f158..924f385dd3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java @@ -35,6 +35,9 @@ public interface GeoLocationProviderService { boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) throws GeoLocationBasedServiceException, AlertAlreadyExistException; + boolean createGeoAlert(Alert alert, String alertType) + throws GeoLocationBasedServiceException; + boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) throws GeoLocationBasedServiceException, AlertAlreadyExistException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index f888d7c32c..1243363cd2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -213,12 +213,110 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic return saveGeoAlert(alert, identifier, alertType, false); } + @Override + public boolean createGeoAlert(Alert alert, String alertType) + throws GeoLocationBasedServiceException { + return saveGeoAlert(alert, alertType, false); + } + @Override public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) throws GeoLocationBasedServiceException, AlertAlreadyExistException { return saveGeoAlert(alert, identifier, alertType, true); } + public boolean saveGeoAlert(Alert alert, String alertType, boolean isUpdate) + throws GeoLocationBasedServiceException { + + Type type = new TypeToken>() { + }.getType(); + Gson gson = new Gson(); + Map parseMap = gson.fromJson(alert.getParseData(), type); + + Map options = new HashMap<>(); + Object content = null; + + if (GeoServices.ALERT_TYPE_WITHIN.equals(alertType)) { + options.put(GeoServices.QUERY_NAME, alert.getQueryName()); + options.put(GeoServices.AREA_NAME, alert.getCustomName()); + content = parseMap.get(GeoServices.GEO_FENCE_GEO_JSON); + + } else if (GeoServices.ALERT_TYPE_EXIT.equals(alertType)) { + options.put(GeoServices.QUERY_NAME, alert.getQueryName()); + options.put(GeoServices.AREA_NAME, alert.getCustomName()); + content = parseMap.get(GeoServices.GEO_FENCE_GEO_JSON); + + } else if (GeoServices.ALERT_TYPE_SPEED.equals(alertType)) { + content = parseMap.get(GeoServices.SPEED_ALERT_VALUE); + + } else if (GeoServices.ALERT_TYPE_PROXIMITY.equals(alertType)) { + options.put(GeoServices.PROXIMITY_DISTANCE, alert.getProximityDistance()); + options.put(GeoServices.PROXIMITY_TIME, alert.getProximityTime()); + content = alert.getParseData(); + + } else if (GeoServices.ALERT_TYPE_STATIONARY.equals(alertType)) { + options.put(GeoServices.QUERY_NAME, alert.getQueryName()); + options.put(GeoServices.AREA_NAME, alert.getCustomName()); + options.put(GeoServices.STATIONARY_TIME, alert.getStationeryTime()); + options.put(GeoServices.FLUCTUATION_RADIUS, alert.getFluctuationRadius()); + content = alert.getParseData(); + + } else if (GeoServices.ALERT_TYPE_TRAFFIC.equals(alertType)) { + content = parseMap.get(GeoServices.GEO_FENCE_GEO_JSON); + } else { + throw new GeoLocationBasedServiceException( + "Unrecognized execution plan type: " + alertType + " while creating geo alert"); + } + + //persist alert in registry + updateRegistry(getRegistryPath(alertType, alert.getQueryName()), content, + options); + + //deploy alert into event processor + EventProcessorAdminServiceStub eventprocessorStub = null; + String action = (isUpdate ? "updating" : "creating"); + try { + eventprocessorStub = getEventProcessorAdminServiceStub(); + String parsedTemplate = parseTemplate(alertType, parseMap); + String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); + if (validationResponse.equals("success")) { + if (isUpdate) { + String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName()); + eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); + } else { + eventprocessorStub.deployExecutionPlan(parsedTemplate); + } + } else { + if (validationResponse.startsWith( + "'within' is neither a function extension nor an aggregated attribute extension" + )) { + log.error("GPL Siddhi Geo Extension is not configured. Please execute maven script " + + "`siddhi-geo-extention-deployer.xml` in $IOT_HOME/analytics/scripts"); + } else { + log.error("Execution plan validation failed: " + validationResponse); + } + throw new GeoLocationBasedServiceException( + "Error occurred while " + action + " geo " + alertType); + } + return true; + } catch (AxisFault axisFault) { + throw new GeoLocationBasedServiceException( + "Event processor admin service initialization failed while " + action + " geo alert '" + + alertType, axisFault + ); + } catch (IOException e) { + throw new GeoLocationBasedServiceException( + "Event processor admin service failed while " + action + " geo alert '" + + alertType, e); + } catch (JWTClientException e) { + throw new GeoLocationBasedServiceException( + "JWT token creation failed while " + action + " geo alert '" + alertType, e); + } finally { + cleanup(eventprocessorStub); + } + + } + public boolean saveGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType, boolean isUpdate) throws GeoLocationBasedServiceException, AlertAlreadyExistException { @@ -358,6 +456,34 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic return path; } + private String getRegistryPath(String alertType, String queryName) + throws GeoLocationBasedServiceException { + String path = ""; + if (GeoServices.ALERT_TYPE_WITHIN.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_WITHIN + + "/" + "/" + queryName; + } else if (GeoServices.ALERT_TYPE_EXIT.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_EXIT + + "/" + queryName; + } else if (GeoServices.ALERT_TYPE_SPEED.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_SPEED + + "/" ; + } else if (GeoServices.ALERT_TYPE_PROXIMITY.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_PROXIMITY + + "/" + queryName; + } else if (GeoServices.ALERT_TYPE_STATIONARY.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_STATIONARY + + "/" + queryName; + } else if (GeoServices.ALERT_TYPE_TRAFFIC.equals(alertType)) { + path = GeoServices.REGISTRY_PATH_FOR_ALERTS + GeoServices.ALERT_TYPE_TRAFFIC + + "/" + queryName; + } else { + throw new GeoLocationBasedServiceException( + "Unrecognized execution plan type: " + alertType); + } + return path; + } + private String getExecutionPlanName(String alertType, String queryName, String deviceId) { if ("Traffic".equals(alertType)) { return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; @@ -368,6 +494,14 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } } + private String getExecutionPlanName(String alertType, String queryName) { + if ("Traffic".equals(alertType)) { + return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; + } else { + return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + "_alert"; + } + } + @Override public boolean removeGeoAlert(String alertType, DeviceIdentifier identifier, String queryName) throws GeoLocationBasedServiceException { @@ -650,6 +784,24 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } } + private void updateRegistry(String path, Object content, Map options) + throws GeoLocationBasedServiceException { + try { + + Registry registry = getGovernanceRegistry(); + Resource newResource = registry.newResource(); + newResource.setContent(content); + newResource.setMediaType("application/json"); + for (Map.Entry option : options.entrySet()) { + newResource.addProperty(option.getKey(), option.getValue()); + } + registry.put(path, newResource); + } catch (RegistryException e) { + throw new GeoLocationBasedServiceException( + "Error occurred while setting the Within Alert", e); + } + } + /** * Loads the keystore. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js index c973d07985..407819fef9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js @@ -10,7 +10,8 @@ * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * "AS IS" BASIS, WITHOUTna innawa kiala + WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License.