diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/pom.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/pom.xml similarity index 98% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/pom.xml rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/pom.xml index 980599638a..ba0ee40585 100644 --- a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/pom.xml +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/pom.xml @@ -7,10 +7,10 @@ ../pom.xml 4.0.0 - org.wso2.carbon.device.mgt.iot.droneanalyzer.api + org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api 1.9.2-SNAPSHOT war - WSO2 Carbon - IoT Server Drone Analyzer API + WSO2 Carbon - IoT Server Drone Analyzer Controller API http://maven.apache.org diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java similarity index 96% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java index f7b3ec854b..6f03d3b283 100644 --- a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneRealTimeService.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.iot.droneanalyzer.service; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConstants; import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.transport.DroneAnalyzerXMPPConnector; @@ -47,11 +46,6 @@ public class DroneRealTimeService { } } - - /** - * - * @param session - */ @OnOpen public void onOpen(Session session){ log.info(session.getId() + " has opened a connection"); diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java new file mode 100644 index 0000000000..25b389bfc3 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java @@ -0,0 +1,227 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service; + +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.device.DeviceType; +import org.wso2.carbon.apimgt.annotations.device.feature.Feature; +import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil; +import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; +import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; +import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.iot.DeviceManagement; +import org.wso2.carbon.device.mgt.iot.DeviceValidator; +import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo; +import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppAccount; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppServerClient; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConstants; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.controller.DroneController; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.controller.impl.DroneControllerImpl; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.exception.DroneAnalyzerException; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.util.DroneAnalyzerServiceUtils; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.util.scep.ContentType; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.util.scep.SCEPOperation; +import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException; +import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; +import org.wso2.carbon.device.mgt.iot.util.ZipArchive; +import org.wso2.carbon.device.mgt.iot.util.ZipUtil; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +@API( name="drone_analyzer", version="1.0.0", context="/drone_analyzer") +@DeviceType( value = "drone_analyzer") +public class DroneService { + + private static org.apache.commons.logging.Log log = LogFactory.getLog(DroneService.class); + private static final String SUPER_TENANT = "carbon.super"; + @Context //injected response proxy supporting multiple thread + private HttpServletResponse response; + private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); + private DroneController droneController = new DroneControllerImpl(); + + /* --------------------------------------------------------------------------------------- + Device specific APIs - Control APIs + Data-Publishing APIs + Also contains utility methods required for the execution of these APIs + --------------------------------------------------------------------------------------- */ + @Path("controller/register/{owner}/{deviceId}/{ip}/{port}") + @POST + public String registerDeviceIP(@PathParam("owner") String owner, @PathParam("deviceId") String deviceId, + @PathParam("ip") String deviceIP, + @PathParam("port") String devicePort, + @Context HttpServletResponse response, + @Context HttpServletRequest request) { + String result; + log.info("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId + + " of owner: " + owner); + String deviceHttpEndpoint = deviceIP + ":" + devicePort; + deviceToIpMap.put(deviceId, deviceHttpEndpoint); + result = "Device-IP Registered"; + response.setStatus(Response.Status.OK.getStatusCode()); + if (log.isDebugEnabled()) { + log.debug(result); + } + log.info(owner + deviceId + deviceIP + devicePort ); + return result; + + } + + @Path("controller/send_command") + @POST + @Feature( code="send_command", name="Send Command", type="operation", + description="Send Commands to Drone") + public Response droneController(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, + @QueryParam("action") String action, @QueryParam("duration") String duration, + @QueryParam("speed") String speed){ + try { + DeviceValidator deviceValidator = new DeviceValidator(); + if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, + DroneConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } + } catch (DeviceManagementException e) { + log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } + try { + DroneAnalyzerServiceUtils.sendControlCommand(droneController, deviceId, action, Double.valueOf(speed), + Double.valueOf(duration)); + return Response.status(Response.Status.ACCEPTED).build(); + + } catch (DeviceManagementException e) { + log.error("Drone command didn't success. Try again, \n"+ e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + } + + @GET + @Path("controller/scep") + public Response scepRequest(@QueryParam("operation") String operation, @QueryParam("message") String message) { + if (log.isDebugEnabled()) { + log.debug("Invoking SCEP operation " + operation); + } + if (SCEPOperation.GET_CA_CERT.getValue().equals(operation)) { + if (log.isDebugEnabled()) { + log.debug("Invoking GetCACert"); + } + try { + CertificateManagementService certificateManagementService = + DroneAnalyzerServiceUtils.getCertificateManagementService(); + SCEPResponse scepResponse = certificateManagementService.getCACertSCEP(); + Response.ResponseBuilder responseBuilder; + switch (scepResponse.getResultCriteria()) { + case CA_CERT_FAILED: + log.error("CA cert failed"); + responseBuilder = Response.serverError(); + break; + case CA_CERT_RECEIVED: + + if (log.isDebugEnabled()) { + log.debug("CA certificate received in GetCACert"); + } + responseBuilder = Response.ok(scepResponse.getEncodedResponse(), + ContentType.X_X509_CA_CERT); + break; + case CA_RA_CERT_RECEIVED: + if (log.isDebugEnabled()) { + log.debug("CA and RA certificates received in GetCACert"); + } + responseBuilder = Response.ok(scepResponse.getEncodedResponse(), + ContentType.X_X509_CA_RA_CERT); + break; + default: + log.error("Invalid SCEP request"); + responseBuilder = Response.serverError(); + break; + } + return responseBuilder.build(); + } catch (DroneAnalyzerException e) { + log.error("Error occurred while enrolling the drone device", e); + } catch (KeystoreException e) { + log.error("Keystore error occurred while enrolling the drone device", e); + } + + } else if (SCEPOperation.GET_CA_CAPS.getValue().equals(operation)) { + + if (log.isDebugEnabled()) { + log.debug("Invoking GetCACaps"); + } + + try { + CertificateManagementService certificateManagementService = DroneAnalyzerServiceUtils. + getCertificateManagementService(); + byte caCaps[] = certificateManagementService.getCACapsSCEP(); + return Response.ok(caCaps, MediaType.TEXT_PLAIN).build(); + } catch (DroneAnalyzerException e) { + log.error("Error occurred while enrolling the device", e); + } + + } else { + log.error("Invalid SCEP operation " + operation); + } + + return Response.serverError().build(); + } + + @POST + @Path("controller/scep") + public Response scepRequestPost(@QueryParam("operation") String operation, InputStream inputStream) { + + if (log.isDebugEnabled()) { + log.debug("Invoking SCEP operation " + operation); + } + + if (SCEPOperation.PKI_OPERATION.getValue().equals(operation)) { + + if (log.isDebugEnabled()) { + log.debug("Invoking PKIOperation"); + } + try { + CertificateManagementService certificateManagementService = DroneAnalyzerServiceUtils. + getCertificateManagementService(); + byte pkiMessage[] = certificateManagementService.getPKIMessageSCEP(inputStream); + return Response.ok(pkiMessage, ContentType.X_PKI_MESSAGE).build(); + + } catch (DroneAnalyzerException e) { + log.error("Error occurred while enrolling the device", e); + } catch (KeystoreException e) { + log.error("Keystore error occurred while enrolling the device", e); + } + } + return Response.serverError().build(); + } +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/META-INF/resources.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/META-INF/resources.xml similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/META-INF/resources.xml rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/META-INF/resources.xml diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/META-INF/webapp-classloading.xml similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/META-INF/webapp-classloading.xml rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/META-INF/webapp-classloading.xml diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml new file mode 100644 index 0000000000..643c0896f5 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..2a9646df69 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,49 @@ + + + WSO2 IoT Server + WSO2 IoT Server + + CXFServlet + org.apache.cxf.transport.servlet.CXFServlet + 1 + + + CXFServlet + /* + + + + isAdminService + false + + + doAuthentication + false + + + + + managed-api-enabled + true + + + managed-api-owner + admin + + + managed-api-context-template + /drone_analyzer/{version} + + + managed-api-application + drone_analyzer + + + managed-api-isSecured + true + + diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/pom.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/pom.xml new file mode 100644 index 0000000000..a391ad70be --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/pom.xml @@ -0,0 +1,253 @@ + + + device-mgt-iot-droneanalyzer + org.wso2.carbon.devicemgt-plugins + 1.9.2-SNAPSHOT + ../pom.xml + + 4.0.0 + org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api + 1.9.2-SNAPSHOT + war + WSO2 Carbon - IoT Server Drone Analyzer Manager API + http://maven.apache.org + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + provided + + + org.apache.axis2.wso2 + axis2-client + + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.analytics + provided + + + org.apache.axis2.wso2 + axis2-client + + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.certificate.mgt.core + provided + + + commons-codec.wso2 + commons-codec + + + + + + + + org.apache.cxf + cxf-rt-frontend-jaxws + provided + + + org.apache.cxf + cxf-rt-frontend-jaxrs + provided + + + org.apache.cxf + cxf-rt-transports-http + provided + + + + + org.eclipse.paho + mqtt-client + provided + + + + + org.springframework + spring-context + + + + + org.apache.httpcomponents + httpasyncclient + 4.1 + provided + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot + provided + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.droneanalyzer + provided + + + + + org.codehaus.jackson + jackson-core-asl + + + org.codehaus.jackson + jackson-jaxrs + + + javax + javaee-web-api + provided + + + javax.ws.rs + jsr311-api + provided + + + commons-httpclient.wso2 + commons-httpclient + provided + + + + org.wso2.carbon + org.wso2.carbon.utils + provided + + + org.bouncycastle.wso2 + bcprov-jdk15on + + + org.wso2.carbon + org.wso2.carbon.user.api + + + org.wso2.carbon + org.wso2.carbon.queuing + + + org.wso2.carbon + org.wso2.carbon.base + + + org.apache.axis2.wso2 + axis2 + + + org.igniterealtime.smack.wso2 + smack + + + org.igniterealtime.smack.wso2 + smackx + + + jaxen + jaxen + + + commons-fileupload.wso2 + commons-fileupload + + + org.apache.ant.wso2 + ant + + + org.apache.ant.wso2 + ant + + + commons-httpclient.wso2 + commons-httpclient + + + org.eclipse.equinox + javax.servlet + + + org.wso2.carbon + org.wso2.carbon.registry.api + + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.webapp.publisher + provided + + + + commons-codec + commons-codec + + + + org.igniterealtime.smack.wso2 + smack + provided + + + org.igniterealtime.smack.wso2 + smackx + provided + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.annotations + provided + + + + + + + + + + maven-compiler-plugin + + UTF-8 + ${wso2.maven.compiler.source} + ${wso2.maven.compiler.target} + + + + maven-war-plugin + + src/main/webapp/WEB-INF/web.xml + drone_analyzer_mgt + + + + + diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java similarity index 86% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java index 96ea3bce4d..3327442dc2 100644 --- a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/DroneService.java @@ -63,7 +63,6 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -@API( name="drone_analyzer", version="1.0.0", context="/drone_analyzer") @DeviceType( value = "drone_analyzer") public class DroneService { @@ -73,7 +72,6 @@ public class DroneService { private HttpServletResponse response; private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); private DroneController droneController = new DroneControllerImpl(); - /* --------------------------------------------------------------------------------------- Device management specific APIs Also contains utility methods required for the execution of these APIs @@ -228,15 +226,12 @@ public class DroneService { if (owner == null) { return Response.status(400).build();//bad request } - //create new device id String deviceId = shortUUID(); - //create token String token = UUID.randomUUID().toString(); String refreshToken = UUID.randomUUID().toString(); //adding registering data - boolean status = register(deviceId, deviceName, owner); if (!status) { return Response.status(500).entity( @@ -244,7 +239,6 @@ public class DroneService { + " owner:" + owner).build(); } - ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile; try { @@ -253,7 +247,6 @@ public class DroneService { } catch (DeviceManagementException ex) { return Response.status(500).entity("Error occurred while creating zip file").build(); } - Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile()); rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\""); return rb.build(); @@ -284,9 +277,7 @@ public class DroneService { if (owner == null) { throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!"); } - KeyGenerationUtil.createApplicationKeys("drone"); - //create new device id String deviceId = shortUUID(); TokenClient accessTokenClient = new TokenClient(DroneConstants.DEVICE_TYPE); @@ -303,7 +294,6 @@ public class DroneService { XmppServerClient xmppServerClient = new XmppServerClient(); xmppServerClient.initControlQueue(); boolean status; - if (XmppConfig.getInstance().isEnabled()) { status = xmppServerClient.createXMPPAccount(newXmppAccount); if (!status) { @@ -336,60 +326,6 @@ public class DroneService { return Long.toString(l, Character.MAX_RADIX); } - /* --------------------------------------------------------------------------------------- - Device specific APIs - Control APIs + Data-Publishing APIs - Also contains utility methods required for the execution of these APIs - --------------------------------------------------------------------------------------- */ - @Path("controller/register/{owner}/{deviceId}/{ip}/{port}") - @POST - public String registerDeviceIP(@PathParam("owner") String owner, @PathParam("deviceId") String deviceId, - @PathParam("ip") String deviceIP, - @PathParam("port") String devicePort, - @Context HttpServletResponse response, - @Context HttpServletRequest request) { - String result; - log.info("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId + - " of owner: " + owner); - String deviceHttpEndpoint = deviceIP + ":" + devicePort; - deviceToIpMap.put(deviceId, deviceHttpEndpoint); - result = "Device-IP Registered"; - response.setStatus(Response.Status.OK.getStatusCode()); - if (log.isDebugEnabled()) { - log.debug(result); - } - log.info(owner + deviceId + deviceIP + devicePort ); - return result; - - } - - @Path("controller/send_command") - @POST - @Feature( code="send_command", name="Send Command", type="operation", - description="Send Commands to Drone") - public Response droneController(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, - @QueryParam("action") String action, @QueryParam("duration") String duration, - @QueryParam("speed") String speed){ - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - DroneConstants.DEVICE_TYPE))) { - return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); - } - } catch (DeviceManagementException e) { - log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); - } - try { - DroneAnalyzerServiceUtils.sendControlCommand(droneController, deviceId, action, Double.valueOf(speed), - Double.valueOf(duration)); - return Response.status(Response.Status.ACCEPTED).build(); - - } catch (DeviceManagementException e) { - log.error("Drone command didn't success. Try again, \n"+ e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } - } - @GET @Path("controller/scep") public Response scepRequest(@QueryParam("operation") String operation, @QueryParam("message") String message) { diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java new file mode 100644 index 0000000000..c3eb321d90 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/exception/DroneAnalyzerException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.exception; + +public class DroneAnalyzerException extends Exception { + private static final long serialVersionUID = 118512086958330189L; + + public DroneAnalyzerException(String errorMessage) { + super(errorMessage); + } + + public DroneAnalyzerException(String errorMessage, Throwable throwable) { + super(errorMessage, throwable); + } +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java new file mode 100644 index 0000000000..a8740dab7c --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/transport/DroneAnalyzerXMPPConnector.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.transport; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.jivesoftware.smack.packet.Message; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConnector; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConstants; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.trasformer.MessageTransformer; + +public class DroneAnalyzerXMPPConnector extends XmppConnector { + private static Log log = LogFactory.getLog(DroneAnalyzerXMPPConnector.class); + + private static String xmppServerIP; + private static int xmppServerPort; + private static String xmppAdminUsername; + private static String xmppAdminPassword; + private static String xmppAdminAccountJID; + private MessageTransformer messageTransformer; + + public DroneAnalyzerXMPPConnector(MessageTransformer messageTransformer) { + super(XmppConfig.getInstance().getXmppServerIP(), + XmppConfig.getInstance().getSERVER_CONNECTION_PORT()); + this.messageTransformer = messageTransformer; + + } + + public void initConnector() { + xmppServerIP = XmppConfig.getInstance().getXmppServerIP(); + xmppAdminUsername = XmppConfig.getInstance().getXmppUsername(); + xmppAdminPassword = XmppConfig.getInstance().getXmppPassword(); + xmppAdminAccountJID = xmppAdminUsername + "@" + xmppServerIP; + } + + public void connectAndLogin() { + try { + super.connectAndLogin(xmppAdminUsername, xmppAdminPassword, null); + super.setMessageFilterOnReceiver(xmppAdminAccountJID); + } catch (DeviceManagementException e) { + log.error("Connect/Login attempt to XMPP Server at: " + xmppServerIP + " failed"); + retryXMPPConnection(); + } + } + + public void setMessageFilterOnReceiver(String receiver){ + super.setMessageFilterOnReceiver( receiver+ "@" + xmppServerIP); + } + + public void connectLoginAndSetFilterOnReceiver(){ + initConnector(); + connectAndLogin(); + setMessageFilterOnReceiver(DroneConstants.DEVICE_ID); + } + + public void disconnect(){ + super.closeConnection(); + } + + @Override + protected void processXMPPMessage(Message xmppMessage) { + String from = xmppMessage.getFrom(); + String subject = xmppMessage.getSubject(); + String inbound_message = xmppMessage.getBody(); + int indexOfAt = from.indexOf("@"); + int indexOfSlash = from.indexOf("/"); + String deviceId = from.substring(0, indexOfAt); + String resource = from.substring(indexOfSlash + 1, from.length()); + + + + if ((inbound_message != null)&&(resource.equals(DroneConstants.MESSAGE_RESOURCE)) ){ + messageTransformer.messageTranslater(inbound_message); + } + else { + log.error("Message is empty or it is not belongs to "+ DroneConstants.DEVICE_ID); + } + } + + private void retryXMPPConnection() { + Thread retryToConnect = new Thread() { + @Override + public void run() { + while (true) { + if (!isConnected()) { + if (log.isDebugEnabled()) { + log.debug("Re-trying to reach XMPP Server...."); + } + try { + DroneAnalyzerXMPPConnector.super.connectAndLogin(xmppAdminUsername, xmppAdminPassword, null); + DroneAnalyzerXMPPConnector.super.setMessageFilterOnReceiver(xmppAdminAccountJID); + } catch (DeviceManagementException e1) { + if (log.isDebugEnabled()) { + log.debug("Attempt to re-connect to XMPP-Server failed"); + } + } + } else { + break; + } + + try { + Thread.sleep(5000); + } catch (InterruptedException e1) { + log.error("XMPP: Thread Sleep Interrupt Exception"); + } + } + } + }; + retryToConnect.setDaemon(true); + retryToConnect.start(); + } +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java new file mode 100644 index 0000000000..7add0cbfb7 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/trasformer/MessageTransformer.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.trasformer; + + +import org.apache.commons.collections4.queue.CircularFifoQueue; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConstants; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.MessageConfig; + +import java.io.IOException; + + +public class MessageTransformer { + + private Log log = LogFactory.getLog(MessageTransformer.class); + private CircularFifoQueue sharedQueue; + + private String outbound_message_format_for_simulator = "{\"quatanium_val\":[%f, %f, %f, %f]," + + "\"basicParam\":{\"velocity\":[%f, %f, %f], \"global_location\":[%f, %f, %f]},\"battery_level\":%f, \"device_type\":\"IRIS_DRONE\"}"; + private String outbound_message_format_for_iris_drone = "{\"quatanium_val\":[%f, %f, %f]," + + "\"basicParam\":{\"velocity\":[%f, %f, %f], \"global_location\":[%f, %f, %f]},\"battery_level\":%f," + + "\"device_type\":\"SIMULATOR\"}"; + + public MessageTransformer(){ + sharedQueue = new CircularFifoQueue(DroneConstants.MAXIMUM_BUFFERE_SIZE_OF_SHARED_QUEUE); + } + + private void messageTranslaterForSimulator(JsonNode inbound_message){ + JsonNode node = inbound_message; + String outbound_message; + + try { + JsonNode velocity = node.get(MessageConfig.OUT_BASIC_PARAM_VAL).get(MessageConfig.OUT_BASIC_PARAM_VELOCITY); + JsonNode global_location = node.get(MessageConfig.OUT_BASIC_PARAM_VAL).get( + MessageConfig.OUT_BASIC_PARAM_GLOBAL_LOCATION); + JsonNode quatanium_vals = node.get(MessageConfig.OUT_QUATANNIM_VAL); + JsonNode battery_level = node.get(MessageConfig.OUT_BATTERY_LEVEL); + outbound_message = String.format(outbound_message_format_for_simulator, sTd(quatanium_vals.get(0)), + sTd(quatanium_vals.get(1)), sTd(quatanium_vals.get(2)), sTd(quatanium_vals.get(0)), + sTd(velocity.get(0)), sTd(velocity.get(1)), sTd(velocity.get(2)), sTd(global_location.get(0)), + sTd(global_location.get(1)), sTd(global_location.get(2)), sTd(battery_level)); + sharedQueue.add(outbound_message); + } catch (Exception e) { + log.error(e.getMessage()+",\n"+ e); + } + } + + private void messageTranslaterForIRISDrone(JsonNode inbound_message){ + JsonNode node = inbound_message; + String outbound_message; + try { + + JsonNode velocity = node.get(MessageConfig.OUT_BASIC_PARAM_VAL).get(MessageConfig.OUT_BASIC_PARAM_VELOCITY); + JsonNode global_location = node.get(MessageConfig.OUT_BASIC_PARAM_VAL).get( + MessageConfig.OUT_BASIC_PARAM_GLOBAL_LOCATION); + JsonNode quatanium_vals = node.get(MessageConfig.OUT_QUATANNIM_VAL); + JsonNode battery_level = node.get(MessageConfig.OUT_BATTERY_LEVEL); + outbound_message = String.format(outbound_message_format_for_iris_drone, sTd(quatanium_vals.get(0)), + sTd(quatanium_vals.get(1)), sTd(quatanium_vals.get(2)), sTd(velocity.get(0)), + sTd(velocity.get(1)), sTd(velocity.get(2)), sTd(global_location.get(0)), + sTd(global_location.get(1)), sTd(global_location.get(2)), sTd(battery_level)); + sharedQueue.add(outbound_message); + + }catch (Exception e) { + log.error(e.getMessage()+",\n"+ e); + } + } + + public void messageTranslater(String inbound_message){ + JsonNode actualMessage = null; + ObjectMapper objectMapper = new ObjectMapper(); + + try { + actualMessage = objectMapper.readValue(inbound_message, JsonNode.class); + JsonNode deviceType = actualMessage.get(MessageConfig.IN_DEVICE_TYPE); + switch (deviceType.getTextValue()) { + + case MessageConfig.IN_IRIS_DRONE: + messageTranslaterForIRISDrone(actualMessage); + break; + case MessageConfig.IN_SIMULATOR: + messageTranslaterForSimulator(actualMessage); + break; + } + } catch (JsonProcessingException e) { + log.error("Incoming message might be corrupted, "+ e); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + + private double sTd(JsonNode s) + { + return Double.parseDouble(s.toString()); + } + + public String getMessage() { + try{ + if(sharedQueue.isEmpty() || sharedQueue == null){ + return ""; + } + return sharedQueue.remove(); + }catch(Exception e) { + log.error("There is no more messages to send or internal server error has been occurred, \n"+ e ); + return ""; + } + } + + public boolean isEmptyQueue(){ + return sharedQueue != null? sharedQueue.isEmpty():false; + } + +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java new file mode 100644 index 0000000000..d6cdce7f3e --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/DroneAnalyzerServiceUtils.java @@ -0,0 +1,138 @@ + +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.util; + +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException; +import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConstants; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.controller.DroneController; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.controller.impl.DroneControllerImpl; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.exception.DroneAnalyzerException; +import org.wso2.carbon.device.mgt.iot.droneanalyzer.service.transport.DroneAnalyzerXMPPConnector; + +import java.io.File; + +public class DroneAnalyzerServiceUtils { + + private static final String SUPER_TENANT = "carbon.super"; + private static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; + private static org.apache.commons.logging.Log log = LogFactory.getLog(DroneAnalyzerServiceUtils.class); + + public static void sendCommandViaXMPP(String deviceOwner, String deviceId, String resource, + String state, DroneAnalyzerXMPPConnector droneXMPPConnector) + throws DeviceManagementException { + + String xmppServerDomain = XmppConfig.getInstance().getXmppEndpoint(); + int indexOfChar = xmppServerDomain.lastIndexOf(File.separator); + if (indexOfChar != -1) { + xmppServerDomain = xmppServerDomain.substring((indexOfChar + 1), + xmppServerDomain.length()); + } + indexOfChar = xmppServerDomain.indexOf(":"); + if (indexOfChar != -1) { + xmppServerDomain = xmppServerDomain.substring(0, indexOfChar); + } + String clientToConnect = deviceId + "@" + xmppServerDomain + File.separator + deviceOwner; + String message = resource.replace("/", "") + ":" + state; + droneXMPPConnector.sendXMPPMessage(clientToConnect, message, "CONTROL-REQUEST"); + + } + + public static boolean sendControlCommand(DroneController controller, String deviceId, String action, double speed, double duration) + throws DeviceManagementException { + boolean control_state = false; + try{ + switch (action){ + case DroneConstants.TAKE_OFF: + control_state = controller.takeoff(); + break; + case DroneConstants.LAND: + control_state = controller.land(); + break; + case DroneConstants.BACK: + control_state = controller.back(speed, duration); + break; + case DroneConstants.CLOCK_WISE: + control_state = controller.clockwise(speed, duration); + break; + case DroneConstants.COUNTER_CLOCKWISE: + control_state = controller.conterClockwise(speed, duration); + break; + case DroneConstants.DOWN: + control_state = controller.down(speed, duration); + break; + case DroneConstants.FRONT: + control_state = controller.back(speed, duration); + break; + case DroneConstants.FORWARD: + control_state = controller.clockwise(speed, duration); + break; + case DroneConstants.UP: + control_state = controller.up(speed, duration); + break; + } + }catch(Exception e){ + log.error(e.getMessage()+ "\n"+ e); + } + return control_state; + } + + public static boolean publishToDAS(String owner, String deviceId, float temperature) { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + ctx.setTenantDomain(SUPER_TENANT, true); + DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService( + DeviceAnalyticsService.class, null); + Object metdaData[] = {owner, DroneConstants.DEVICE_TYPE, deviceId, + System.currentTimeMillis()}; + Object payloadData[] = {temperature}; + + try { + deviceAnalyticsService.publishEvent(TEMPERATURE_STREAM_DEFINITION, "1.0.0", metdaData, + new Object[0], payloadData); + } catch (DataPublisherConfigurationException e) { + return false; + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + return true; + } + + public static CertificateManagementService getCertificateManagementService() throws + DroneAnalyzerException { + + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + CertificateManagementService certificateManagementService = (CertificateManagementService) + ctx.getOSGiService(CertificateManagementService.class, null); + + if (certificateManagementService == null) { + String msg = "EnrollmentService is not initialized"; + log.error(msg); + throw new DroneAnalyzerException(msg); + } + + return certificateManagementService; + } + +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java new file mode 100644 index 0000000000..00e19d4469 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/ContentType.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.util.scep; + +public class ContentType { + public static final String X_PKI_MESSAGE = "application/x-pki-message"; + public static final String X_X509_CA_CERT = "application/x-x509-ca-cert"; + public static final String X_X509_CA_RA_CERT = "application/x-x509-ca-ra-cert"; +} + diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java new file mode 100644 index 0000000000..a14ed10eb0 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/droneanalyzer/service/util/scep/SCEPOperation.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.iot.droneanalyzer.service.util.scep; + +public enum SCEPOperation { + GET_CA_CERT("GetCACert"), + GET_CA_CAPS("GetCACaps"), + PKI_OPERATION("PKIOperation"); + + private String value; + + private SCEPOperation(String value) { + this.setValue(value); + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/resources.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/resources.xml new file mode 100644 index 0000000000..00eeca1585 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/resources.xml @@ -0,0 +1,87 @@ + + + + + + + + Any + PUT + http://localhost:9763/drone_analyzer/manager/device/register + /manager/device/register/* + + + Any + DELETE + http://localhost:9763/drone_analyzer/manager/device/remove + /manager/device/remove/* + + + Any + POST + http://localhost:9763/drone_analyzer/manager/device/update + /manager/device/update/* + + + Any + GET + http://localhost:9763/drone_analyzer/manager/device + /manager/device/* + + + Any + GET + http://localhost:9763/drone_analyzer/manager/devices + /manager/devices/* + + + Any + GET + http://localhost:9763/drone_analyzer/manager/device/{sketch_type}/download + /manager/device/{sketch_type}/download + + + Any + GET + http://localhost:9763/drone_analyzer/manager/device/{sketch_type}/generate_link + /manager/device/{sketch_type}/generate_link + + + + Any + POST + http://localhost:9763/drone_analyzer/controller/register/{owner}/{deviceId}/{ip}/{port} + /controller/register/{owner}/{deviceId}/{ip}/{port} + + + Any + GET + http://localhost:9763/drone_analyzer/controller/send_command + /controller/send_command + + \ No newline at end of file diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml new file mode 100644 index 0000000000..fa44619195 --- /dev/null +++ b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml @@ -0,0 +1,33 @@ + + + + + + + + + false + + + CXF,Carbon + diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/WEB-INF/cxf-servlet.xml rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml diff --git a/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.api/src/main/webapp/WEB-INF/web.xml rename to components/device-mgt-iot-droneanalyzer/org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api/src/main/webapp/WEB-INF/web.xml diff --git a/components/device-mgt-iot-droneanalyzer/pom.xml b/components/device-mgt-iot-droneanalyzer/pom.xml index 692b9b6e69..8100707ca0 100644 --- a/components/device-mgt-iot-droneanalyzer/pom.xml +++ b/components/device-mgt-iot-droneanalyzer/pom.xml @@ -33,7 +33,8 @@ 1.9.2-SNAPSHOT org.wso2.carbon.device.mgt.iot.droneanalyzer - org.wso2.carbon.device.mgt.iot.droneanalyzer.api + org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api + org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api pom WSO2 Carbon - IoT Device Drone Analyzer Management Component diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java index 484b0ce8e8..b2cffe1996 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java @@ -163,7 +163,6 @@ public class RaspberryPiService { @DELETE public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -181,8 +180,6 @@ public class RaspberryPiService { } finally { deviceManagement.endTenantFlow(); } - - } @Path("manager/device/update/{device_id}") diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/pom.xml b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/pom.xml index b72740af74..d6b8029d28 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/pom.xml +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/pom.xml @@ -26,7 +26,14 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.droneanalyzer.api + org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api + 1.9.2-SNAPSHOT + war + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api + 1.9.2-SNAPSHOT war @@ -75,13 +82,22 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.droneanalyzer.api + org.wso2.carbon.device.mgt.iot.droneanalyzer.controller.api war true ${basedir}/src/main/resources/webapps/ drone_analyzer.war + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.droneanalyzer.mgt.api + + war + true + ${basedir}/src/main/resources/webapps/ + drone_analyzer_mgt.war + @@ -161,8 +177,6 @@ - - diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.drone_analyzer.device-view/device-view.hbs b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.drone_analyzer.device-view/device-view.hbs index c7e1150f4e..79769b5a86 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.drone_analyzer.device-view/device-view.hbs +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.drone_analyzer.device-view/device-view.hbs @@ -2,12 +2,15 @@ {{/zone}} {{#zone "device-opetations"}} -
- Operations -
{{unit "iot.unit.device.operation-bar" device=device}}
+
+
Device Statistics
+ {{unit "iot.unit.device.droneanalyzer.statistics" device=device}} +
+ {{/zone}} {{#zone "device-detail-properties"}}
@@ -21,14 +24,8 @@
-
- -
-
Device Statistics
- {{unit "iot.unit.device.droneanalyzer.statistics" device=device}} -
+
Policy Compliance