-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d193584
commit d790223
Showing
14 changed files
with
1,436 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...service/src/main/kotlin/com/egm/stellio/subscription/service/mqtt/MQTTNotificationData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.egm.stellio.subscription.service.mqtt | ||
|
||
import com.egm.stellio.subscription.model.Notification | ||
|
||
data class MQTTNotificationData( | ||
val topic: String, | ||
val mqttMessage: MqttMessage, | ||
val qos: Int, | ||
val brokerUrl: String, | ||
val clientId: String, | ||
val username: String, | ||
val password: String? = null, | ||
) { | ||
|
||
data class MqttMessage( | ||
val body: Notification, | ||
val metadata: Map<String, String> = emptyMap(), | ||
) | ||
} |
68 changes: 68 additions & 0 deletions
68
...vice/src/main/kotlin/com/egm/stellio/subscription/service/mqtt/MQTTNotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.egm.stellio.subscription.service.mqtt | ||
|
||
import com.egm.stellio.shared.model.BadSchemeException | ||
import com.egm.stellio.subscription.model.Notification | ||
import com.egm.stellio.subscription.model.Subscription | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import org.eclipse.paho.client.mqttv3.MqttException as MqttExceptionV3 | ||
import org.eclipse.paho.mqttv5.common.MqttException as MqttExceptionV5 | ||
|
||
@Service | ||
class MQTTNotificationService( | ||
@Value("\${mqtt.clientId}") | ||
private val clientId: String = "stellio-context-brokerUrl", | ||
private val mqttVersionService: MQTTVersionService | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(javaClass) | ||
|
||
suspend fun mqttNotifier( | ||
subscription: Subscription, | ||
notification: Notification, | ||
headers: Map<String, String> | ||
): Boolean { | ||
val endpoint = subscription.notification.endpoint | ||
val uri = endpoint.uri | ||
val userInfo = uri.userInfo.split(':') | ||
val username = userInfo.getOrNull(0) ?: "" | ||
val password = userInfo.getOrNull(1) | ||
val brokerScheme = Mqtt.SCHEME.brokerSchemeMap[uri.scheme] | ||
?: throw BadSchemeException("${uri.scheme} is not a valid mqtt scheme") | ||
|
||
val brokerPort = if (uri.port != -1) uri.port else Mqtt.SCHEME.defaultPortMap[uri.scheme] | ||
|
||
val brokerUrl = "$brokerScheme://${uri.host}:$brokerPort" | ||
val notifierInfo = endpoint.notifierInfo?.map { it.key to it.value }?.toMap() ?: emptyMap() | ||
val qos = | ||
notifierInfo[Mqtt.QualityOfService.KEY]?.let { Integer.parseInt(it) } ?: Mqtt.QualityOfService.AT_MOST_ONCE | ||
|
||
val data = MQTTNotificationData( | ||
topic = uri.path, | ||
brokerUrl = brokerUrl, | ||
clientId = clientId, | ||
qos = qos, | ||
mqttMessage = MQTTNotificationData.MqttMessage(notification, headers), | ||
username = username, | ||
password = password | ||
) | ||
|
||
try { | ||
val mqttVersion = notifierInfo[Mqtt.Version.KEY] | ||
when (mqttVersion) { | ||
Mqtt.Version.V3 -> mqttVersionService.callMqttV3(data) | ||
Mqtt.Version.V5 -> mqttVersionService.callMqttV5(data) | ||
else -> mqttVersionService.callMqttV5(data) | ||
} | ||
logger.info("successfull mqtt notification for uri : ${data.brokerUrl} version: $mqttVersion") | ||
return true | ||
} catch (e: MqttExceptionV3) { | ||
logger.error("failed mqttv3 notification for uri : ${data.brokerUrl}", e) | ||
return false | ||
} catch (e: MqttExceptionV5) { | ||
logger.error("failed mqttv5 notification for uri : ${data.brokerUrl}", e) | ||
return false | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...n-service/src/main/kotlin/com/egm/stellio/subscription/service/mqtt/MQTTVersionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.egm.stellio.subscription.service.mqtt | ||
|
||
import com.egm.stellio.shared.util.JsonUtils.serializeObject | ||
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions | ||
import org.springframework.stereotype.Service | ||
import org.eclipse.paho.client.mqttv3.MqttClient as MqttClientv3 | ||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions as MqttConnectOptionsv3 | ||
import org.eclipse.paho.client.mqttv3.MqttMessage as MqttMessagev3 | ||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence as MemoryPersistencev3 | ||
import org.eclipse.paho.mqttv5.client.IMqttToken as IMqttTokenv5 | ||
import org.eclipse.paho.mqttv5.client.MqttAsyncClient as MqttAsyncClientv5 | ||
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence as MemoryPersistencev5 | ||
import org.eclipse.paho.mqttv5.common.MqttMessage as MqttMessagev5 | ||
|
||
@Service | ||
class MQTTVersionService { | ||
|
||
internal suspend fun callMqttV3(data: MQTTNotificationData) { | ||
val persistence = MemoryPersistencev3() | ||
val sampleClient = MqttClientv3(data.brokerUrl, data.clientId, persistence) | ||
val connOpts = MqttConnectOptionsv3() | ||
connOpts.isCleanSession = true | ||
connOpts.userName = data.username | ||
connOpts.password = data.password?.toCharArray() ?: "".toCharArray() | ||
sampleClient.connect(connOpts) | ||
val message = MqttMessagev3( | ||
serializeObject(data.mqttMessage).toByteArray() | ||
) | ||
message.qos = data.qos | ||
sampleClient.publish(data.topic, message) | ||
sampleClient.disconnect() | ||
} | ||
|
||
internal suspend fun callMqttV5(data: MQTTNotificationData) { | ||
val persistence = MemoryPersistencev5() | ||
val sampleClient = MqttAsyncClientv5(data.brokerUrl, data.clientId, persistence) | ||
val connOpts = MqttConnectionOptions() | ||
connOpts.isCleanStart = true | ||
connOpts.userName = data.username | ||
connOpts.password = data.password?.toByteArray() | ||
var token: IMqttTokenv5 = sampleClient.connect(connOpts) | ||
token.waitForCompletion() | ||
val message = MqttMessagev5(serializeObject(data.mqttMessage).toByteArray()) | ||
message.qos = data.qos | ||
token = sampleClient.publish(data.topic, message) | ||
token.waitForCompletion() | ||
sampleClient.disconnect() | ||
sampleClient.close() | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
subscription-service/src/main/kotlin/com/egm/stellio/subscription/service/mqtt/Mqtt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.egm.stellio.subscription.service.mqtt | ||
|
||
object Mqtt { | ||
|
||
object Version { | ||
const val KEY = "MQTT-Version" | ||
const val V3 = "mqtt3.1.1" | ||
const val V5 = "mqtt5.0" | ||
} | ||
|
||
object QualityOfService { | ||
const val KEY = "MQTT-QoS" | ||
const val AT_MOST_ONCE = 0 | ||
// const val AT_LEAST_ONCE = 1 | ||
// const val EXACTLY_ONCE = 2 | ||
} | ||
|
||
object SCHEME { | ||
const val MQTT = "mqtt" | ||
const val MQTTS = "mqtts" | ||
const val MQTT_DEFAULT_PORT = 1883 | ||
const val MQTTS_DEFAULT_PORT = 8883 | ||
val defaultPortMap = mapOf(MQTT to MQTT_DEFAULT_PORT, MQTTS to MQTTS_DEFAULT_PORT) | ||
val brokerSchemeMap = mapOf(MQTT to "tcp", MQTTS to "ssl") | ||
} | ||
} |
21 changes: 8 additions & 13 deletions
21
subscription-service/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
spring.config.import=classpath:/shared.properties | ||
|
||
spring.r2dbc.url = r2dbc:postgresql://localhost/stellio_subscription | ||
spring.r2dbc.username = stellio | ||
spring.r2dbc.password = stellio_password | ||
|
||
spring.r2dbc.url=r2dbc:postgresql://localhost/stellio_subscription | ||
spring.r2dbc.username=stellio | ||
spring.r2dbc.password=stellio_password | ||
# Required for Flyway to know where the DB is located | ||
spring.flyway.url = jdbc:postgresql://localhost/stellio_subscription | ||
spring.flyway.user = ${spring.r2dbc.username} | ||
spring.flyway.password = ${spring.r2dbc.password} | ||
|
||
spring.flyway.url=jdbc:postgresql://localhost/stellio_subscription | ||
spring.flyway.user=${spring.r2dbc.username} | ||
spring.flyway.password=${spring.r2dbc.password} | ||
# Client registration used to get entities from search-service | ||
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=client_credentials | ||
spring.security.oauth2.client.registration.keycloak.client-id=client-id | ||
spring.security.oauth2.client.registration.keycloak.client-secret=client-secret | ||
spring.security.oauth2.client.provider.keycloak.token-uri=https://my.sso/token | ||
|
||
subscription.entity-service-url=http://localhost:8083 | ||
|
||
# Stellio url used to form the link to get the contexts associated to a notification | ||
subscription.stellio-url=http://localhost:8080 | ||
|
||
server.port = 8084 | ||
server.port=8084 | ||
mqtt.clientId=stellio-mqtt-client |
Oops, something went wrong.