Skip to content

Commit

Permalink
Merge pull request #4058 from Coduz/feat-dataTranslatorOptionalDevice…
Browse files Browse the repository at this point in the history
…IdResolution

✨ [Messaging] Introduced configuration to optionally skip Device.id resolution for KapuaDataMessage
  • Loading branch information
Coduz authored Jun 19, 2024
2 parents 6083dcf + 8bf666e commit 9ab98f4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.eclipse.kapua.translator.kura.kapua.keystore.TranslatorAppKeystoreItemsKuraKapua;
import org.eclipse.kapua.translator.kura.kapua.keystore.TranslatorAppKeystoreNoContentKuraKapua;
import org.eclipse.kapua.translator.kura.kapua.keystore.TranslatorAppKeystoresKuraKapua;
import org.eclipse.kapua.translator.setting.TranslatorKapuaKuraSettings;

public class KapuaKuraTranslatorsModule extends AbstractKapuaModule {
@Override
Expand Down Expand Up @@ -122,6 +123,7 @@ protected void configureModule() {
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.setSerializationInclusion(JsonInclude.Include.NON_NULL));

bind(TranslatorKapuaKuraSettings.class).in(Singleton.class);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.kapua.translator.exception.InvalidMessageException;
import org.eclipse.kapua.translator.exception.InvalidPayloadException;
import org.eclipse.kapua.translator.exception.TranslateException;
import org.eclipse.kapua.translator.setting.TranslatorKapuaKuraSettingKeys;
import org.eclipse.kapua.translator.setting.TranslatorKapuaKuraSettings;

import javax.inject.Inject;

Expand All @@ -48,6 +50,13 @@ public class TranslatorDataKuraKapua extends Translator<KuraDataMessage, KapuaDa
@Inject
private TranslatorKuraKapuaUtils translatorKuraKapuaUtils;

final private boolean resolveDeviceId;

@Inject
public TranslatorDataKuraKapua(TranslatorKapuaKuraSettings translatorKapuaKuraSettings) {
resolveDeviceId = translatorKapuaKuraSettings.getBoolean(TranslatorKapuaKuraSettingKeys.TRANSLATOR_KURA_KAPUA_DATA_DEVICE_ID_RESOLVE);
}

@Override
public KapuaDataMessage translate(KuraDataMessage kuraMessage) throws TranslateException {
try {
Expand All @@ -62,11 +71,8 @@ public KapuaDataMessage translate(KuraDataMessage kuraMessage) throws TranslateE
throw new KapuaEntityNotFoundException(Account.TYPE, kuraMessage.getChannel().getScope());
}

Device device = deviceRegistryService.findByClientId(account.getId(), kuraMessage.getChannel().getClientId());

KapuaDataMessage kapuaDataMessage = kapuaDataMessageFactory.newKapuaDataMessage();
kapuaDataMessage.setScopeId(account.getId());
kapuaDataMessage.setDeviceId(device != null ? device.getId() : null);
kapuaDataMessage.setClientId(kuraMessage.getChannel().getClientId());
kapuaDataMessage.setChannel(kapuaDataChannel);
kapuaDataMessage.setPayload(kapuaDataPayload);
Expand All @@ -75,6 +81,12 @@ public KapuaDataMessage translate(KuraDataMessage kuraMessage) throws TranslateE
kapuaDataMessage.setReceivedOn(kuraMessage.getTimestamp());
kapuaDataMessage.setPosition(translatorKuraKapuaUtils.translate(kuraMessage.getPayload().getPosition()));

// Optionally resolve the KapuaDataChannel.clientId to improve performances
if (resolveDeviceId) {
Device device = deviceRegistryService.findByClientId(account.getId(), kuraMessage.getChannel().getClientId());
kapuaDataMessage.setDeviceId(device != null ? device.getId() : null);
}

// Return Kapua Message
return kapuaDataMessage;
} catch (InvalidChannelException | InvalidPayloadException te) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.translator.setting;

import org.eclipse.kapua.commons.setting.SettingKey;
import org.eclipse.kapua.message.device.data.KapuaDataMessage;
import org.eclipse.kapua.service.device.call.message.kura.KuraChannel;
import org.eclipse.kapua.service.device.call.message.kura.data.KuraDataMessage;
import org.eclipse.kapua.service.device.registry.Device;

/**
* {@link SettingKey}s for {@link TranslatorKapuaKuraSettings}
*
* @since 2.1.0
*/
public enum TranslatorKapuaKuraSettingKeys implements SettingKey {

/**
* Whether to resolve the {@link Device#getId()} from the {@link KuraChannel#getClientId()} when converting from {@link KuraDataMessage} to {@link KapuaDataMessage}.
*
* @since 2.1.0
*/
TRANSLATOR_KURA_KAPUA_DATA_DEVICE_ID_RESOLVE("translator.kura.kapua.data.deviceId.resolve");

/**
* The key value of the {@link SettingKey}.
*
* @since 2.1.0
*/
private final String key;

/**
* Constructor.
*
* @param key The key value of the {@link SettingKey}.
* @since 2.1.0
*/
TranslatorKapuaKuraSettingKeys(String key) {
this.key = key;
}

@Override
public String key() {
return key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.translator.setting;

import org.eclipse.kapua.commons.setting.AbstractKapuaSetting;

/**
* {@link TranslatorKapuaKuraSettings} for {@code kapua-translator-kapua-kura} module.
*
* @see AbstractKapuaSetting
* @since 2.1.0
*/
public class TranslatorKapuaKuraSettings extends AbstractKapuaSetting<TranslatorKapuaKuraSettingKeys> {

/**
* Setting filename.
*
* @since 2.1.0
*/
private static final String TRANSLATOR_KAPUA_KURA_SETTING_RESOURCE = "translator-kapua-kura-settings.properties";

/**
* Constructor.
*
* @since 2.1.0
*/
public TranslatorKapuaKuraSettings() {
super(TRANSLATOR_KAPUA_KURA_SETTING_RESOURCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
###############################################################################
# Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eurotech - initial API and implementation
#
###############################################################################
translator.kura.kapua.data.deviceId.resolve=true

0 comments on commit 9ab98f4

Please sign in to comment.