Skip to content

Commit

Permalink
Add ComponentName to message properties (#5234) (#5258)
Browse files Browse the repository at this point in the history
This adds support for the new ComponentName message property to edgeHub. This is a property used by pnp devices and was added to the SDK already
  • Loading branch information
lfitchett authored Jul 17, 2021
1 parent a48271f commit 4f36aba
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ byte[] GetMessageBody()
systemProperties.AddIfNonEmpty(SystemProperties.InterfaceId, hubInterfaceId);
}

if (sourceMessage.MessageAnnotations.Map.TryGetValue(SystemProperties.ComponentName, out string componentName))
{
systemProperties.AddIfNonEmpty(SystemProperties.ComponentName, componentName);
}

if (sourceMessage.ApplicationProperties != null)
{
foreach (KeyValuePair<MapKey, object> property in sourceMessage.ApplicationProperties.Map)
Expand Down Expand Up @@ -188,6 +193,11 @@ public AmqpMessage FromMessage(IMessage message)
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsConnectionModuleId] = connectionModuleId;
}

if (message.SystemProperties.TryGetNonEmptyValue(SystemProperties.ComponentName, out string componentName))
{
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsComponentName] = componentName;
}

if (message.SystemProperties.TryGetNonEmptyValue(SystemProperties.MessageSchema, out string messageSchema))
{
amqpMessage.ApplicationProperties.Map[Constants.MessagePropertiesMessageSchemaKey] = messageSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static class Constants
public const string MessageAnnotationsInputNameKey = "x-opt-input-name";
public const string MessageAnnotationsConnectionDeviceId = "iothub-connection-device-id";
public const string MessageAnnotationsConnectionModuleId = "iothub-connection-module-id";
public const string MessageAnnotationsComponentName = "dt-subject";
public const string WebSocketSubProtocol = "AMQPWSB10";
public const string WebSocketListenerName = WebSocketSubProtocol + "-listener";
public const string ServiceBusCbsSaslMechanismName = "MSSBCBS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public Message FromMessage(IMessage inputMessage)
message.MessageSchema = messageSchema;
}

if (inputMessage.SystemProperties.TryGetNonEmptyValue(SystemProperties.ComponentName, out string componentName))
{
message.ComponentName = componentName;
}

