#!/usr/bin/env python3
# Copyright (c) 2020 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

import Agent
import BothTrace
import ServicePluginContext
import Tac
import Tracing
import os
from ControllerClient import ControllerClient
from GenericReactor import GenericReactor

__defaultTraceHandle__ = Tracing.Handle( 'TwinCvxClient' )
bv = BothTrace.Var
bt5 = BothTrace.tracef5

def agentName():
   return "TwinCvxClient"

class TwinCvxClient( ControllerClient ):
   def __init__( self, *args, **kwargs ):
      ControllerClient.__init__( self, *args, **kwargs )
      self.mcsSwitchConfig = None
      self.mcsServiceReactor = None

   def doInfraMounts( self, mg ):
      bt5()
      self.config = mg.mount( "mcs/switch/controller/config",
                              "ControllerClient::Config", "r" )
      self.status = mg.mount( "mcs/switch/controller/status",
                              "ControllerClient::Status", "w" )
      self.controllerSysdbConfig = mg.mount( "mcs/switch/controller/publish/config",
                                             "ControllerClient::SysdbConfig", "w" )
      self.controllerSysdbStatus = mg.mount( "mcs/switch/controller/publish/status",
                                             "ControllerClient::SysdbStatus", "r" )
      # Since it's an MCS only agent, we'll hard-code the service config instead
      # of taking the DefaultConfigPlugin route. Of course, we need to have this
      # keep up with any updates to real CVX and Mcs versions.
      self.serviceConfig = Tac.newInstance( "Controller::ServiceConfigDir",
                                            "serviceConfig" )
      spc = ServicePluginContext.ServicePluginContext( self.serviceConfig )
      ServicePluginContext.addServiceInfo( spc, "CVX",
                                           ( ( 3, "SSL support" ), ), enabled=True )
      ServicePluginContext.addServiceInfo( spc, "NetworkTopology",
                                           ( ( 3, "Topology" ), ), enabled=True )
      ServicePluginContext.addServiceInfo( spc, "Mcs",
                                           ( ( 1, "MCS dual-cluster" ), ) )
      # We will never use CVX infra's test service
      self.overrideServiceConfig = Tac.newInstance(
                                    "Controller::OverrideServiceConfigDir",
                                    "debugService" )
      self.mcsSwitchConfig = mg.mount( "mcs/switch/config", "Mcs::Client::Config" )

   def doMountsComplete( self ):
      bt5()
      self.mcsServiceReactor = GenericReactor( self.mcsSwitchConfig,
                                               [ 'enabled' ],
                                               self.handleMcsService,
                                               callBackNow=True )
      ControllerClient.doMountsComplete( self )

   def handleMcsService( self, notifiee=None ):
      bt5( "Mcs enabled: ", bv( self.mcsSwitchConfig.enabled ) )
      self.serviceConfig.service[ "Mcs" ].enabled = self.mcsSwitchConfig.enabled

def main():
   # pylint: disable-next=consider-using-f-string
   qtfile = "{}{}.qt".format( agentName(), "-%d" if "QUICKTRACEDIR"
                              not in os.environ else "" )
   BothTrace.initialize( qtfile, "8,128,8,8,8,128,8,8,128,8", maxStringLen=80 )
   container = Agent.AgentContainer( [ TwinCvxClient ] )
   container.runAgents()

if __name__ == "__main__":
   main()
