@startuml
Participant "API Consumer"
participant "Apigee\n<On Prem>"
box "Products & Services"
database "Elastic Search\n<AWS>"
participant "Device API\n<PCF>"
participant "Preview Tool"
actor "Business User"
end box
Participant "Union API"
Participant "Tetra"
participant "Assurant API"

group GET Operation
"API Consumer"->"Apigee\n<On Prem>": TAC and IMEI Hash (GET)
note over "Apigee\n<On Prem>"
    IMEI is hashed using SHA3-256
end note
"Apigee\n<On Prem>"->"Device API\n<PCF>": TAC and IMEI Hash (GET /device-spec)
"Device API\n<PCF>"->"Elastic Search\n<AWS>": Lookup IMEI Hash
else IMEI Hash Found
"Elastic Search\n<AWS>"->"Device API\n<PCF>": Device Name or SKU
"Device API\n<PCF>"->"Device API\n<PCF>": Lookup Variant
note over "Device API\n<PCF>"
Variant is looked up in one of two ways
1) Device Name: index maps string to variant
2) SKU: lookup SKU directly in cache
end note
"Device API\n<PCF>"->"Apigee\n<On Prem>": Device Variant
"Apigee\n<On Prem>"-> "API Consumer": Device Variant
else IMEI Hash Not Found
"Elastic Search\n<AWS>"->"Device API\n<PCF>": IMEI Hash Not Found
"Device API\n<PCF>"->"Apigee\n<On Prem>" : 404 Device Resource Not Found
"Apigee\n<On Prem>" -> "API Consumer" : 404 Device Resource Not Found
end

group POST Operation
"API Consumer"-> "Apigee\n<On Prem>": IMEI (POST)
"Apigee\n<On Prem>"->"Device API\n<PCF>": IMEI (POST /device-specs)
"Device API\n<PCF>"->"Device API\n<PCF>": Hash IMEI
note over "Device API\n<PCF>"
    IMEI is hashed using SHA3-256
end note
"Device API\n<PCF>"->"Elastic Search\n<AWS>": Lookup IMEI Hash
note over "Device API\n<PCF>"
    Also checks a TAC blacklist
    specific to this POST operation
    if blacklisted (returns a 404?)
end note
group IMEI Hash Found
"Elastic Search\n<AWS>"->"Device API\n<PCF>": Device Name or SKU
"Device API\n<PCF>"->"Device API\n<PCF>": Lookup Variant
note over "Device API\n<PCF>"
Variant is looked up in one of two ways
1) Device Name: index maps string to variant
2) SKU: lookup SKU directly in cache
end note
"Device API\n<PCF>"->"Apigee\n<On Prem>": Device Variant
"Apigee\n<On Prem>"-> "API Consumer": Device Variant
end

group IMEI Hash Not Found
"Elastic Search\n<AWS>"->"Device API\n<PCF>": IMEI Hash Not Found
"Device API\n<PCF>" -> "Tetra": Lookup SKU for IMEI

group SKU Returned by Tetra
"Tetra" -> "Device API\n<PCF>": SKU
"Device API\n<PCF>"->"Device API\n<PCF>": Lookup Variant
"Device API\n<PCF>"->"Apigee\n<On Prem>": Device Variant
"Apigee\n<On Prem>"-> "API Consumer": Device Variant
"Device API\n<PCF>" -> "Elastic Search\n<AWS>": Store SKU / IMEI Hash Mapping
end

group SKU not Returned by Tetra
"Tetra" -> "Device API\n<PCF>": SKU not found
note over "Device API\n<PCF>"
If Tetra cannot return a SKU
or if Tetra returns a dummy SKU
then an alt lookup is performed
end note
"Device API\n<PCF>" -> "Device API\n<PCF>": Lookup Mfr. for TAC
else Apple Device 
"Device API\n<PCF>"->"Union API": IMEI (getAppleDeviceConfig)
"Union API"->"Device API\n<PCF>": Device Name (OEMConfiguration)
else Non-Apple Device
"Device API\n<PCF>"-> "Assurant API": IMEI
"Assurant API" -> "Device API\n<PCF>": Device Name

group Match to Variant
"Device API\n<PCF>"->"Device API\n<PCF>": Match Device Name\nto Device Variant
note over "Device API\n<PCF>"
Lookup variant for Device Name
If no mapping found then run matching logic
end note
"Device API\n<PCF>"->"Apigee\n<On Prem>": Device Variant
"Apigee\n<On Prem>"-> "API Consumer": Device Variant
"Device API\n<PCF>"->"Elastic Search\n<AWS>": Store Device Name / IMEI Hash Mapping
"Device API\n<PCF>"->"Device API\n<PCF>": Store the Device Name / Variant Mapping
end
end
end

group Union API Not Available
"Device API\n<PCF>"->"Union API": IMEI (getAppleDeviceConfig)
"Union API"->"Device API\n<PCF>": 503 Error
"Device API\n<PCF>"->"Apigee\n<On Prem>": 503 Error
"Apigee\n<On Prem>"-> "API Consumer": 503 Error
end
end

group Modify OEMConfiguration to Variant Mapping
"Business User" -> "Preview Tool": Modify Device Name /\nVariant Mappings
"Preview Tool" -> "Device API\n<PCF>": Update Device Name /\nVariant Mappings
end
@enduml