@startuml configure scan
!include mid-cbf-puml-definitions.txt

title Configure Scan (Correlation)\n

' participant "TMC\n" as tmc #Gold
participant "CSP_Mid\n.LMC" as lmc #Thistle
box "\nMCS\n"
    participant "Mid.CBF\nController" as controller
    participant "Mid.CBF\nSubarray" as subarray
    collections "VCC\nUnit" as vcc
    collections "FSP\nUnit" as fsp_unit
    collections "FSP Corr\nSubarray" as fspcorr
    participant "Visibility\nTransport" as vis_trans
end box
box "\nFHS VCC\n" 
    participant "VCC All Bands\nController" as vcc_cont
end box
box "\nFHS FSP\n"
    participant "FSP\nController" as fsp_cont
    participant "FSP Corr\nController" as fsp_corr_ctrl
    participant "FSP PST\nController" as fsp_pst_ctrl
    participant "FSP PSS\nController" as fsp_pss_ctrl
    participant "FPGA Monitor\nand Control" as fpga
end box

lmc         -> subarray     : ConfigureScan(json_str)
subarray    -> subarray     : Validate JSON against schema,\nLoad new configuration

' group #LightCyan For each FSP:
'     subarray    -> fsp          : RemoveSubarrayMembership
'     group #SeaShell If len(subarrayMembership) == 0:
'         fsp         -> fspcorr      : AdminMode.OFFLINE
'         note over fspcorr           : OpState: DISABLE
'         fsp         -> fsp_cont     : Set function mode to IDLE
'         note over fsp_cont          : functionMode: IDLE
'         note over fsp               : functionMode: IDLE
'         subarray    -> fsp          : AdminMode.OFFLINE
'         note over fsp             : OpState: DISABLE
'     end group
' end group

' group #LemonChiffon Delay model subscription point
'     subarray    -> tmc          : Unsubscribe all events
' end group

' subarray    -> subarray     : Clear old configuration

' group #LemonChiffon Delay model subscription point
'     subarray    -> tmc          : Subscribe delay model
' end group

group #LightCyan For each assigned receptor:
    ' subarray    -> vcc          : ConfigureBand(json_str)
    ' vcc         -> vcc_cont     : ConfigureBand(int)
    subarray ->  vcc ++       : ConfigureScan(json_str)
    vcc ->  vcc_cont          : ConfigureScan(json_str)
    vcc <-- vcc_cont          : //result//
    subarray <-- vcc --       : //result// 
    note over vcc             : ObsState: READY
end group

group #LightCyan For each FSP in use:
    subarray  -> subarray     : Build FSP\nconfiguration

    subarray -> fsp_unit      : FunctionMode?

    alt If FunctionMode == IDLE (not part of a subarray)
        subarray <-- fsp_unit : IDLE
        subarray -> fsp_unit  : AdminMode.ONLINE
        note over fsp_unit    : OpState: ON
        subarray -> fsp_unit ++    : SetFunctionMode(str)
        fsp_unit      -> fsp_unit      : Validate FunctionMode
        loop #LightCyan each FSP Controller in the FSP Unit
            fsp_unit ->  fsp_cont  ++  : SetFunctionMode(int)            
            fsp_cont -> fsp_pst_ctrl   : AdminMode.OFFLINE
            fsp_cont -> fsp_pss_ctrl   : AdminMode.OFFLINE
            fsp_cont ->  fpga          : Re-program Corr bitstream
            fsp_cont <-- fpga          : //result//
            fsp_cont -> fsp_corr_ctrl  : AdminMode.ONLINE
            note over fsp_corr_ctrl: Sets communication\nstatus to established.
            fsp_unit <-- fsp_cont --   : //result//
        end loop
        subarray <-- fsp_unit --  : //result//
        note over fsp_cont        : functionMode: CORR
        note over fsp_unit        : functionMode: CORR 
    else #LightGreen FunctionMode == CORR
        subarray <-- fsp_unit     : CORR
        note over fsp_unit: NOP
    else #LightSalmon FunctionMode == PST or FunctionMode == PSS
        subarray <-- fsp_unit      : PST or PSS
        note over fsp_unit: Not allowed
    end alt

    subarray  -> fsp_unit ++  : AddSubarrayMembership(int)
    fsp_unit  -> fsp_unit     : Validate subarrayID
    fsp_unit   -> fspcorr     : AdminMode.ONLINE
    note over fspcorr         : OpState: ON
    subarray <-- fsp_unit --  : //result//

    subarray -> fspcorr ++    : ConfigureScan(json_str)
    ' fspcorr     -> fspcorr      : Reset old configuration,\nRelease all VCCs,\nLoad FSP configuration,\nAssign new VCCs,\nBuild HPS FSP CORR configuration
    fspcorr ->  fsp_corr_ctrl : ConfigureScan(json_str)
    fspcorr <-- fsp_corr_ctrl : //result//
    note over fspcorr         : ObsState: READY
    subarray <-- fspcorr --   : //result//
end group

subarray    -> vis_trans    : Configure(json_str, yaml_str)

note over subarray          : ObsState: READY
lmc        <-- subarray     : Success

@enduml