Skip to content

Commit

Permalink
refactor(ModemReadyEvent): remove ModemReadyEvent handling from Cloud…
Browse files Browse the repository at this point in the history
…Connections (#5331)

* refactor(ModemReadyEvent): remove ModemReadyEvent handling from CloudConnections

Signed-off-by: Marcello Martina <[email protected]>

* refactor: removed republish birth on modem detect option

Signed-off-by: Marcello Martina <[email protected]>

* chore: updated copyright headers

Signed-off-by: Marcello Martina <[email protected]>

---------

Signed-off-by: Marcello Martina <[email protected]>
  • Loading branch information
marcellorinaldo authored Aug 14, 2024
1 parent bb46d09 commit 6a47cf4
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 258 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018, 2020 Eurotech and/or its affiliates and others
Copyright (c) 2018, 2024 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
SPDX-License-Identifier: EPL-2.0
Contributors:
Eurotech
-->
<MetaData xmlns="http://www.osgi.org/xmlns/metatype/v1.2.0" localization="en_us">
Expand Down Expand Up @@ -57,15 +57,7 @@
required="true"
default="false"
description="Whether or not to republish the MQTT Birth Certificate on GPS lock event"/>

<AD id="republish.mqtt.birth.cert.on.modem.detect"
name="Republish Mqtt Birth Cert On Modem Detect"
type="Boolean"
cardinality="0"
required="true"
default="false"
description="Whether or not to republish the MQTT Birth Certificate on modem detection event. This functionality is currently not supported on devices configured to use NetworkManager, in this case the property value is ignored."/>"/>


<AD id="payload.encoding"
name="Payload Encoding"
type="String"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import org.eclipse.kura.message.KuraApplicationTopic;
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.net.NetworkService;
import org.eclipse.kura.net.modem.ModemReadyEvent;
import org.eclipse.kura.net.status.NetworkInterfaceStatus;
import org.eclipse.kura.net.status.NetworkInterfaceType;
import org.eclipse.kura.net.status.NetworkStatusService;
Expand Down Expand Up @@ -100,8 +99,6 @@ public class CloudConnectionManagerImpl

private static final String SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE = "Cannot setup cloud service connection";

private static final String ERROR = "ERROR";

private static final Logger logger = LoggerFactory.getLogger(CloudConnectionManagerImpl.class);

private static final String CONNECTION_EVENT_PID_PROPERTY_KEY = "cloud.service.pid";
Expand Down Expand Up @@ -292,8 +289,7 @@ protected void activate(ComponentContext componentContext, Map<String, Object> p
//
// install event listener for GPS locked event
Dictionary<String, Object> props = new Hashtable<>();
String[] eventTopics = { PositionLockedEvent.POSITION_LOCKED_EVENT_TOPIC,
ModemReadyEvent.MODEM_EVENT_READY_TOPIC, EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL,
String[] eventTopics = { PositionLockedEvent.POSITION_LOCKED_EVENT_TOPIC, EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL,
EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL };
props.put(EventConstants.EVENT_TOPIC, eventTopics);
this.cloudServiceRegistration = this.ctx.getBundleContext().registerService(EventHandler.class.getName(), this,
Expand Down Expand Up @@ -363,13 +359,8 @@ public void handleEvent(Event event) {
return;
}

if (ModemReadyEvent.MODEM_EVENT_READY_TOPIC.contains(topic)) {
handleModemReadyEvent(event);
return;
}

if ((EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL.equals(topic)
|| EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL.equals(topic)) && this.dataService.isConnected()) {
if ((EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL.equals(topic) || EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL.equals(topic))
&& this.dataService.isConnected()) {
logger.debug("CloudConnectionManagerImpl: received install/uninstall event, publishing BIRTH.");
tryPublishBirthCertificate(false);
}
Expand All @@ -384,35 +375,6 @@ private void handlePositionLockedEvent() {
}
}

private void handleModemReadyEvent(Event event) {
logger.info("Handling ModemReadyEvent");
ModemReadyEvent modemReadyEvent = (ModemReadyEvent) event;
// keep these identifiers around until we can publish the certificate
this.imei = (String) modemReadyEvent.getProperty(ModemReadyEvent.IMEI);
this.imsi = (String) modemReadyEvent.getProperty(ModemReadyEvent.IMSI);
this.iccid = (String) modemReadyEvent.getProperty(ModemReadyEvent.ICCID);
this.rssi = (String) modemReadyEvent.getProperty(ModemReadyEvent.RSSI);
this.modemFwVer = (String) modemReadyEvent.getProperty(ModemReadyEvent.FW_VERSION);
logger.trace("handleEvent() :: IMEI={}", this.imei);
logger.trace("handleEvent() :: IMSI={}", this.imsi);
logger.trace("handleEvent() :: ICCID={}", this.iccid);
logger.trace("handleEvent() :: RSSI={}", this.rssi);
logger.trace("handleEvent() :: FW_VERSION={}", this.modemFwVer);

if (this.dataService.isConnected() && this.options.getRepubBirthCertOnModemDetection() && isModemInfoValid()) {
logger.debug("handleEvent() :: publishing BIRTH certificate ...");
tryPublishBirthCertificate(false);
}
}

private boolean isModemInfoValid(final String modemInfo) {
return !(modemInfo == null || modemInfo.length() == 0 || modemInfo.equals(ERROR));
}

public boolean isModemInfoValid() {
return isModemInfoValid(this.imei) && isModemInfoValid(this.imsi) && isModemInfoValid(this.iccid);
}

private void tryPublishBirthCertificate(boolean isNewConnection) {
try {
publishBirthCertificate(isNewConnection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2024 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
Expand Down Expand Up @@ -33,7 +33,6 @@ public class CloudConnectionManagerOptions {
private static final String DEVICE_CUSTOM_NAME = "device.custom-name";
private static final String ENCODE_GZIP = "encode.gzip";
private static final String REPUB_BIRTH_ON_GPS_LOCK = "republish.mqtt.birth.cert.on.gps.lock";
private static final String REPUB_BIRTH_ON_MODEM_DETECT = "republish.mqtt.birth.cert.on.modem.detect";
private static final String PAYLOAD_ENCODING = "payload.encoding";

private static final int LIFECYCLE_QOS = 1;
Expand Down Expand Up @@ -113,22 +112,6 @@ public boolean getRepubBirthCertOnGpsLock() {
return repubBirth;
}

/**
* Returns true if the current CloudService configuration
* specifies the cloud client should republish the MQTT birth
* certificate on modem detection events.
*
* @return a boolean value.
*/
public boolean getRepubBirthCertOnModemDetection() {
boolean repubBirth = false;
if (this.properties != null && this.properties.get(REPUB_BIRTH_ON_MODEM_DETECT) != null
&& this.properties.get(REPUB_BIRTH_ON_MODEM_DETECT) instanceof Boolean) {
repubBirth = (Boolean) this.properties.get(REPUB_BIRTH_ON_MODEM_DETECT);
}
return repubBirth;
}

/**
* This method parses the Cloud Service configuration and returns the selected cloud payload encoding.
* By default, this method returns {@link CloudPayloadEncoding} {@code KURA_PROTOBUF}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2011, 2021 Eurotech and/or its affiliates and others
Copyright (c) 2011, 2024 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
Expand Down Expand Up @@ -69,15 +69,7 @@
required="true"
default="false"
description="Whether or not to republish the MQTT Birth Certificate on GPS lock event"/>

<AD id="republish.mqtt.birth.cert.on.modem.detect"
name="Republish Mqtt Birth Cert On Modem Detect"
type="Boolean"
cardinality="0"
required="true"
default="false"
description="Whether or not to republish the MQTT Birth Certificate on modem detection event. This functionality is currently not supported on devices configured to use NetworkManager, in this case the property value is ignored."/>


<AD id="republish.mqtt.birth.cert.on.tamper.event"
name="Republish Mqtt Birth Cert On Tamper Event"
type="Boolean"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import org.eclipse.kura.message.KuraApplicationTopic;
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.net.NetworkService;
import org.eclipse.kura.net.modem.ModemReadyEvent;
import org.eclipse.kura.net.status.NetworkInterfaceStatus;
import org.eclipse.kura.net.status.NetworkInterfaceType;
import org.eclipse.kura.net.status.NetworkStatusService;
Expand Down Expand Up @@ -122,7 +121,6 @@ public class CloudServiceImpl
private static final String CONNECTION_EVENT_PID_PROPERTY_KEY = "cloud.service.pid";
private static final String SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE = "Cannot setup cloud service connection";
private static final String NOTIFICATION_PUBLISHER_PID = "org.eclipse.kura.cloud.publisher.CloudNotificationPublisher";
private static final String ERROR = "ERROR";
private static final String KURA_PAYLOAD = "KuraPayload";
static final String EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL = "org/osgi/service/deployment/INSTALL";
static final String EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL = "org/osgi/service/deployment/UNINSTALL";
Expand Down Expand Up @@ -338,8 +336,7 @@ protected void activate(ComponentContext componentContext, Map<String, Object> p
//
// install event listener for GPS locked event
Dictionary<String, Object> props = new Hashtable<>();
String[] eventTopics = { PositionLockedEvent.POSITION_LOCKED_EVENT_TOPIC,
ModemReadyEvent.MODEM_EVENT_READY_TOPIC, TamperEvent.TAMPER_EVENT_TOPIC,
String[] eventTopics = { PositionLockedEvent.POSITION_LOCKED_EVENT_TOPIC, TamperEvent.TAMPER_EVENT_TOPIC,
EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL, EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL };
props.put(EventConstants.EVENT_TOPIC, eventTopics);
this.cloudServiceRegistration = this.ctx.getBundleContext().registerService(EventHandler.class.getName(), this,
Expand Down Expand Up @@ -422,11 +419,6 @@ public void handleEvent(Event event) {
return;
}

if (ModemReadyEvent.MODEM_EVENT_READY_TOPIC.contains(topic)) {
handleModemReadyEvent(event);
return;
}

if (TamperEvent.TAMPER_EVENT_TOPIC.equals(topic) && this.dataService.isConnected()
&& this.options.getRepubBirthCertOnTamperEvent()) {
logger.debug("CloudServiceImpl: received tamper event, publishing BIRTH.");
Expand All @@ -449,35 +441,6 @@ private void tryPublishBirthCertificate(boolean isNewConnection) {
}
}

private void handleModemReadyEvent(Event event) {
logger.info("Handling ModemReadyEvent");
ModemReadyEvent modemReadyEvent = (ModemReadyEvent) event;
// keep these identifiers around until we can publish the certificate
this.imei = (String) modemReadyEvent.getProperty(ModemReadyEvent.IMEI);
this.imsi = (String) modemReadyEvent.getProperty(ModemReadyEvent.IMSI);
this.iccid = (String) modemReadyEvent.getProperty(ModemReadyEvent.ICCID);
this.rssi = (String) modemReadyEvent.getProperty(ModemReadyEvent.RSSI);
this.modemFwVer = (String) modemReadyEvent.getProperty(ModemReadyEvent.FW_VERSION);
logger.trace("handleEvent() :: IMEI={}", this.imei);
logger.trace("handleEvent() :: IMSI={}", this.imsi);
logger.trace("handleEvent() :: ICCID={}", this.iccid);
logger.trace("handleEvent() :: RSSI={}", this.rssi);
logger.trace("handleEvent() :: FW_VERSION={}", this.modemFwVer);

if (this.dataService.isConnected() && this.options.getRepubBirthCertOnModemDetection() && isModemInfoValid()) {
logger.debug("handleEvent() :: publishing BIRTH certificate ...");
tryPublishBirthCertificate(false);
}
}

private boolean isModemInfoValid(final String modemInfo) {
return !(modemInfo == null || modemInfo.length() == 0 || modemInfo.equals(ERROR));
}

public boolean isModemInfoValid() {
return isModemInfoValid(this.imei) && isModemInfoValid(this.imsi) && isModemInfoValid(this.iccid);
}

private void handlePositionLockedEvent() {
// if we get a position locked event,
// republish the birth certificate only if we are configured to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2024 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
Expand Down Expand Up @@ -37,7 +37,6 @@ public class CloudServiceOptions {
private static final String DEVICE_CUSTOM_NAME = "device.custom-name";
private static final String ENCODE_GZIP = "encode.gzip";
private static final String REPUB_BIRTH_ON_GPS_LOCK = "republish.mqtt.birth.cert.on.gps.lock";
private static final String REPUB_BIRTH_ON_MODEM_DETECT = "republish.mqtt.birth.cert.on.modem.detect";
private static final String REPUB_BIRTH_ON_TAMPER_EVENT = "republish.mqtt.birth.cert.on.tamper.event";
private static final String ENABLE_DFLT_SUBSCRIPTIONS = "enable.default.subscriptions";
private static final String PAYLOAD_ENCODING = "payload.encoding";
Expand Down Expand Up @@ -116,21 +115,6 @@ public boolean getRepubBirthCertOnGpsLock() {
return repubBirth;
}

/**
* Returns true if the current CloudService configuration
* specifies the cloud client should republish the MQTT birth
* certificate on modem detection events.
*
* @return a boolean value.
*/
public boolean getRepubBirthCertOnModemDetection() {
boolean repubBirth = false;
if (this.properties != null && this.properties.get(REPUB_BIRTH_ON_MODEM_DETECT) instanceof Boolean) {
repubBirth = (Boolean) this.properties.get(REPUB_BIRTH_ON_MODEM_DETECT);
}
return repubBirth;
}

public boolean getRepubBirthCertOnTamperEvent() {
boolean repubBirth = true;
if (this.properties != null && this.properties.get(REPUB_BIRTH_ON_TAMPER_EVENT) instanceof Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@
import org.eclipse.kura.data.transport.listener.DataTransportListener;
import org.eclipse.kura.message.KuraBirthPayload;
import org.eclipse.kura.message.KuraDeviceProfile;
import org.eclipse.kura.net.modem.ModemReadyEvent;
import org.eclipse.kura.net.status.NetworkStatusService;
import org.eclipse.kura.net.status.modem.ModemConnectionStatus;
import org.eclipse.kura.net.status.modem.ModemInterfaceStatus;
import org.eclipse.kura.net.status.modem.Sim;
import org.eclipse.kura.net.status.modem.ModemInterfaceStatus.ModemInterfaceStatusBuilder;
import org.eclipse.kura.net.status.modem.Sim;
import org.eclipse.kura.security.tamper.detection.TamperDetectionService;
import org.eclipse.kura.security.tamper.detection.TamperEvent;
import org.eclipse.kura.security.tamper.detection.TamperStatus;
Expand Down Expand Up @@ -80,10 +79,6 @@ public class CloudServiceTest {
private static final String MODEM_IMEI = "modem_imei";
private static final String TAMPER_STATUS = "tamper_status";
private static final String MODEM_FIRMWARE_VERSION = "modem_firmware_version";
private static final String FOO_FW_VER = "fooFwVer";
private static final String FOO_ICCID = "fooIccid";
private static final String FOO_IMSI = "fooImsi";
private static final String FOO_IMEI = "fooImei";
private static final String FOO_FW_VER1 = "fooFwVer1";
private static final String FOO_ICCID1 = "fooIccid1";
private static final String FOO_IMSI1 = "fooImsi1";
Expand Down Expand Up @@ -247,22 +242,11 @@ public void shouldSupportAdditionalBirthProperties()
throws InterruptedException, ExecutionException, TimeoutException, KuraException, InvalidSyntaxException {

clearNetworkStatusServiceMock();
final Map<String, Object> properties = new HashMap<>();

properties.put(ModemReadyEvent.IMEI, FOO_IMEI);
properties.put(ModemReadyEvent.IMSI, FOO_IMSI);
properties.put(ModemReadyEvent.ICCID, FOO_ICCID);
properties.put(ModemReadyEvent.RSSI, "0");
properties.put(ModemReadyEvent.FW_VERSION, FOO_FW_VER);

final ModemReadyEvent modemReadyEvent = new ModemReadyEvent(properties);

eventAdmin.sendEvent(modemReadyEvent);
cloudServiceImpl.setSystemService(createMockSystemService(Optional.empty()));
final JsonObject metrics = publishBirthAndGetMetrics();

assertEquals("getCpuVersion", metrics.get(KuraDeviceProfile.CPU_VERSION_KEY).asString());
assertEquals(FOO_FW_VER, metrics.get(MODEM_FIRMWARE_VERSION).asString());

}

Expand Down Expand Up @@ -380,32 +364,6 @@ public void shouldRepublishBirthOnTamperEvent()
}
}

@Test
public void shouldPublishBirthMessageWithModemInfoWhenEventReceived()
throws InterruptedException, ExecutionException, TimeoutException, KuraException, InvalidSyntaxException {

clearNetworkStatusServiceMock();
final Map<String, Object> properties = new HashMap<>();

properties.put(ModemReadyEvent.IMEI, FOO_IMEI);
properties.put(ModemReadyEvent.IMSI, FOO_IMSI);
properties.put(ModemReadyEvent.ICCID, FOO_ICCID);
properties.put(ModemReadyEvent.RSSI, "0");
properties.put(ModemReadyEvent.FW_VERSION, FOO_FW_VER);

final ModemReadyEvent modemReadyEvent = new ModemReadyEvent(properties);

eventAdmin.sendEvent(modemReadyEvent);
final JsonObject metrics = publishBirthAndGetMetrics();

assertEquals(FOO_IMEI, metrics.get(MODEM_IMEI).asString());
assertEquals(FOO_IMSI, metrics.get(MODEM_IMSI).asString());
assertEquals(FOO_ICCID, metrics.get(MODEM_ICCID).asString());
assertEquals("0", metrics.get(MODEM_RSSI).asString());
assertEquals(FOO_FW_VER, metrics.get(MODEM_FIRMWARE_VERSION).asString());

}

@Test
public void shouldPublishBirthMessageWithModemInfoWhenNetworkStatusServiceIsPresent()
throws InterruptedException, ExecutionException, TimeoutException, KuraException, InvalidSyntaxException {
Expand Down
Loading

0 comments on commit 6a47cf4

Please sign in to comment.