if (inputMessage.SystemProperties.TryGetNonEmptyValue(SystemProperties.InterfaceId, out string interfaceId)
&& interfaceId.Equals(Constants.SecurityMessageIoTHubInterfaceId, StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -95,6 +100,7 @@ public IMessage ToMessage(Message sourceMessage)
message.SystemProperties.AddIfNonEmpty(SystemProperties.MessageSchema, sourceMessage.MessageSchema);
message.SystemProperties.AddIfNonEmpty(SystemProperties.LockToken, sourceMessage.LockToken);
message.SystemProperties.AddIfNonEmpty(SystemProperties.DeliveryCount, sourceMessage.DeliveryCount.ToString());
message.SystemProperties.AddIfNonEmpty(SystemProperties.ComponentName, sourceMessage.ComponentName);

if (sourceMessage.SequenceNumber > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class SystemProperties
public const string SequenceNumber = "sequenceNumber";
public const string InterfaceId = "iothub-interface-id";
public const string ModelId = "modelId";
public const string ComponentName = "dt-subject";

public const string RpConnectionDeviceIdInternal = "rpSenderDeviceId";
public const string RpConnectionModuleIdInternal = "rpSenderModuleId";
Expand All @@ -53,7 +54,8 @@ public static class SystemProperties
{ OnTheWireSystemPropertyNames.MessageSchemaOnTheWireName, MessageSchema },
{ OnTheWireSystemPropertyNames.OperationOnTheWireName, Operation },
{ OnTheWireSystemPropertyNames.CreationTimeOnTheWireName, CreationTime },
{ OnTheWireSystemPropertyNames.InterfaceIdOnTheWireName, InterfaceId }
{ OnTheWireSystemPropertyNames.InterfaceIdOnTheWireName, InterfaceId },
{ OnTheWireSystemPropertyNames.ComponentNameOnTheWireName, ComponentName }
};

public static readonly Dictionary<string, string> OutgoingSystemPropertiesMap = new Dictionary<string, string>
Expand All @@ -71,6 +73,7 @@ public static class SystemProperties
{ CreationTime, OnTheWireSystemPropertyNames.CreationTimeOnTheWireName },
{ ConnectionDeviceId, OnTheWireSystemPropertyNames.ConnectionDeviceIdOnTheWireName },
{ ConnectionModuleId, OnTheWireSystemPropertyNames.ConnectionModuleIdOnTheWireName },
{ ComponentName, OnTheWireSystemPropertyNames.ComponentNameOnTheWireName },
{ RpConnectionDeviceIdInternal, OnTheWireSystemPropertyNames.ConnectionDeviceIdOnTheWireName },
{ RpConnectionModuleIdInternal, OnTheWireSystemPropertyNames.ConnectionModuleIdOnTheWireName }
};
Expand All @@ -92,6 +95,7 @@ static class OnTheWireSystemPropertyNames
public const string CreationTimeOnTheWireName = "$.ctime";
public const string OperationOnTheWireName = "iothub-operation";
public const string InterfaceIdOnTheWireName = "$.ifid";
public const string ComponentNameOnTheWireName = "$.sub";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public static class SystemProperties
public const string AuthMethod = "connectionAuthMethod";
public const string ContentType = "contentType";
public const string ContentEncoding = "contentEncoding";
public const string ComponentName = "componentName";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public void ToMessageTest_AllProperties()
string messageSchema = "testSchema";
string operation = "foo";
string outputName = "output1";
string componentName = "testComponent";

using (AmqpMessage amqpMessage = AmqpMessage.Create(new Data { Value = new ArraySegment<byte>(bytes) }))
{
Expand All @@ -122,6 +123,7 @@ public void ToMessageTest_AllProperties()
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsDeliveryCountKey] = deliveryCount;
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsLockTokenName] = lockToken;
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsSequenceNumberName] = sequenceNumber;
amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsComponentName] = componentName;

amqpMessage.ApplicationProperties.Map[Constants.MessagePropertiesMessageSchemaKey] = messageSchema;
amqpMessage.ApplicationProperties.Map[Constants.MessagePropertiesCreationTimeKey] = creationTime;
Expand All @@ -140,7 +142,7 @@ public void ToMessageTest_AllProperties()
// Assert
Assert.NotNull(receivedMessage);
Assert.Equal(receivedMessage.Body, bytes);
Assert.Equal(15, receivedMessage.SystemProperties.Count);
Assert.Equal(16, receivedMessage.SystemProperties.Count);
Assert.Equal(2, receivedMessage.Properties.Count);

Assert.Equal(receivedMessage.SystemProperties[SystemProperties.MessageId], messageId);
Expand All @@ -158,6 +160,7 @@ public void ToMessageTest_AllProperties()
Assert.Equal(receivedMessage.SystemProperties[SystemProperties.CreationTime], creationTime);
Assert.Equal(receivedMessage.SystemProperties[SystemProperties.Operation], operation);
Assert.Equal(receivedMessage.SystemProperties[SystemProperties.OutputName], outputName);
Assert.Equal(receivedMessage.SystemProperties[SystemProperties.ComponentName], componentName);

Assert.Equal("Value1", receivedMessage.Properties["Prop1"]);
Assert.Equal("Value2", receivedMessage.Properties["Prop2"]);
Expand Down Expand Up @@ -270,6 +273,7 @@ public void FromMessageTest_AllProperties()
string outputName = "outputName";
string connectionDeviceId = "edgeDevice1";
string connectionModuleId = "module1";
string componentName = "testComponent";

var systemProperties = new Dictionary<string, string>
{
Expand All @@ -290,7 +294,8 @@ public void FromMessageTest_AllProperties()
[SystemProperties.InputName] = inputName,
[SystemProperties.OutputName] = outputName,
[SystemProperties.ConnectionDeviceId] = connectionDeviceId,
[SystemProperties.ConnectionModuleId] = connectionModuleId
[SystemProperties.ConnectionModuleId] = connectionModuleId,
[SystemProperties.ComponentName] = componentName,
};

