forked from community/device-mgt-core
parent
d050736aaf
commit
174b10a4d6
@ -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
|
||||
*
|
||||
* @throws MetadataManagementException if error while adding default TaskFrequency
|
||||
*/
|
||||
void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to reset existing TaskFrequency to default values in xml
|
||||
*
|
||||
* @throws MetadataManagementException if error while resetting default TaskFrequency
|
||||
*/
|
||||
void resetToDefaultTaskFrequency() throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to get existing TaskFrequency 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,164 @@
|
||||
/*
|
||||
* 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(String deviceType) {
|
||||
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
||||
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();
|
||||
log.error("addDefaultTaskFrequency");
|
||||
for (DeviceType deviceType : deviceTypes) {
|
||||
log.error(deviceType.getName());
|
||||
log.error(getDefaultTaskFrequency(deviceType.getName()));
|
||||
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(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) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetToDefaultTaskFrequency() throws MetadataManagementException {
|
||||
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
if(metaValue == null){
|
||||
addDefaultTaskFrequency(tenantId);
|
||||
}
|
||||
|
||||
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,58 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, 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 org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.core.ServerStartupObserver;
|
||||
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() {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Server has just started, hence started publishing unpublished APIs");
|
||||
log.debug("Total number of unpublished APIs: ");
|
||||
}
|
||||
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
|
||||
try {
|
||||
deviceTaskConfigurationService.addDefaultTaskFrequency(MultitenantConstants.SUPER_TENANT_ID);
|
||||
} catch (MetadataManagementException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("Finish Adding default task");
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue