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

import Cell
import EntityManager
import Fru.Agent
import Tac

import os
import sys

fdlFilePath = '/etc/fdl'
intfs = []

def genIntfModular():
   global intfs
   maxLineCards = 10
   maxPorts = 48

   # Line cards start from the third slot
   intfs = []
   for lineCard in range( 3, maxLineCards+1 ):
      for port in range( 1, maxPorts+1 ):
         intfName = "Ethernet%d/%d" % ( lineCard, port )
         intfs.append( intfName )

def genIntfFixed():
   global intfs
   sysname = "zerotouch-gen-startup"
   Tac.sysnameIs( sysname )
   em = EntityManager.Local( sysname )
   entmibDir = em.root()[ 'hardware' ][ 'entmib' ]
   ethPhyIntfStatusDir = em.root()[ 'interface' ][ 'status' ][ 'eth' ][ 'phy' ]
   fru = Fru.Agent.Fru( em, fdlFile=fdlFilePath )

   # Nothing is delayed in the Fru agent (at least on a fixed system), 
   # so this is sufficient to ensure that the fdl is exec'd and all 
   # fru drivers have been run
   Tac.runActivities( 0 )

   if entmibDir.root is None:
      print >> sys.stderr, 'entmibDir root not set'
      os._exit( 1 )

   # Get interfaces from sysdb and sort them by portId
   maxPortIdSeen = 0
   for epis in ethPhyIntfStatusDir.keys():
      portId = ethPhyIntfStatusDir[ epis ].portId
      if portId:
         intfs.append( epis )
         if portId > maxPortIdSeen:
            maxPortIdSeen = portId
   intfs = sorted( intfs )

   if maxPortIdSeen == 0:
      print >> sys.stderr, 'no interfaces found'
      os._exit( 1 )

def genIntfHw():
   if Cell.cellType() == "fixed":
      genIntfFixed()
   else:
      genIntfModular()

def genIntfSim():
   for i in range( 1, 129 ):
      intfName = 'Ethernet%d' % i
      intfs.append( intfName )

defaultModelIsSwitched = False

if defaultModelIsSwitched == True:
   if os.path.exists( fdlFilePath ):
      genIntfHw()
   else:
      genIntfSim()

print """! ZeroTouch initial config file
!
zerotouch enable
!
logging console informational
!
logging level ALL errors
logging level ZTP informational
!
! Temporary workaround for bug477516, remove when bug477516 is fixed
logging policy match match-list ztpFilter discard
!
match-list input string ztpFilter
   10 match regex ETH-4
!
"""
for intf in intfs:
   print "interface %s" % ( intf )
   print """
   no switchport
   ipv6 enable
!"""
print """
system control-plane
   no service-policy input copp-system-policy
!
banner login
No startup-config was found.

The device is in Zero Touch Provisioning mode and is attempting to 
download the startup-config from a remote system. The device will not  
be fully functional until either a valid startup-config is downloaded 
from a remote system or Zero Touch Provisioning is cancelled.

To cancel Zero Touch Provisioning, login as admin and type 
'zerotouch cancel' at the CLI. Alternatively, to disable Zero Touch  
Provisioning permanently, type 'zerotouch disable' at the CLI.  
Note: The device will reload when these commands are issued. 

EOF
!
!
end"""