var properties = new Dictionary<string, string>
Expand Down Expand Up @@ -333,6 +338,7 @@ byte[] GetMessageBody(AmqpMessage sourceMessage)
Assert.Equal(inputName, amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsInputNameKey]);
Assert.Equal(connectionDeviceId, amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsConnectionDeviceId]);
Assert.Equal(connectionModuleId, amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsConnectionModuleId]);
Assert.Equal(componentName, amqpMessage.MessageAnnotations.Map[Constants.MessageAnnotationsComponentName]);

Assert.Equal(messageSchema, amqpMessage.ApplicationProperties.Map[Constants.MessagePropertiesMessageSchemaKey]);
Assert.Equal(creationTime, amqpMessage.ApplicationProperties.Map[Constants.MessagePropertiesCreationTimeKey]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public void TestFromMessage_AllSystemProperties()
[SystemProperties.MsgCorrelationId] = "1234",
[SystemProperties.MessageId] = "m1",
[SystemProperties.CreationTime] = creationTime,
[SystemProperties.InterfaceId] = Constants.SecurityMessageIoTHubInterfaceId
[SystemProperties.InterfaceId] = Constants.SecurityMessageIoTHubInterfaceId,
[SystemProperties.ComponentName] = "Test Component"
};

var message = Mock.Of<IMessage>(
Expand All @@ -159,6 +160,7 @@ public void TestFromMessage_AllSystemProperties()
Assert.Equal("m1", clientMessage.MessageId);
Assert.Equal(creationTime, clientMessage.CreationTimeUtc.ToString("o"));
Assert.True(clientMessage.IsSecurityMessage);
Assert.Equal("Test Component", clientMessage.ComponentName);
}

[Unit]
Expand All @@ -180,6 +182,7 @@ public void TestToMessage_AllSystemProperties()
clientMessage.CorrelationId = "1234";
clientMessage.MessageId = "m1";
clientMessage.CreationTimeUtc = creationTime;
clientMessage.ComponentName = "Test Component";

IMessageConverter<Message> messageConverter = new DeviceClientMessageConverter();
IMessage message = messageConverter.ToMessage(clientMessage);
Expand All @@ -189,7 +192,7 @@ public void TestToMessage_AllSystemProperties()
Assert.Equal("Bar", message.Properties["Foo"]);
Assert.Equal("Value2", message.Properties["Prop2"]);

