This allowing users to configure task frequencies independently for each tenant.pull/370/head
parent
d050736aaf
commit
03f95e8cdf
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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 io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DeviceFrequencyMap extends HashMap<String, DeviceFrequencyMap.Device> {
|
||||
public static class Device {
|
||||
private int frequency;
|
||||
|
||||
public int getFrequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
public void setFrequency(int frequency) {
|
||||
this.frequency = frequency;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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 io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
|
||||
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
|
||||
|
||||
public interface DeviceTaskConfigurationService {
|
||||
|
||||
/**
|
||||
* This method is useful to add Default Task Frequency from device type xml
|
||||
*
|
||||
* @throws MetadataManagementException if error while adding default TaskFrequency
|
||||
*/
|
||||
void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to add custom Task Frequency for device types
|
||||
*
|
||||
* @throws MetadataManagementException if error while adding default TaskFrequency
|
||||
*/
|
||||
void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to get existing Task Frequency by device type and tenant id
|
||||
*
|
||||
* @throws MetadataManagementException if error while getting existing TaskFrequency
|
||||
*/
|
||||
int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException;
|
||||
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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 io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceFrequencyMap;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurationService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceTaskConfigurationServiceImpl.class);
|
||||
|
||||
private final MetadataDAO metadataDAO;
|
||||
|
||||
public DeviceTaskConfigurationServiceImpl() {
|
||||
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
|
||||
}
|
||||
|
||||
private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
|
||||
metadataDAO.addMetadata(tenantId, metadata);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(key + " metadata entry has been inserted successfully");
|
||||
}
|
||||
}
|
||||
|
||||
private Metadata constructTaskFrequencyMetadata(Map<String, Integer> deviceFrequencies) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
for (Map.Entry<String, Integer> entry : deviceFrequencies.entrySet()) {
|
||||
String deviceType = entry.getKey();
|
||||
int frequency = entry.getValue();
|
||||
// Create a JsonObject for the current device type
|
||||
JsonObject deviceObject = new JsonObject();
|
||||
deviceObject.addProperty("frequency", String.valueOf(frequency));
|
||||
jsonObject.add(deviceType, deviceObject);
|
||||
}
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setMetaKey(MetadataConstants.DEVICE_TASK_FREQUENCY);
|
||||
metadata.setMetaValue(jsonObject.toString());
|
||||
return metadata;
|
||||
}
|
||||
|
||||
|
||||
private int getDefaultTaskFrequency(DeviceManagementProviderService dms, String deviceType) {
|
||||
OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getDeviceMonitoringConfig(deviceType);
|
||||
return operationMonitoringTaskConfig.getFrequency();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException {
|
||||
try {
|
||||
Map<String, Integer> deviceFrequencies = new HashMap<>();
|
||||
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
||||
List<DeviceType> deviceTypes = dms.getDeviceTypes();
|
||||
for (DeviceType deviceType : deviceTypes) {
|
||||
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(dms, deviceType.getName()));
|
||||
}
|
||||
MetadataManagementDAOFactory.beginTransaction();
|
||||
if (!metadataDAO.isExist(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY)) {
|
||||
Metadata defaultTaskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies);
|
||||
// Add default TaskFrequency
|
||||
addMetadataEntry(tenantId, defaultTaskFrequencyMetadata, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
||||
}
|
||||
MetadataManagementDAOFactory.commitTransaction();
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
MetadataManagementDAOFactory.rollbackTransaction();
|
||||
String msg = "Error occurred while inserting default task frequency metadata entry.";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while trying to obtain device types.";
|
||||
log.error(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException {
|
||||
try {
|
||||
Map<String, Integer> deviceFrequencies = new HashMap<>();
|
||||
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
||||
List<DeviceType> deviceTypes = dms.getDeviceTypes();
|
||||
for (DeviceType deviceType : deviceTypes) {
|
||||
deviceFrequencies.put(deviceType.getName(), frequency);
|
||||
}
|
||||
MetadataManagementDAOFactory.beginTransaction();
|
||||
if (!metadataDAO.isExist(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY)) {
|
||||
Metadata defaultTaskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies);
|
||||
// Add Task Frequency entry for given Frequency
|
||||
addMetadataEntry(tenantId, defaultTaskFrequencyMetadata, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
||||
}
|
||||
MetadataManagementDAOFactory.commitTransaction();
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
MetadataManagementDAOFactory.rollbackTransaction();
|
||||
String msg = "Error occurred while inserting default task frequency metadata entry.";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while trying to obtain device types.";
|
||||
log.error(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException {
|
||||
try {
|
||||
MetadataManagementDAOFactory.openConnection();
|
||||
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
||||
String metaValue = metadata.getMetaValue();
|
||||
Gson gson = new Gson();
|
||||
DeviceFrequencyMap deviceFrequencyMap = gson.fromJson(metaValue, DeviceFrequencyMap.class);
|
||||
// Check if the device type exists in the map
|
||||
if (deviceFrequencyMap.containsKey(deviceType)) {
|
||||
// Retrieve the frequency for the given device type
|
||||
return deviceFrequencyMap.get(deviceType).getFrequency();
|
||||
} else {
|
||||
throw new MetadataManagementException("Device type not found: " + deviceType);
|
||||
}
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving device task frequency metadata for tenant:" + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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 io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
|
||||
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.core.ServerStartupObserver;
|
||||
import org.wso2.carbon.user.api.Tenant;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||
|
||||
public class TaskConfigurationStartupHandler implements ServerStartupObserver {
|
||||
|
||||
private static final Log log = LogFactory.getLog(TaskConfigurationStartupHandler.class);
|
||||
|
||||
@Override
|
||||
public void completingServerStartup() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completedServerStartup() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
|
||||
RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Server has just started, hence started adding default task frequencies");
|
||||
}
|
||||
try {
|
||||
deviceTaskConfigurationService.addDefaultTaskFrequency(MultitenantConstants.SUPER_TENANT_ID);
|
||||
Tenant[] tenantArray = realmService.getTenantManager().getAllTenants();
|
||||
if (tenantArray != null) {
|
||||
for (Tenant tenant : tenantArray) {
|
||||
deviceTaskConfigurationService.addDefaultTaskFrequency(tenant.getId());
|
||||
}
|
||||
}
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while adding default task frequency metadata entry.";
|
||||
log.error(msg, e);
|
||||
} catch (UserStoreException e) {
|
||||
log.error("Error occurred while trying to get the available tenants.", e);
|
||||
}
|
||||
log.info("Finish adding default task frequencies");
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue