From 125ee626d9248be84b1fdbd7e4b43c0e3cf0c076 Mon Sep 17 00:00:00 2001 From: Amalka Subasinghe Date: Fri, 30 Jun 2023 12:04:07 +0530 Subject: [PATCH] improve code to use service call for operations --- .../device/AddOperationFunctionProcessor.java | 38 ++++--------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/components/extensions/siddhi-extensions/io.entgra.device.mgt.plugins.extension.siddhi.device/src/main/java/io/entgra/device/mgt/plugins/extension/siddhi/device/AddOperationFunctionProcessor.java b/components/extensions/siddhi-extensions/io.entgra.device.mgt.plugins.extension.siddhi.device/src/main/java/io/entgra/device/mgt/plugins/extension/siddhi/device/AddOperationFunctionProcessor.java index 1c0d30c2f..81a9b127b 100644 --- a/components/extensions/siddhi-extensions/io.entgra.device.mgt.plugins.extension.siddhi.device/src/main/java/io/entgra/device/mgt/plugins/extension/siddhi/device/AddOperationFunctionProcessor.java +++ b/components/extensions/siddhi-extensions/io.entgra.device.mgt.plugins.extension.siddhi.device/src/main/java/io/entgra/device/mgt/plugins/extension/siddhi/device/AddOperationFunctionProcessor.java @@ -18,23 +18,13 @@ package io.entgra.device.mgt.plugins.extension.siddhi.device; -import feign.Feign; -import feign.Logger; -import feign.gson.GsonDecoder; -import feign.gson.GsonEncoder; -import feign.jaxrs.JAXRSContract; -import feign.okhttp.OkHttpClient; -import feign.slf4j.Slf4jLogger; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; +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.utils.DeviceUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.exception.ExecutionPlanRuntimeException; 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 String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; 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 @@ -138,9 +117,9 @@ public class AddOperationFunctionProcessor extends StreamFunctionProcessor { JSONArray deviceIds = new JSONArray((String) data[0]); String deviceType = (String) data[1]; - List deviceIdentifiers = new ArrayList<>(); + List deviceIdentifiers = new ArrayList<>(); 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(); @@ -154,11 +133,8 @@ public class AddOperationFunctionProcessor extends StreamFunctionProcessor { String date = new SimpleDateFormat(DATE_FORMAT_NOW).format(new Date()); operation.setCreatedTimeStamp(date); - OperationRequest operationRequest = new OperationRequest(); - operationRequest.setDeviceIdentifiers(deviceIdentifiers); - operationRequest.setOperation(operation); try { - Activity activity = operationService.addOperation(deviceType, operationRequest); + Activity activity = DeviceUtils.getDeviceManagementProviderService().addOperation(deviceType, operation, deviceIdentifiers); return new Object[]{activity.getActivityId()}; } catch (Exception e) { log.error("Error occurred while adding the operation " + operation.toString(), e);