Assert.Equal(10, message.SystemProperties.Count);
Assert.Equal(11, message.SystemProperties.Count);
Assert.Equal("utf-8", message.SystemProperties[SystemProperties.ContentEncoding]);
Assert.Equal("application/json", message.SystemProperties[SystemProperties.ContentType]);
Assert.Equal("schema1", message.SystemProperties[SystemProperties.MessageSchema]);
Expand All @@ -199,6 +202,7 @@ public void TestToMessage_AllSystemProperties()
Assert.Equal(creationTime.ToString("o"), message.SystemProperties[SystemProperties.CreationTime]);
Assert.Equal(DateTime.Parse(message.SystemProperties[SystemProperties.CreationTime], null, DateTimeStyles.RoundtripKind), creationTime);
Assert.Equal("0", message.SystemProperties[SystemProperties.DeliveryCount]);
Assert.Equal("Test Component", message.SystemProperties[SystemProperties.ComponentName]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public void TestToMessage_AllSystemProperties()
address.Append($"&{HttpUtility.UrlEncode("$.schema")}=someschema");
// creation time
address.Append($"&{HttpUtility.UrlEncode("$.ctime")}={HttpUtility.UrlEncode("2018-01-31")}");
// component name
address.Append($"&{HttpUtility.UrlEncode("$.sub")}=testComponent");

// add custom properties
address.Append("&Foo=Bar&Prop2=Value2&Prop3=Value3/");
Expand All @@ -154,7 +156,7 @@ public void TestToMessage_AllSystemProperties()
IMessage message = protocolGatewayMessageConverter.ToMessage(protocolGatewayMessage);
Assert.NotNull(message);

Assert.Equal(10, message.SystemProperties.Count);
Assert.Equal(11, message.SystemProperties.Count);
Assert.Equal("1234", message.SystemProperties[SystemProperties.MsgCorrelationId]);
Assert.Equal("mid1", message.SystemProperties[SystemProperties.MessageId]);
Assert.Equal("d2", message.SystemProperties[SystemProperties.To]);
Expand All @@ -165,6 +167,7 @@ public void TestToMessage_AllSystemProperties()
Assert.Equal("someschema", message.SystemProperties[SystemProperties.MessageSchema]);
Assert.Equal("2018-01-31", message.SystemProperties[SystemProperties.CreationTime]);
Assert.Equal("Device_6", message.SystemProperties[SystemProperties.ConnectionDeviceId]);
Assert.Equal("testComponent", message.SystemProperties[SystemProperties.ComponentName]);

Assert.Equal(3, message.Properties.Count);
Assert.Equal("Bar", message.Properties["Foo"]);
Expand Down Expand Up @@ -215,7 +218,8 @@ public void TestFromMessage_AllSystemProperties()
[SystemProperties.MsgCorrelationId] = "1234",
[SystemProperties.MessageId] = "m1",
[SystemProperties.ConnectionDeviceId] = "fromDevice1",
[SystemProperties.ConnectionModuleId] = "fromModule1"
[SystemProperties.ConnectionModuleId] = "fromModule1",
[SystemProperties.ComponentName] = "testComponent"
};

var message = Mock.Of<IMessage>(
Expand All @@ -227,8 +231,8 @@ public void TestFromMessage_AllSystemProperties()
var protocolGatewayMessageConverter = new ProtocolGatewayMessageConverter(converter, ByteBufferConverter);
IProtocolGatewayMessage pgMessage = protocolGatewayMessageConverter.FromMessage(message);
Assert.NotNull(pgMessage);
Assert.Equal(@"devices/Device1/modules/Module1/inputs/input1/Foo=Bar&Prop2=Value2&Prop3=Value3&%24.ce=utf-8&%24.ct=application%2Fjson&%24.schema=schema1&%24.to=foo&%24.uid=user1&%24.cid=1234&%24.mid=m1&%24.cdid=fromDevice1&%24.cmid=fromModule1", pgMessage.Address);
Assert.Equal(12, pgMessage.Properties.Count);
Assert.Equal(@"devices/Device1/modules/Module1/inputs/input1/Foo=Bar&Prop2=Value2&Prop3=Value3&%24.ce=utf-8&%24.ct=application%2Fjson&%24.schema=schema1&%24.to=foo&%24.uid=user1&%24.cid=1234&%24.mid=m1&%24.cdid=fromDevice1&%24.cmid=fromModule1&%24.sub=testComponent", pgMessage.Address);
Assert.Equal(13, pgMessage.Properties.Count);
Assert.Equal("Bar", pgMessage.Properties["Foo"]);
Assert.Equal("Value2", pgMessage.Properties["Prop2"]);
Assert.Equal("Value3", pgMessage.Properties["Prop3"]);
Expand All @@ -241,6 +245,7 @@ public void TestFromMessage_AllSystemProperties()
Assert.Equal("m1", pgMessage.Properties["$.mid"]);
Assert.Equal("fromDevice1", pgMessage.Properties["$.cdid"]);
Assert.Equal("fromModule1", pgMessage.Properties["$.cmid"]);
Assert.Equal("testComponent", pgMessage.Properties["$.sub"]);
Assert.False(pgMessage.Properties.ContainsKey("$.on"));
Assert.True(DateTime.UtcNow - pgMessage.CreatedTimeUtc < TimeSpan.FromSeconds(3));
}
Expand Down

0 comments on commit 4f36aba

Please sign in to comment.