Search in sources :

Example 1 with DevicePackages

use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.

the class TranslatorAppPackageKuraKapua method translate.

private void translate(PackageResponsePayload packageResponsePayload, String charEncoding, KuraDeploymentPackages kuraDeploymentPackages) throws KapuaException {
    try {
        KuraDeploymentPackage[] deploymentPackageArray = kuraDeploymentPackages.getDeploymentPackages();
        if (deploymentPackageArray != null) {
            KapuaLocator locator = KapuaLocator.getInstance();
            DevicePackageFactory deviceDeploymentFactory = locator.getFactory(DevicePackageFactory.class);
            DevicePackages deviceDeploymentPackages = deviceDeploymentFactory.newDeviceDeploymentPackages();
            for (KuraDeploymentPackage deploymentPackage : deploymentPackageArray) {
                DevicePackage deviceDeploymentPackage = deviceDeploymentFactory.newDeviceDeploymentPackage();
                deviceDeploymentPackage.setName(deploymentPackage.getName());
                deviceDeploymentPackage.setVersion(deploymentPackage.getVersion());
                DevicePackageBundleInfos devicePackageBundleInfos = deviceDeploymentPackage.getBundleInfos();
                KuraBundleInfo[] bundleInfoArray = deploymentPackage.getBundleInfos();
                for (KuraBundleInfo bundleInfo : bundleInfoArray) {
                    DevicePackageBundleInfo devicePackageBundleInfo = deviceDeploymentFactory.newDevicePackageBundleInfo();
                    devicePackageBundleInfo.setName(bundleInfo.getName());
                    devicePackageBundleInfo.setVersion(bundleInfo.getVersion());
                    // Add the new DevicePackageBundleInfo object to the corresponding list
                    devicePackageBundleInfos.getBundlesInfos().add(devicePackageBundleInfo);
                }
                // Add the new DeviceDeploymentPackage object to the corresponding list
                deviceDeploymentPackages.getPackages().add(deviceDeploymentPackage);
            }
            StringWriter sw = new StringWriter();
            XmlUtil.marshal(deviceDeploymentPackages, sw);
            byte[] requestBody = sw.toString().getBytes(charEncoding);
            packageResponsePayload.setBody(requestBody);
        }
    } catch (Exception e) {
        throw new TranslatorException(TranslatorErrorCodes.INVALID_BODY, e, kuraDeploymentPackages);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DevicePackageBundleInfo(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfo) DevicePackage(org.eclipse.kapua.service.device.management.packages.model.DevicePackage) KuraBundleInfo(org.eclipse.kapua.service.device.call.kura.model.deploy.KuraBundleInfo) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) DevicePackageFactory(org.eclipse.kapua.service.device.management.packages.DevicePackageFactory) StringWriter(java.io.StringWriter) DevicePackageBundleInfos(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfos) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KuraDeploymentPackage(org.eclipse.kapua.service.device.call.kura.model.deploy.KuraDeploymentPackage)

Example 2 with DevicePackages

use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.

the class Devices method getPackages.

/**
 * Returns the list of packages deployed on a device.
 *
 * @param deviceId
 *            The client ID of the Device requested.
 * @return The list of the installed packages.
 */
@GET
@Path("{deviceId}/packages")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DevicePackages getPackages(@PathParam("deviceId") String deviceId) {
    DevicePackages result = packageFactory.newDeviceDeploymentPackages();
    try {
        KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
        KapuaId id = KapuaEid.parseShortId(deviceId);
        result = packageService.getInstalled(scopeId, id, null);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(result);
}
Also used : KapuaId(org.eclipse.kapua.model.id.KapuaId) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages)

Example 3 with DevicePackages

use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method findDevicePackages.

// 
// Packages
// 
@Override
public List<GwtDeploymentPackage> findDevicePackages(String scopeShortId, String deviceShortId) throws GwtKapuaException {
    List<GwtDeploymentPackage> gwtPkgs = new ArrayList<GwtDeploymentPackage>();
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        DevicePackageManagementService deviceManagementService = locator.getService(DevicePackageManagementService.class);
        KapuaId scopeId = KapuaEid.parseShortId(scopeShortId);
        KapuaId deviceId = KapuaEid.parseShortId(deviceShortId);
        DevicePackages deploymentPackages = deviceManagementService.getInstalled(scopeId, deviceId, null);
        for (DevicePackage deploymentPackage : deploymentPackages.getPackages()) {
            GwtDeploymentPackage gwtPkg = new GwtDeploymentPackage();
            gwtPkg.setName(deploymentPackage.getName());
            gwtPkg.setVersion(deploymentPackage.getVersion());
            DevicePackageBundleInfos devicePackageBundleInfos = deploymentPackage.getBundleInfos();
            if (devicePackageBundleInfos != null) {
                List<GwtBundleInfo> gwtBundleInfos = new ArrayList<GwtBundleInfo>();
                for (DevicePackageBundleInfo bundleInfo : devicePackageBundleInfos.getBundlesInfos()) {
                    GwtBundleInfo gwtBundleInfo = new GwtBundleInfo();
                    gwtBundleInfo.setName(bundleInfo.getName());
                    gwtBundleInfo.setVersion(bundleInfo.getVersion());
                    gwtBundleInfos.add(gwtBundleInfo);
                }
                gwtPkg.setBundleInfos(gwtBundleInfos);
            }
            gwtPkgs.add(gwtPkg);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtPkgs;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDeploymentPackage(org.eclipse.kapua.app.console.shared.model.GwtDeploymentPackage) ArrayList(java.util.ArrayList) DevicePackageBundleInfo(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfo) DevicePackage(org.eclipse.kapua.service.device.management.packages.model.DevicePackage) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) DevicePackageManagementService(org.eclipse.kapua.service.device.management.packages.DevicePackageManagementService) DevicePackageBundleInfos(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfos) KapuaId(org.eclipse.kapua.model.id.KapuaId) GwtBundleInfo(org.eclipse.kapua.app.console.shared.model.GwtBundleInfo)

Example 4 with DevicePackages

use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method getInstalled.

@Override
public DevicePackages getInstalled(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.read, scopeId));
    // 
    // Prepare the request
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.READ);
    packageRequestChannel.setPackageResource(null);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
    packageRequestMessage.setScopeId(scopeId);
    packageRequestMessage.setDeviceId(deviceId);
    packageRequestMessage.setCapturedOn(new Date());
    packageRequestMessage.setPayload(packageRequestPayload);
    packageRequestMessage.setChannel(packageRequestChannel);
    // 
    // Do get
    @SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
    PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    PackageResponsePayload responsePayload = responseMessage.getPayload();
    DevicePackages devicePackages;
    if (responsePayload.getBody() != null) {
        DeviceManagementSetting config = DeviceManagementSetting.getInstance();
        String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        String body = null;
        try {
            body = new String(responsePayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
        }
        try {
            devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
        }
    } else {
        devicePackages = new DevicePackagesImpl();
    }
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), PackageAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.READ);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
    return devicePackages;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) DevicePackagesImpl(org.eclipse.kapua.service.device.management.packages.model.internal.DevicePackagesImpl) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) KapuaException(org.eclipse.kapua.KapuaException) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) PackageRequestChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Aggregations

DevicePackages (org.eclipse.kapua.service.device.management.packages.model.DevicePackages)4 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)3 KapuaException (org.eclipse.kapua.KapuaException)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 DevicePackage (org.eclipse.kapua.service.device.management.packages.model.DevicePackage)2 DevicePackageBundleInfo (org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfo)2 DevicePackageBundleInfos (org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfos)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GwtBundleInfo (org.eclipse.kapua.app.console.shared.model.GwtBundleInfo)1 GwtDeploymentPackage (org.eclipse.kapua.app.console.shared.model.GwtDeploymentPackage)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 KuraBundleInfo (org.eclipse.kapua.service.device.call.kura.model.deploy.KuraBundleInfo)1 KuraDeploymentPackage (org.eclipse.kapua.service.device.call.kura.model.deploy.KuraDeploymentPackage)1 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)1 DevicePackageFactory (org.eclipse.kapua.service.device.management.packages.DevicePackageFactory)1