improve code to use service call for operations

synced-apim420
Amalka Subasinghe 1 year ago
parent d7e4d7c9ac
commit 125ee626d9

@ -18,23 +18,13 @@
package io.entgra.device.mgt.plugins.extension.siddhi.device; package io.entgra.device.mgt.plugins.extension.siddhi.device;
import feign.Feign; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import feign.Logger; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity;
import feign.gson.GsonDecoder; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Operation;
import feign.gson.GsonEncoder; import io.entgra.device.mgt.plugins.extension.siddhi.device.utils.DeviceUtils;
import feign.jaxrs.JAXRSContract;
import feign.okhttp.OkHttpClient;
import feign.slf4j.Slf4jLogger;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray; import org.json.JSONArray;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Operation;
import io.entgra.device.mgt.plugins.extension.siddhi.device.client.OAuthRequestInterceptor;
import io.entgra.device.mgt.plugins.extension.siddhi.device.client.configs.SiddhiExtensionConfigReader;
import io.entgra.device.mgt.plugins.extension.siddhi.device.client.dto.OperationRequest;
import io.entgra.device.mgt.plugins.extension.siddhi.device.client.services.OperationService;
import io.entgra.device.mgt.plugins.extension.siddhi.device.utils.ClientUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext; import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException; import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor; import org.wso2.siddhi.core.executor.ExpressionExecutor;
@ -53,17 +43,6 @@ public class AddOperationFunctionProcessor extends StreamFunctionProcessor {
private static final Log log = LogFactory.getLog(AddOperationFunctionProcessor.class); private static final Log log = LogFactory.getLog(AddOperationFunctionProcessor.class);
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
private static final String DEVICE_MGT_BASE_CONTEXT = "/api/device-mgt/v1.0"; private static final String DEVICE_MGT_BASE_CONTEXT = "/api/device-mgt/v1.0";
private OperationService operationService;
public AddOperationFunctionProcessor() {
operationService = Feign.builder().client(new OkHttpClient(ClientUtils.getSSLClient()))
.logger(new Slf4jLogger())
.logLevel(Logger.Level.FULL).requestInterceptor(new OAuthRequestInterceptor())
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
.target(OperationService.class, ClientUtils.replaceProperties(
SiddhiExtensionConfigReader.getInstance().getConfig().getGatewayEndpoint() +
DEVICE_MGT_BASE_CONTEXT));
}
/** /**
* The init method of the StreamProcessor, this method will be called before other methods * The init method of the StreamProcessor, this method will be called before other methods
@ -138,9 +117,9 @@ public class AddOperationFunctionProcessor extends StreamFunctionProcessor {
JSONArray deviceIds = new JSONArray((String) data[0]); JSONArray deviceIds = new JSONArray((String) data[0]);
String deviceType = (String) data[1]; String deviceType = (String) data[1];
List<String> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (int i = 0; i < deviceIds.length(); i++) { for (int i = 0; i < deviceIds.length(); i++) {
deviceIdentifiers.add(deviceIds.getString(i)); deviceIdentifiers.add(new DeviceIdentifier(deviceIds.getString(i), deviceType));
} }
Operation operation = new Operation(); Operation operation = new Operation();
@ -154,11 +133,8 @@ public class AddOperationFunctionProcessor extends StreamFunctionProcessor {
String date = new SimpleDateFormat(DATE_FORMAT_NOW).format(new Date()); String date = new SimpleDateFormat(DATE_FORMAT_NOW).format(new Date());
operation.setCreatedTimeStamp(date); operation.setCreatedTimeStamp(date);
OperationRequest operationRequest = new OperationRequest();
operationRequest.setDeviceIdentifiers(deviceIdentifiers);
operationRequest.setOperation(operation);
try { try {
Activity activity = operationService.addOperation(deviceType, operationRequest); Activity activity = DeviceUtils.getDeviceManagementProviderService().addOperation(deviceType, operation, deviceIdentifiers);
return new Object[]{activity.getActivityId()}; return new Object[]{activity.getActivityId()};
} catch (Exception e) { } catch (Exception e) {
log.error("Error occurred while adding the operation " + operation.toString(), e); log.error("Error occurred while adding the operation " + operation.toString(), e);

Loading…
Cancel
Save