@startuml
'https://plantuml.com/sequence-diagram

autonumber
box Device2 #white
	actor uApp #red
	entity uSubscription as SM1 #brown
end box
box Device1 #white
	entity uSubscription as SM2 #brown
	entity uExample #blue
end box

uApp -> SM1: Unsubscribe(\nUnsubscribeRequest)
note right
 	<size:12>**UnsubscribeRequest:**</size>
	""topic:"" { ""uri"": ""//topic//"" }
	""subscriber"": { ""uri"": " up://Device2/uApp/" }
end note
|||
== Check if the last local subscriber has Unsubscribed ==
alt uApp is the last subscriber
 	SM1 --> uApp: Status
	note right
		<size:12>**Status:**</size>
		""code"": ""OK""
	end note
	SM1 -> SM1
 	note right
		""SubscriptionStatus.state"" ==
		""UNSUBSCRIBE_PENDING""
	end note
	SM1 -> SM2: Unsubscribe(UnsubscribeRequest)
	note right
 		<size:12>**UnsubscribeRequest:**</size>
		""topic:"" { ""uri"": ""//topic//"" }
		""subscriber"": { ""uri"": " up://Device2/core.usubscription/" }
	end note
	|||
	alt Success
 		SM2 --> SM1: Status
		note right
			<size:12>**Status:**</size>
			""code"": ""OK""
		end note
 		SM2 -[#0000FF]-\ uExample: <font color=blue>Update</font>
		note right
			<size:12>**Update:**</size>
			""topic:"" { ""uri"": ""//topic//"" }
			""subscriber"": { ""uri"": " up://Device2/core.usubscription/" }
			""status:"" {
				\t""state"": ""UNSUBSCRIBED"",
				\t""code"": ""OK""
			}
		end note
	|||
	else Failure
		SM2 --> SM1: Status
		note right
			<size:12>**Status:**</size>
			""code"": ""INVALID_ARGUMENT | NOT_FOUND | PERMISSION_DENIED""
		end note
	end alt
|||
else uApp is NOT the last subscriber
	alt Success
		SM1--> uApp: Status
		note right
			<size:12>**Status:**</size>
			""code"": ""OK""
		end note
	else Failure
		SM1 --> uApp: Status
		note right
			<size:12>**Status:**</size>
			""code"": ""INVALID_ARGUMENT | NOT_FOUND | PERMISSION_DENIED""
		end note
	end alt
end alt
@enduml