Search in sources :

Example 6 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeManager method updateDeviceInfo.

@Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("updating the details of sampledevice device : " + deviceIdentifier);
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().updateDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the sampledevice device : " + deviceIdentifier;
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 7 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeManager method modifyEnrollment.

@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Modifying the sampledevice device enrollment data");
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().updateDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the enrollment of the sampledevice device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 8 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeServiceImpl method register.

/**
 * Register device into device management service.
 *
 * @param deviceId unique identifier for given device type instance
 * @param name     name for the device type instance
 * @return check whether device is installed into cdmf
 */
private boolean register(String deviceId, String name) {
    try {
        DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
        deviceIdentifier.setId(deviceId);
        deviceIdentifier.setType(DeviceTypeConstants.DEVICE_TYPE);
        if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
            return false;
        }
        Device device = new Device();
        device.setDeviceIdentifier(deviceId);
        EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
        enrolmentInfo.setDateOfEnrolment(new Date().getTime());
        enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
        enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
        enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
        device.setName(name);
        device.setType(DeviceTypeConstants.DEVICE_TYPE);
        enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
        device.setEnrolmentInfo(enrolmentInfo);
        boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
        return added;
    } catch (DeviceManagementException e) {
        log.error(e.getMessage(), e);
        return false;
    }
}
Also used : DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) Device(org.wso2.carbon.device.mgt.common.Device) EnrolmentInfo(org.wso2.carbon.device.mgt.common.EnrolmentInfo) Date(java.util.Date) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier)

Example 9 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeServiceImpl method createDownloadFile.

private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType) throws DeviceManagementException, JWTClientException, APIManagerException, UserStoreException {
    // create new device id
    String deviceId = shortUUID();
    if (apiApplicationKey == null) {
        String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName();
        applicationUsername = applicationUsername + "@" + APIUtil.getAuthenticatedUserTenantDomain();
        APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
        String[] tags = { DeviceTypeConstants.DEVICE_TYPE };
        apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(DeviceTypeConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, "3600");
    }
    JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
    String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_" + deviceId;
    AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), apiApplicationKey.getConsumerSecret(), owner + "@" + APIUtil.getAuthenticatedUserTenantDomain(), scopes);
    // create token
    String accessToken = accessTokenInfo.getAccessToken();
    String refreshToken = accessTokenInfo.getRefreshToken();
    boolean status = register(deviceId, deviceName);
    if (!status) {
        String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner;
        throw new DeviceManagementException(msg);
    }
    ZipUtil ziputil = new ZipUtil();
    ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId, deviceName, accessToken, refreshToken, apiApplicationKey.toString());
    return zipFile;
}
Also used : AccessTokenInfo(org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) APIManagementProviderService(org.wso2.carbon.apimgt.application.extension.APIManagementProviderService) JWTClient(org.wso2.carbon.identity.jwt.client.extension.JWTClient) ZipUtil(org.wso2.iot.sampledevice.api.util.ZipUtil) ZipArchive(org.wso2.iot.sampledevice.api.util.ZipArchive)

Example 10 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class ZipUtil method createZipFile.

/**
 * Create agent zip file.
 *
 * @param owner             Owner of device.
 * @param tenantDomain      Tenant of the device.
 * @param deviceType        Device type.
 * @param deviceId          Device ID.
 * @param deviceName        Device Name
 * @param token             Auth token to access the api.
 * @param refreshToken      Refresh token to generate new auth token.
 * @param apiApplicationKey Application key.
 * @return Zip archive.
 * @throws DeviceManagementException Error creating zip archive.
 */
public ZipArchive createZipFile(String owner, String tenantDomain, String deviceType, String deviceId, String deviceName, String token, String refreshToken, String apiApplicationKey) throws DeviceManagementException {
    String sketchFolder = "repository" + File.separator + "resources" + File.separator + "sketches";
    String archivesPath = CarbonUtils.getCarbonHome() + File.separator + sketchFolder + File.separator + "archives" + File.separator + deviceId;
    String templateSketchPath = sketchFolder + File.separator + deviceType;
    String iotServerIP;
    try {
        iotServerIP = getServerUrl();
        String httpsServerEP = Utils.replaceSystemProperty(HTTPS_PROTOCOL_URL);
        String httpServerEP = Utils.replaceSystemProperty(HTTP_PROTOCOL_URL);
        String mqttEndpoint = Utils.replaceSystemProperty(DEFAULT_MQTT_ENDPOINT);
        if (mqttEndpoint.contains(LOCALHOST)) {
            mqttEndpoint = mqttEndpoint.replace(LOCALHOST, iotServerIP);
            httpsServerEP = httpsServerEP.replace(LOCALHOST, iotServerIP);
            httpServerEP = httpServerEP.replace(LOCALHOST, iotServerIP);
        }
        String base64EncodedApplicationKey = getBase64EncodedAPIAppKey(apiApplicationKey).trim();
        Map<String, String> contextParams = new HashMap<>();
        contextParams.put("SERVER_NAME", APIUtil.getTenantDomainOftheUser());
        contextParams.put("DEVICE_OWNER", owner);
        contextParams.put("DEVICE_ID", deviceId);
        contextParams.put("DEVICE_NAME", deviceName);
        contextParams.put("HTTPS_EP", httpsServerEP);
        contextParams.put("HTTP_EP", httpServerEP);
        contextParams.put("APIM_EP", httpsServerEP);
        contextParams.put("MQTT_EP", mqttEndpoint);
        contextParams.put("DEVICE_TOKEN", token);
        contextParams.put("DEVICE_REFRESH_TOKEN", refreshToken);
        contextParams.put("API_APPLICATION_KEY", base64EncodedApplicationKey);
        ZipArchive zipFile;
        zipFile = getSketchArchive(archivesPath, templateSketchPath, contextParams, deviceName);
        return zipFile;
    } catch (IOException e) {
        throw new DeviceManagementException("Zip File Creation Failed", e);
    }
}
Also used : DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)14 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)4 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)4 IOException (java.io.IOException)3 Date (java.util.Date)2 Path (javax.ws.rs.Path)2 Device (org.wso2.carbon.device.mgt.common.Device)2 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)2 EnrolmentInfo (org.wso2.carbon.device.mgt.common.EnrolmentInfo)2 ZipArchive (org.wso2.iot.sampledevice.api.util.ZipArchive)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 APIManagementProviderService (org.wso2.carbon.apimgt.application.extension.APIManagementProviderService)1