Skip to content

Commit

Permalink
Merge branch 'backport-4058' into release-1.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Coduz committed Jun 19, 2024
2 parents 79e52c2 + 6839145 commit 0809378
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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;

/**
* {@link Translator} implementation from {@link KuraDataMessage} to {@link KapuaDataMessage}
Expand All @@ -45,6 +47,12 @@ public class TranslatorDataKuraKapua extends Translator<KuraDataMessage, KapuaDa

private static final KapuaDataMessageFactory DATA_MESSAGE_FACTORY = LOCATOR.getFactory(KapuaDataMessageFactory.class);

final private boolean resolveDeviceId;

public TranslatorDataKuraKapua() {
resolveDeviceId = TranslatorKapuaKuraSettings.getInstance().getBoolean(TranslatorKapuaKuraSettingKeys.TRANSLATOR_KURA_KAPUA_DATA_DEVICE_ID_RESOLVE);
}

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

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

KapuaDataMessage kapuaDataMessage = DATA_MESSAGE_FACTORY.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 @@ -77,6 +82,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 = DEVICE_REGISTRY_SERVICE.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,57 @@
/*******************************************************************************
* 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";

/**
* The singleton instance of {@link TranslatorKapuaKuraSettings}
*
* @since 2.1.0
*/
private static final TranslatorKapuaKuraSettings INSTANCE = new TranslatorKapuaKuraSettings();

/**
* Constructor.
*
* @since 2.1.0
*/
public TranslatorKapuaKuraSettings() {
super(TRANSLATOR_KAPUA_KURA_SETTING_RESOURCE);
}

/**
* Gets the singleton instance of {@link TranslatorKapuaKuraSettings}
* @return The singleton instance of {@link TranslatorKapuaKuraSettings}
*
* @since 2.1.0
*/
public static TranslatorKapuaKuraSettings getInstance() {
return INSTANCE;
}
}
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 0809378

Please sign in to comment.