forked from community/device-mgt-core
Introduce event operations & geo fence event handling See merge request entgra/carbon-device-mgt!699corrective-policy
commit
30f78f5ba9
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
public class EventAction {
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "actionType",
|
||||
value = "Type of the event action to be triggered in the device level",
|
||||
required = true)
|
||||
private String actionType;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "payload",
|
||||
value = "Payload of the event action",
|
||||
required = true)
|
||||
private Object payload;
|
||||
|
||||
public String getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(String actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void setPayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventConfig {
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "id",
|
||||
value = "id of the event entry")
|
||||
private int id;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "eventLogic",
|
||||
value = "Logic of the event should be handled at the device level",
|
||||
required = true)
|
||||
private String eventLogic;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "actions",
|
||||
value = "List of actions to be triggered according to the logic",
|
||||
required = true)
|
||||
private List<EventAction> actions;
|
||||
|
||||
public String getEventLogic() {
|
||||
return eventLogic;
|
||||
}
|
||||
|
||||
public void setEventLogic(String eventLogic) {
|
||||
this.eventLogic = eventLogic;
|
||||
}
|
||||
|
||||
public List<EventAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(List<EventAction> actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public class EventAction {
|
||||
private String actionType;
|
||||
private Object payload;
|
||||
|
||||
public String getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(String actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void setPayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public class EventConfig {
|
||||
private int eventId;
|
||||
private String eventSource;
|
||||
private String eventLogic;
|
||||
private String actions;
|
||||
|
||||
public String getEventLogic() {
|
||||
return eventLogic;
|
||||
}
|
||||
|
||||
public void setEventLogic(String eventLogic) {
|
||||
this.eventLogic = eventLogic;
|
||||
}
|
||||
|
||||
public String getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(String actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
public int getEventId() {
|
||||
return eventId;
|
||||
}
|
||||
|
||||
public void setEventId(int eventId) {
|
||||
this.eventId = eventId;
|
||||
}
|
||||
|
||||
public String getEventSource() {
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
public void setEventSource(String eventSource) {
|
||||
this.eventSource = eventSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof EventConfig) {
|
||||
EventConfig eventConfig = (EventConfig) obj;
|
||||
return this.eventSource.equalsIgnoreCase(eventConfig.getEventSource()) &&
|
||||
this.eventLogic.equalsIgnoreCase(eventConfig.getEventLogic());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{eventId=" + eventId +
|
||||
", eventSource='" + eventSource + '\'' +
|
||||
", eventLogic='" + eventLogic + '\'' +
|
||||
", actions='" + actions + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public class EventConfigurationException extends Exception {
|
||||
public EventConfigurationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventConfigurationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public EventConfigurationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public EventConfigurationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventConfigurationProviderService {
|
||||
/**
|
||||
* Create event configuration records
|
||||
*
|
||||
* @param eventConfigList event list to be added
|
||||
* @param groupIds group ids of the events are mapped
|
||||
* @return generated event ids
|
||||
* @throws EventConfigurationException errors thrown while creating event configuration
|
||||
*/
|
||||
List<Integer> createEventsOfDeviceGroup(List<EventConfig> eventConfigList, List<Integer> groupIds)
|
||||
throws EventConfigurationException;
|
||||
|
||||
/**
|
||||
* Update event configuration records
|
||||
*
|
||||
* @param eventConfig updated event configuration list. event ids should be present for
|
||||
* the updating events and event ids should be -1 for the newly creating events
|
||||
* @param removedEventIdList event ids of removed while updating the event configuration
|
||||
* @param groupIds group ids to be mapped with updated events
|
||||
* @return Newly created event Ids
|
||||
* @throws EventConfigurationException
|
||||
*/
|
||||
List<Integer> updateEventsOfDeviceGroup(List<EventConfig> eventConfig, List<Integer> removedEventIdList,
|
||||
List<Integer> groupIds) throws EventConfigurationException;
|
||||
|
||||
/**
|
||||
* Retrieve event list using event IDs
|
||||
*
|
||||
* @param createdEventIds event ids
|
||||
* @return {@link EventConfig} List of events of defined IDs
|
||||
* @throws EventConfigurationException
|
||||
*/
|
||||
List<EventConfig> getEvents(List<Integer> createdEventIds) throws EventConfigurationException;
|
||||
|
||||
/**
|
||||
* Get event sources attached to a specific group
|
||||
*
|
||||
* @param groupId mapped group id of events
|
||||
* @param tenantId event owning tenant
|
||||
* @return Event sources of the group
|
||||
* @throws EventConfigurationException error thrown while retrieving event sources
|
||||
*/
|
||||
List<String> getEventsSourcesOfGroup(int groupId, int tenantId) throws EventConfigurationException;
|
||||
|
||||
/**
|
||||
* Delete events by event Ids
|
||||
*
|
||||
* @param eventList event list to be deleted
|
||||
* @throws EventConfigurationException error thrown while deleting event records
|
||||
*/
|
||||
void deleteEvents(List<EventConfig> eventList) throws EventConfigurationException;
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public interface EventMetaData {}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventOperation {
|
||||
private String eventSource;
|
||||
private EventMetaData eventDefinition;
|
||||
private String eventTriggers;
|
||||
|
||||
public String getEventSource() {
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
public void setEventSource(String eventSource) {
|
||||
this.eventSource = eventSource;
|
||||
}
|
||||
|
||||
public EventMetaData getEventDefinition() {
|
||||
return eventDefinition;
|
||||
}
|
||||
|
||||
public void setEventDefinition(EventMetaData eventDefinition) {
|
||||
this.eventDefinition = eventDefinition;
|
||||
}
|
||||
|
||||
public String getEventTriggers() {
|
||||
return eventTriggers;
|
||||
}
|
||||
|
||||
public void setEventTriggers(List<EventConfig> eventList) {
|
||||
JsonArray eventTriggers = new JsonArray();
|
||||
JsonObject eventTrigger;
|
||||
for (EventConfig eventConfig : eventList) {
|
||||
eventTrigger = new JsonObject();
|
||||
eventTrigger.addProperty("eventId", eventConfig.getEventId());
|
||||
eventTrigger.addProperty("eventLogic", eventConfig.getEventLogic());
|
||||
eventTrigger.addProperty("actions", eventConfig.getActions());
|
||||
eventTriggers.add(eventTrigger);
|
||||
}
|
||||
this.eventTriggers = eventTriggers.toString();
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.event.config;
|
||||
public class EventRevokeOperation {
|
||||
private String eventSource;
|
||||
private int id;
|
||||
|
||||
public String getEventSource() {
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
public void setEventSource(String eventSource) {
|
||||
this.eventSource = eventSource;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.geo.service;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventMetaData;
|
||||
|
||||
public class GeoFenceEventMeta implements EventMetaData {
|
||||
private int id;
|
||||
private String fenceName;
|
||||
private String description;
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private float radius;
|
||||
private String geoJson;
|
||||
private String fenceShape;
|
||||
|
||||
public GeoFenceEventMeta() {
|
||||
}
|
||||
|
||||
public GeoFenceEventMeta(GeofenceData geofenceData) {
|
||||
this.id = geofenceData.getId();
|
||||
this.fenceName = geofenceData.getFenceName();
|
||||
this.description = geofenceData.getDescription();
|
||||
this.latitude = geofenceData.getLatitude();
|
||||
this.longitude = geofenceData.getLongitude();
|
||||
this.radius = geofenceData.getRadius();
|
||||
this.geoJson = geofenceData.getGeoJson();
|
||||
this.fenceShape = geofenceData.getFenceShape();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFenceName() {
|
||||
return fenceName;
|
||||
}
|
||||
|
||||
public void setFenceName(String fenceName) {
|
||||
this.fenceName = fenceName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public float getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public void setRadius(float radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public String getGeoJson() {
|
||||
return geoJson;
|
||||
}
|
||||
|
||||
public void setGeoJson(String geoJson) {
|
||||
this.geoJson = geoJson;
|
||||
}
|
||||
|
||||
public String getFenceShape() {
|
||||
return fenceShape;
|
||||
}
|
||||
|
||||
public void setFenceShape(String fenceShape) {
|
||||
this.fenceShape = fenceShape;
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventConfigDAO {
|
||||
|
||||
/**
|
||||
* Create event configuration entries of the db for a selected tenant
|
||||
* @param eventConfigList event list to be created
|
||||
* @param tenantId corresponding tenant id of the events
|
||||
* @return generated event ids while storing geofence data
|
||||
* @throws EventManagementDAOException error occurred while creating event records
|
||||
*/
|
||||
List<Integer> storeEventRecords(List<EventConfig> eventConfigList, int tenantId) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Cerate even-group mapping records
|
||||
* @param eventIds event ids to be mapped with groups
|
||||
* @param groupIds group ids of the event attached with
|
||||
* @return true for the successful creation
|
||||
* @throws EventManagementDAOException error occurred while creating event-group mapping records
|
||||
*/
|
||||
boolean addEventGroupMappingRecords(List<Integer> eventIds, List<Integer> groupIds) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Get events owned by a specific device group
|
||||
* @param groupIds group ids of the events
|
||||
* @param tenantId tenant of the events owning
|
||||
* @return list of event configuration filtered by tenant id and group ids
|
||||
* @throws EventManagementDAOException error occurred while reading event records
|
||||
*/
|
||||
List<EventConfig> getEventsOfGroups(List<Integer> groupIds, int tenantId) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Get events of groups using group Id
|
||||
* @param groupId id of the group
|
||||
* @param tenantId id of the tenant
|
||||
* @return EventConfig list of specific group
|
||||
* @throws EventManagementDAOException errors occur while retrieving events of groups
|
||||
*/
|
||||
List<EventConfig> getEventsOfGroups(int groupId, int tenantId) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Delete event group mapping records using the group ids
|
||||
* @param groupIdsToDelete id of groups
|
||||
* @throws EventManagementDAOException error occurred while deleting event-group mapping records
|
||||
*/
|
||||
void deleteEventGroupMappingRecordsByGroupIds(List<Integer> groupIdsToDelete) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Update event records of the tenant
|
||||
* @param eventsToUpdate updating event records
|
||||
* @throws EventManagementDAOException error occurred while updating events
|
||||
*/
|
||||
void updateEventRecords(List<EventConfig> eventsToUpdate) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Delete events using event ids
|
||||
* @param eventsIdsToDelete ids of the events which should be deleted
|
||||
* @throws EventManagementDAOException error occurred while deleting event records
|
||||
*/
|
||||
void deleteEventRecords(List<Integer> eventsIdsToDelete) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Get event records by event ids
|
||||
* @param eventIds filtering event ids
|
||||
* @return filtered event configuration list
|
||||
* @throws EventManagementDAOException error occurred while reading events
|
||||
*/
|
||||
List<EventConfig> getEventsById(List<Integer> eventIds) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Get group ids belong to events using event ids
|
||||
* @param eventIds Ids of the events mapped with group
|
||||
* @return Group Id list
|
||||
* @throws EventManagementDAOException thrown errors while retrieving group Ids of events
|
||||
*/
|
||||
List<Integer> getGroupsOfEvents(List<Integer> eventIds) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Delete event group mapping records using event Ids
|
||||
* @param eventIds Ids of the events
|
||||
* @throws EventManagementDAOException thrown errors while deleting event group mappings
|
||||
*/
|
||||
void deleteEventGroupMappingRecordsByEventIds(List<Integer> eventIds) throws EventManagementDAOException;
|
||||
|
||||
/**
|
||||
* Retrieve event sources mapped with specific groups and tenant
|
||||
* @param groupId Id of the group
|
||||
* @param tenantId Id of the tenant
|
||||
* @return Event source list belong to
|
||||
* @throws EventManagementDAOException thrown errors while retrieving event sources
|
||||
*/
|
||||
List<String> getEventSourcesOfGroups(int groupId, int tenantId) throws EventManagementDAOException;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
public class EventManagementDAOException extends Exception{
|
||||
public EventManagementDAOException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventManagementDAOException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public EventManagementDAOException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public EventManagementDAOException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,329 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractEventConfigDAO implements EventConfigDAO {
|
||||
private static final Log log = LogFactory.getLog(AbstractEventConfigDAO.class);
|
||||
|
||||
@Override
|
||||
public boolean addEventGroupMappingRecords(List<Integer> eventIds, List<Integer> groupIds) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_EVENT_GROUP_MAPPING(" +
|
||||
"EVENT_ID ," +
|
||||
"GROUP_ID) " +
|
||||
"VALUES (?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer groupId : groupIds) {
|
||||
for (Integer eventId : eventIds) {
|
||||
stmt.setInt(1, eventId);
|
||||
stmt.setInt(2, groupId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
}
|
||||
return stmt.executeBatch().length > 0;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventConfig> getEventsOfGroups(List<Integer> groupIds, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
List<EventConfig> eventList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"E.ID AS EVENT_ID, " +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS " +
|
||||
"FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " +
|
||||
"WHERE G.EVENT_ID = E.ID " +
|
||||
"AND G.GROUP_ID IN (%s) " +
|
||||
"AND E.TENANT_ID = ? " +
|
||||
"GROUP BY E.ID";
|
||||
String inClause = String.join(", ", Collections.nCopies(groupIds.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer groupId : groupIds) {
|
||||
stmt.setInt(index++, groupId);
|
||||
}
|
||||
stmt.setInt(index, tenantId);
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
while (rst.next()) {
|
||||
EventConfig event = new EventConfig();
|
||||
event.setEventId(rst.getInt("EVENT_ID"));
|
||||
event.setEventSource(rst.getString("EVENT_SOURCE"));
|
||||
event.setEventLogic(rst.getString("EVENT_LOGIC"));
|
||||
event.setActions(rst.getString("ACTIONS"));
|
||||
eventList.add(event);
|
||||
}
|
||||
return eventList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventConfig> getEventsOfGroups(int groupId, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
List<EventConfig> eventList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"E.ID AS EVENT_ID, " +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS " +
|
||||
"FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " +
|
||||
"WHERE G.EVENT_ID = E.ID " +
|
||||
"AND G.GROUP_ID = ? " +
|
||||
"AND E.TENANT_ID = ? " +
|
||||
"GROUP BY E.ID";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, groupId);
|
||||
stmt.setInt(2, tenantId);
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
while (rst.next()) {
|
||||
EventConfig event = new EventConfig();
|
||||
event.setEventId(rst.getInt("EVENT_ID"));
|
||||
event.setEventSource(rst.getString("EVENT_SOURCE"));
|
||||
event.setEventLogic(rst.getString("EVENT_LOGIC"));
|
||||
event.setActions(rst.getString("ACTIONS"));
|
||||
eventList.add(event);
|
||||
}
|
||||
return eventList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving event records of group " + groupId
|
||||
+ " and tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEventGroupMappingRecordsByEventIds(List<Integer> eventsIdsToDelete) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_DEVICE_EVENT_GROUP_MAPPING WHERE EVENT_ID IN (%s)";
|
||||
String inClause = String.join(", ", Collections.nCopies(eventsIdsToDelete.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer eventId : eventsIdsToDelete) {
|
||||
stmt.setInt(index++, eventId);
|
||||
}
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEventGroupMappingRecordsByGroupIds(List<Integer> groupIdsToDelete) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_DEVICE_EVENT_GROUP_MAPPING WHERE GROUP_ID IN (%s)";
|
||||
String inClause = String.join(", ", Collections.nCopies(groupIdsToDelete.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer groupId : groupIdsToDelete) {
|
||||
stmt.setInt(index++, groupId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEventRecords(List<EventConfig> eventsToUpdate) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "UPDATE DM_DEVICE_EVENT SET " +
|
||||
"ACTIONS = ? " +
|
||||
"WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (EventConfig updatingEvent : eventsToUpdate) {
|
||||
stmt.setString(1, updatingEvent.getActions());
|
||||
stmt.setInt(2, updatingEvent.getEventId());
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while updating event records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEventRecords(List<Integer> eventsIdsToDelete) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_DEVICE_EVENT WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer eventId : eventsIdsToDelete) {
|
||||
stmt.setInt(1, eventId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting event records of tenant";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventConfig> getEventsById(List<Integer> eventIdList) throws EventManagementDAOException {
|
||||
try {
|
||||
List<EventConfig> eventList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"ID AS EVENT_ID, " +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS " +
|
||||
"FROM DM_DEVICE_EVENT " +
|
||||
"WHERE ID IN (%s) ";
|
||||
String inClause = String.join(", ", Collections.nCopies(eventIdList.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer eventId : eventIdList) {
|
||||
if (eventId != -1) {
|
||||
stmt.setInt(index++, eventId);
|
||||
}
|
||||
}
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
while (rst.next()) {
|
||||
EventConfig event = new EventConfig();
|
||||
event.setEventId(rst.getInt("EVENT_ID"));
|
||||
event.setEventSource(rst.getString("EVENT_SOURCE"));
|
||||
event.setEventLogic(rst.getString("EVENT_LOGIC"));
|
||||
event.setActions(rst.getString("ACTIONS"));
|
||||
eventList.add(event);
|
||||
}
|
||||
return eventList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getGroupsOfEvents(List<Integer> eventIdList) throws EventManagementDAOException {
|
||||
try {
|
||||
List<Integer> groupIdList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"GROUP_ID " +
|
||||
"FROM DM_DEVICE_EVENT_GROUP_MAPPING " +
|
||||
"WHERE EVENT_ID IN (%s) " +
|
||||
"GROUP BY GROUP_ID";
|
||||
String inClause = String.join(", ", Collections.nCopies(eventIdList.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer eventId : eventIdList) {
|
||||
if (eventId != -1) {
|
||||
stmt.setInt(index++, eventId);
|
||||
}
|
||||
}
|
||||
ResultSet resultSet = stmt.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
groupIdList.add(resultSet.getInt("GROUP_ID"));
|
||||
}
|
||||
return groupIdList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getEventSourcesOfGroups(int groupId, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
List<String> eventSourceList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"EVENT_SOURCE " +
|
||||
"FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " +
|
||||
"WHERE G.EVENT_ID = E.ID " +
|
||||
"AND G.GROUP_ID = ? " +
|
||||
"AND E.TENANT_ID = ? " +
|
||||
"GROUP BY EVENT_SOURCE";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, groupId);
|
||||
stmt.setInt(2, tenantId);
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
while (rst.next()) {
|
||||
eventSourceList.add(rst.getString("EVENT_SOURCE"));
|
||||
}
|
||||
return eventSourceList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving event records of group " + groupId
|
||||
+ " and tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl.event;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractEventConfigDAO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class GenericEventConfigDAOImpl extends AbstractEventConfigDAO {
|
||||
private static final Log log = LogFactory.getLog(GenericEventConfigDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<Integer> storeEventRecords(List<EventConfig> eventConfigList, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_EVENT(" +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS, "+
|
||||
"CREATED_TIMESTAMP, " +
|
||||
"TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
for (EventConfig eventConfig : eventConfigList) {
|
||||
stmt.setString(1, eventConfig.getEventSource());
|
||||
stmt.setString(2, eventConfig.getEventLogic());
|
||||
stmt.setString(3, eventConfig.getActions());
|
||||
stmt.setTimestamp(4, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(5, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
int[] createdRowCount = stmt.executeBatch();
|
||||
List<Integer> generatedIds = new ArrayList<>();
|
||||
ResultSet generatedKeys = stmt.getGeneratedKeys();
|
||||
for (int i = 0; i < createdRowCount.length; i++) {
|
||||
if (generatedKeys.next()) {
|
||||
generatedIds.add(generatedKeys.getInt(1));
|
||||
}
|
||||
}
|
||||
return generatedIds;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event configurations for the tenant id " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl.event;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractEventConfigDAO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class H2EventConfigDAOImpl extends AbstractEventConfigDAO {
|
||||
private static final Log log = LogFactory.getLog(H2EventConfigDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<Integer> storeEventRecords(List<EventConfig> eventConfigList, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
List<Integer> generatedIds = new ArrayList<>();
|
||||
String sql = "INSERT INTO DM_DEVICE_EVENT(" +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS, "+
|
||||
"CREATED_TIMESTAMP, " +
|
||||
"TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
for (EventConfig eventConfig : eventConfigList) {
|
||||
stmt.setString(1, eventConfig.getEventSource());
|
||||
stmt.setString(2, eventConfig.getEventLogic());
|
||||
stmt.setString(3, eventConfig.getActions());
|
||||
stmt.setTimestamp(4, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(5, tenantId);
|
||||
int affectedRawCount = stmt.executeUpdate();
|
||||
if (affectedRawCount > 0) {
|
||||
ResultSet generatedKeys = stmt.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
generatedIds.add(generatedKeys.getInt(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return generatedIds;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event configurations for the tenant id " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dto.event.config;
|
||||
|
||||
public class GeoFenceGroupMap {
|
||||
private int fenceId;
|
||||
private int groupId;
|
||||
private String groupName;
|
||||
|
||||
public int getFenceId() {
|
||||
return fenceId;
|
||||
}
|
||||
|
||||
public void setFenceId(int fenceId) {
|
||||
this.fenceId = fenceId;
|
||||
}
|
||||
|
||||
public int getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(int groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof GeoFenceGroupMap) {
|
||||
GeoFenceGroupMap map = (GeoFenceGroupMap)obj;
|
||||
return map.getFenceId() == this.getFenceId()
|
||||
&& map.getGroupId() == this.getGroupId()
|
||||
&& map.getGroupName().equals(this.getGroupName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,238 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.event.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationProviderService;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class EventConfigurationProviderServiceImpl implements EventConfigurationProviderService {
|
||||
private static final Log log = LogFactory.getLog(EventConfigurationProviderServiceImpl.class);
|
||||
private final EventConfigDAO eventConfigDAO;
|
||||
|
||||
public EventConfigurationProviderServiceImpl() {
|
||||
eventConfigDAO = DeviceManagementDAOFactory.getEventConfigDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> createEventsOfDeviceGroup(List<EventConfig> eventConfigList, List<Integer> groupIds)
|
||||
throws EventConfigurationException {
|
||||
int tenantId;
|
||||
try {
|
||||
tenantId = DeviceManagementDAOUtil.getTenantId();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving tenant Id";
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
}
|
||||
|
||||
try {
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Creating event records of tenant " + tenantId);
|
||||
}
|
||||
List<Integer> generatedEventIds = eventConfigDAO.storeEventRecords(eventConfigList, tenantId);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Created events with event ids : " + generatedEventIds.toString());
|
||||
log.debug("Creating event group mapping for created events with group ids : " + groupIds.toString());
|
||||
}
|
||||
eventConfigDAO.addEventGroupMappingRecords(generatedEventIds, groupIds);
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Event configuration added successfully for the tenant " + tenantId);
|
||||
}
|
||||
return generatedEventIds;
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Failed to start/open transaction to store device event configurations";
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (EventManagementDAOException e) {
|
||||
String msg = "Error occurred while saving event records";
|
||||
log.error(msg, e);
|
||||
DeviceManagementDAOFactory.rollbackTransaction();
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> updateEventsOfDeviceGroup(List<EventConfig> newEventList, List<Integer> removedEventIdList,
|
||||
List<Integer> groupIds) throws EventConfigurationException {
|
||||
//todo when concerning about other event types, all of this steps might not necessary.
|
||||
// so divide them into separate service methods
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Updating event configurations of tenant");
|
||||
}
|
||||
List<EventConfig> eventsToAdd;
|
||||
try {
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
eventsToAdd = new ArrayList<>();
|
||||
List<EventConfig> eventsToUpdate = new ArrayList<>();
|
||||
List<Integer> updateEventIdList = new ArrayList<>();
|
||||
for (EventConfig newEvent : newEventList) {
|
||||
if (newEvent.getEventId() == -1) {
|
||||
eventsToAdd.add(newEvent);
|
||||
continue;
|
||||
}
|
||||
eventsToUpdate.add(newEvent);
|
||||
updateEventIdList.add(newEvent.getEventId());
|
||||
}
|
||||
List<Integer> savedGroups = eventConfigDAO.getGroupsOfEvents(updateEventIdList);
|
||||
List<Integer> groupIdsToAdd = new ArrayList<>();
|
||||
List<Integer> groupIdsToDelete = new ArrayList<>();
|
||||
for (Integer savedGroup : savedGroups) {
|
||||
if (!groupIds.contains(savedGroup)) {
|
||||
groupIdsToDelete.add(savedGroup);
|
||||
}
|
||||
}
|
||||
|
||||
for (Integer newGroupId : groupIds) {
|
||||
if (!savedGroups.contains(newGroupId)) {
|
||||
groupIdsToAdd.add(newGroupId);
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventsToUpdate.isEmpty()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Updating event records ");
|
||||
}
|
||||
eventConfigDAO.updateEventRecords(eventsToUpdate);
|
||||
}
|
||||
|
||||
|
||||
if (!groupIdsToDelete.isEmpty()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Deleting event group mapping records of groups");
|
||||
}
|
||||
eventConfigDAO.deleteEventGroupMappingRecordsByGroupIds(groupIdsToDelete);
|
||||
}
|
||||
|
||||
if (!groupIdsToAdd.isEmpty()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Creating event group mapping records for updated events");
|
||||
}
|
||||
eventConfigDAO.addEventGroupMappingRecords(updateEventIdList, groupIdsToAdd);
|
||||
}
|
||||
|
||||
if (!removedEventIdList.isEmpty()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Deleting event group mapping records of removing events");
|
||||
}
|
||||
eventConfigDAO.deleteEventGroupMappingRecordsByEventIds(removedEventIdList);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Deleting removed event records");
|
||||
}
|
||||
eventConfigDAO.deleteEventRecords(removedEventIdList);
|
||||
}
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Failed to start/open transaction to store device event configurations";
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (EventManagementDAOException e) {
|
||||
String msg = "Error occurred while saving event records";
|
||||
log.error(msg, e);
|
||||
DeviceManagementDAOFactory.rollbackTransaction();
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Adding new events while updating event");
|
||||
}
|
||||
return createEventsOfDeviceGroup(eventsToAdd, groupIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventConfig> getEvents(List<Integer> createdEventIds) throws EventConfigurationException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
return eventConfigDAO.getEventsById(createdEventIds);
|
||||
} catch (EventManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving event by IDs : " + Arrays.toString(createdEventIds.toArray());
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to open connection while retrieving event by IDs";
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getEventsSourcesOfGroup(int groupId, int tenantId) throws EventConfigurationException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
return eventConfigDAO.getEventSourcesOfGroups(groupId, tenantId);
|
||||
} catch (EventManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving events of group " + groupId + " and tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to open connection while retrieving event by IDs";
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEvents(List<EventConfig> events) throws EventConfigurationException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
Set<Integer> eventIdSet = new HashSet<>();
|
||||
for (EventConfig eventConfig : events) {
|
||||
eventIdSet.add(eventConfig.getEventId());
|
||||
}
|
||||
if (!eventIdSet.isEmpty()) {
|
||||
eventConfigDAO.deleteEventGroupMappingRecordsByEventIds(Lists.newArrayList(eventIdSet));
|
||||
eventConfigDAO.deleteEventRecords(Lists.newArrayList(eventIdSet));
|
||||
}
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Failed to start/open transaction to delete device event configurations";
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (EventManagementDAOException e) {
|
||||
DeviceManagementDAOFactory.rollbackTransaction();
|
||||
String msg = "Error occurred while deleting event records";
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.event.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventMetaData;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventOperation;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventRevokeOperation;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoFenceEventMeta;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
|
||||
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.geo.task.EventCreateCallback;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Event create/revoke operation creation task.
|
||||
* Use at the time of single event create, update, delete
|
||||
*/
|
||||
public class EventOperationExecutor implements Runnable {
|
||||
private static final Log log = LogFactory.getLog(EventOperationExecutor.class);
|
||||
|
||||
private final List<Integer> groupIds;
|
||||
private final String eventSource;
|
||||
private final EventMetaData eventMetaData;
|
||||
private final int tenantId;
|
||||
private final String operationCode;
|
||||
private final GroupManagementProviderService groupManagementService;
|
||||
private EventCreateCallback callback;
|
||||
|
||||
public EventOperationExecutor(EventMetaData eventMetaData, List<Integer> groupIds, int tenantId,
|
||||
String eventSource, String operationCode) {
|
||||
this.eventMetaData = eventMetaData;
|
||||
this.groupIds = groupIds;
|
||||
this.tenantId = tenantId;
|
||||
this.eventSource = eventSource;
|
||||
this.operationCode = operationCode;
|
||||
this.groupManagementService = DeviceManagementDataHolder.getInstance().getGroupManagementProviderService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("Starting event operation creation task");
|
||||
if (operationCode == null) {
|
||||
log.error("Failed to start event operation task. Operation code is null");
|
||||
return;
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.info("Starting " + operationCode + " operation creation task for event " + eventSource
|
||||
+ " tenant " + tenantId + " group Ids "+ Arrays.toString(groupIds.toArray()));
|
||||
}
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||
ProfileOperation operation = new ProfileOperation();
|
||||
operation.setType(Operation.Type.PROFILE);
|
||||
try {
|
||||
if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) {
|
||||
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG);
|
||||
buildEventConfigOperation(operation);
|
||||
} else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)){
|
||||
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_REVOKE);
|
||||
buildEventRevokeOperation(operation);
|
||||
}
|
||||
} catch (EventConfigurationException e) {
|
||||
log.error("Event creation failed with message : " + e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
Set<Device> devices = new HashSet<>();
|
||||
for (Integer groupId : groupIds) {
|
||||
DeviceGroup group;
|
||||
try {
|
||||
group = groupManagementService.getGroup(groupId, false);
|
||||
} catch (GroupManagementException e) {
|
||||
log.error("Failed to retrieve group with group ID " + groupId, e);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (group != null) {
|
||||
List<Device> allDevicesOfGroup = groupManagementService.getAllDevicesOfGroup(group.getName(), false);
|
||||
if (allDevicesOfGroup == null || allDevicesOfGroup.isEmpty()) {
|
||||
log.warn("No devices found in group " + group.getName());
|
||||
} else {
|
||||
devices.addAll(allDevicesOfGroup);
|
||||
}
|
||||
}
|
||||
} catch (GroupManagementException e) {
|
||||
log.error("Failed to retrieve devices of group with ID " + groupId + " and name " + group.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
if (devices.isEmpty()) {
|
||||
log.warn("No devices found for the specified groups " + Arrays.toString(groupIds.toArray()));
|
||||
return;
|
||||
}
|
||||
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
||||
for (Device device : devices) {
|
||||
if (device.getType().equalsIgnoreCase("android")) {
|
||||
//TODO introduce a proper mechanism for event handling for each device types
|
||||
deviceIdentifiers.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
|
||||
}
|
||||
}
|
||||
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
||||
.getInstance().getDeviceManagementProvider();
|
||||
try {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Creating event operations stared for devices" + Arrays.toString(deviceIdentifiers.toArray()));
|
||||
}
|
||||
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers);
|
||||
//TODO introduce a proper mechanism
|
||||
} catch (OperationManagementException e) {
|
||||
log.error("Creating event operation failed with error ", e);
|
||||
return;
|
||||
} catch (InvalidDeviceException e) {
|
||||
log.error("Creating event operation failed.\n" +
|
||||
"Could not found device/devices for the defined device identifiers.", e);
|
||||
return;
|
||||
}
|
||||
log.info("Event operation creation task completed");
|
||||
if (callback != null) {
|
||||
callback.onCompleteEventOperation(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build operation to create EVENT_REVOKE operation.
|
||||
* @param operation Operation object to build
|
||||
*/
|
||||
private void buildEventRevokeOperation(ProfileOperation operation) {
|
||||
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
||||
createGeoFenceRevokeOperation(operation);
|
||||
} //extend with another cases to handle other types of events
|
||||
}
|
||||
|
||||
/**
|
||||
* Build operation to create EVENT_CONFIG operation.
|
||||
* @param operation Operation object to build
|
||||
* @throws EventConfigurationException Failed while build the operation object
|
||||
*/
|
||||
private void buildEventConfigOperation(ProfileOperation operation) throws EventConfigurationException {
|
||||
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
||||
createGeoFenceConfigOperation(operation);
|
||||
} //extend with another cases to handle other types of events
|
||||
}
|
||||
|
||||
/**
|
||||
* Create EVENT_CONFIG operation object and attach payload to configure geo fence events
|
||||
* @param operation operation object to set the payload
|
||||
* @throws EventConfigurationException Failed while retrieving event list of geo fence
|
||||
*/
|
||||
private void createGeoFenceConfigOperation(ProfileOperation operation) throws EventConfigurationException {
|
||||
GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData;
|
||||
try {
|
||||
GeoLocationProviderService geoLocationProviderService = DeviceManagementDataHolder
|
||||
.getInstance().getGeoLocationProviderService();
|
||||
List<EventConfig> eventConfigList = geoLocationProviderService.getEventsOfGeoFence(geoFenceMeta.getId());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Retrieved event records of Geo Fence " + geoFenceMeta.getId() +
|
||||
". events " + Arrays.toString(eventConfigList.toArray()));
|
||||
}
|
||||
List<EventOperation> eventOperations = new ArrayList<>();
|
||||
EventOperation eventOperation = new EventOperation();
|
||||
eventOperation.setEventDefinition(eventMetaData);
|
||||
eventOperation.setEventSource(eventSource);
|
||||
eventOperation.setEventTriggers(eventConfigList);
|
||||
eventOperations.add(eventOperation);
|
||||
operation.setPayLoad(new Gson().toJson(eventOperations));
|
||||
}catch (GeoLocationBasedServiceException e) {
|
||||
throw new EventConfigurationException("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId()
|
||||
+ " : " + geoFenceMeta.getFenceName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create EVENT_REVOKE operation object and attach payload to configure geo fence events
|
||||
* @param operation operation object to set the payload
|
||||
*/
|
||||
private void createGeoFenceRevokeOperation(ProfileOperation operation) {
|
||||
GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData;
|
||||
EventRevokeOperation eventRevokeOperation = new EventRevokeOperation();
|
||||
eventRevokeOperation.setEventSource(eventSource);
|
||||
eventRevokeOperation.setId(geoFenceMeta.getId());
|
||||
operation.setPayLoad(new Gson().toJson(eventRevokeOperation));
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used to set the task callback to call after the task completed successfully
|
||||
* @param callback Event callback object implemented inside the task starting class
|
||||
*/
|
||||
public void setCallback(EventCreateCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
}
|
@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.event.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationProviderService;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventOperation;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventRevokeOperation;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoFenceEventMeta;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.geo.task.EventCreateCallback;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Event create/revoke operation creation task.
|
||||
* Use at the time of devices assign into group, remove from group.
|
||||
*/
|
||||
public class GroupEventOperationExecutor implements Runnable {
|
||||
private static final Log log = LogFactory.getLog(GroupEventOperationExecutor.class);
|
||||
|
||||
private final int groupId;
|
||||
private final List<DeviceIdentifier> deviceIdentifiers;
|
||||
private final int tenantId;
|
||||
private final String operationCode;
|
||||
private final GeoLocationProviderService geoLocationProviderService;
|
||||
private final EventConfigurationProviderService eventConfigurationService;
|
||||
private EventCreateCallback callback;
|
||||
|
||||
public GroupEventOperationExecutor(int groupId, List<DeviceIdentifier> deviceIdentifiers, int tenantId, String operationCode) {
|
||||
this.groupId = groupId;
|
||||
this.deviceIdentifiers = deviceIdentifiers;
|
||||
this.tenantId = tenantId;
|
||||
this.operationCode = operationCode;
|
||||
this.geoLocationProviderService = DeviceManagementDataHolder.getInstance().getGeoLocationProviderService();
|
||||
this.eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("Starting event operation creation task for devices in group " + groupId + " tenant " + tenantId);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Event creation operation started for devices with IDs " + Arrays.toString(deviceIdentifiers.toArray()));
|
||||
log.debug("Starting tenant flow for tenant with ID : " + tenantId);
|
||||
}
|
||||
ProfileOperation operation = new ProfileOperation();
|
||||
if (operationCode != null) {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||
try {
|
||||
operation.setCode(operationCode);
|
||||
operation.setType(Operation.Type.PROFILE);
|
||||
if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) {
|
||||
buildEventConfigOperationObject(operation);
|
||||
} else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)) {
|
||||
buildEventRevokeOperation(operation);
|
||||
}
|
||||
|
||||
} catch (EventConfigurationException e) {
|
||||
log.error("Failed to retrieve event sources of group " + groupId + ". Event creation operation failed.", e);
|
||||
return;
|
||||
}
|
||||
|
||||
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
||||
.getInstance().getDeviceManagementProvider();
|
||||
try {
|
||||
if (!deviceIdentifiers.isEmpty()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Creating event operations stared");
|
||||
}
|
||||
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers); //TODO introduce a proper mechanism
|
||||
} else {
|
||||
log.info("Device identifiers are empty, Hence ignoring adding event operation");
|
||||
}
|
||||
} catch (OperationManagementException e) {
|
||||
log.error("Creating event operation failed with error ", e);
|
||||
} catch (InvalidDeviceException e) {
|
||||
log.error("Creating event operation failed.\n" +
|
||||
"Could not found device/devices for the defined device identifiers.", e);
|
||||
}
|
||||
log.info("Event operation creation succeeded");
|
||||
if (callback != null) {
|
||||
callback.onCompleteEventOperation(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Build EVENT_REVOKE operation attaching event payload
|
||||
* @param operation operation object to build
|
||||
* @throws EventConfigurationException if not events found for the specific group
|
||||
*/
|
||||
private void buildEventRevokeOperation(ProfileOperation operation) throws EventConfigurationException {
|
||||
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
||||
if (eventSources == null || eventSources.isEmpty()) {
|
||||
String msg = "No events applied for queried group with ID " + groupId;
|
||||
log.info(msg);
|
||||
throw new EventConfigurationException(msg);
|
||||
}
|
||||
for (String eventSource : eventSources) {
|
||||
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
||||
setGeoFenceRevokeOperationContent(operation);
|
||||
} //extend with another cases to handle other types of events
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build EVENT_CONFIG operation attaching event payload
|
||||
* @param operation operation object to build
|
||||
* @throws EventConfigurationException if not events found for the specific group
|
||||
*/
|
||||
private void buildEventConfigOperationObject(ProfileOperation operation) throws EventConfigurationException {
|
||||
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
||||
if (eventSources == null || eventSources.isEmpty()) {
|
||||
String msg = "No events applied for queried group with ID " + groupId;
|
||||
log.info(msg);
|
||||
throw new EventConfigurationException(msg);
|
||||
}
|
||||
for (String eventSource : eventSources) {
|
||||
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
||||
setGeoFenceConfigOperationContent(operation);
|
||||
} //extend with another cases to handle other types of events
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set operation payload GeoFence for EVENT_CONFIG operation
|
||||
* @param operation operation object to attach payload
|
||||
* @throws EventConfigurationException failed while getting events of group
|
||||
*/
|
||||
private void setGeoFenceConfigOperationContent(ProfileOperation operation) throws EventConfigurationException {
|
||||
log.info("Geo fence events found attached with group " + groupId + ", Started retrieving geo fences");
|
||||
List<GeofenceData> geoFencesOfGroup = getGeoFencesOfGroup();
|
||||
List<EventOperation> eventOperationList = new ArrayList<>();
|
||||
for (GeofenceData geofenceData : geoFencesOfGroup) {
|
||||
GeoFenceEventMeta geoFenceEventMeta = new GeoFenceEventMeta(geofenceData);
|
||||
EventOperation eventOperation = new EventOperation();
|
||||
eventOperation.setEventDefinition(geoFenceEventMeta);
|
||||
eventOperation.setEventSource(DeviceManagementConstants.EventServices.GEOFENCE);
|
||||
eventOperation.setEventTriggers(geofenceData.getEventConfig());
|
||||
eventOperationList.add(eventOperation);
|
||||
}
|
||||
operation.setPayLoad(new Gson().toJson(eventOperationList));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get geo fence list of the group
|
||||
* @return {@link GeofenceData} geofence data list of the geo fence
|
||||
* @throws EventConfigurationException error occurred while querying geo fences of group
|
||||
*/
|
||||
private List<GeofenceData> getGeoFencesOfGroup() throws EventConfigurationException {
|
||||
List<GeofenceData> geoFencesOfGroup;
|
||||
try {
|
||||
geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
||||
} catch (GeoLocationBasedServiceException e) {
|
||||
String msg = "Failed to get geo fences of the group";
|
||||
log.error(msg, e);
|
||||
throw new EventConfigurationException(msg, e);
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Retrieved " + geoFencesOfGroup.size() + " geo fences defined for the group " + groupId);
|
||||
}
|
||||
return geoFencesOfGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set operation payload GeoFence for EVENT_REVOKE operation
|
||||
* @param operation operation object to attach payload
|
||||
* @throws EventConfigurationException failed while getting events of group
|
||||
*/
|
||||
private void setGeoFenceRevokeOperationContent(ProfileOperation operation) throws EventConfigurationException {
|
||||
List<GeofenceData> geoFencesOfGroup = getGeoFencesOfGroup();
|
||||
List<EventRevokeOperation> revokeOperationList = new ArrayList<>();
|
||||
for (GeofenceData geofenceData : geoFencesOfGroup) {
|
||||
EventRevokeOperation eventRevokeOperation = new EventRevokeOperation();
|
||||
eventRevokeOperation.setEventSource(DeviceManagementConstants.EventServices.GEOFENCE);
|
||||
eventRevokeOperation.setId(geofenceData.getId());
|
||||
revokeOperationList.add(eventRevokeOperation);
|
||||
}
|
||||
operation.setPayLoad(new Gson().toJson(revokeOperationList));
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used to set the task callback to call after the task completed successfully
|
||||
* @param callback Event callback object implemented inside the task starting class
|
||||
*/
|
||||
public void setCallback(EventCreateCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.geo.task;
|
||||
public interface EventCreateCallback {
|
||||
void onCompleteEventOperation(Object values);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.geo.task;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoFenceEventMeta;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
import org.wso2.carbon.device.mgt.core.event.config.GroupEventOperationExecutor;
|
||||
import org.wso2.carbon.device.mgt.core.event.config.EventOperationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Responsible for Event operation task creation management.
|
||||
* Wrap event operation executor creation
|
||||
*/
|
||||
public class GeoFenceEventOperationManager {
|
||||
private final int tenantId;
|
||||
private final String eventOperationCode;
|
||||
private final EventCreateCallback callback;
|
||||
|
||||
public GeoFenceEventOperationManager(String eventOperationCode, int tenantId, EventCreateCallback callback) {
|
||||
this.eventOperationCode = eventOperationCode;
|
||||
this.tenantId = tenantId;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get executor for create EVENT_CONFIG / EVENT_REVOKE operations at the time of a geofence
|
||||
* created, updated or deleted
|
||||
* @param geofenceData created geofence data object
|
||||
* @return {@link EventOperationExecutor} Created executor to create operations
|
||||
*/
|
||||
public EventOperationExecutor getEventOperationExecutor(GeofenceData geofenceData) {
|
||||
GeoFenceEventMeta geoFenceEventMeta = new GeoFenceEventMeta(geofenceData);
|
||||
EventOperationExecutor executor = new EventOperationExecutor(geoFenceEventMeta, geofenceData.getGroupIds(),
|
||||
tenantId, DeviceManagementConstants.EventServices.GEOFENCE, eventOperationCode);
|
||||
executor.setCallback(callback);
|
||||
return executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get executor for create EVENT_CONFIG / EVENT_REVOKE operations at the time of a device/s
|
||||
* assigned into a group or removed from a group
|
||||
* @param groupId Id of the assigned / removed group
|
||||
* @param deviceIdentifiers Device identifiers assigned to / removed from the group
|
||||
* @return {@link GroupEventOperationExecutor} Created executor to create operations
|
||||
*/
|
||||
public GroupEventOperationExecutor getEventOperationExecutor(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
|
||||
GroupEventOperationExecutor executor = new GroupEventOperationExecutor(groupId, deviceIdentifiers, tenantId, eventOperationCode);
|
||||
executor.setCallback(callback);
|
||||
return executor;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue