#!/usr/bin/env python
#
# Copyright (c) 2015, Arista Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#  - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#  - Neither the name of Arista Networks nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#-------------------Configuration------------------------
EAPI_USERNAME = 'ztpserver'
EAPI_PASSWORD = 'icanttellyou'
EAPI_ENABLE_PASSWORD = ''

# http/https
EAPI_METHOD = 'https'

# How often to poll for the interfaces (seconds)
EAPI_POLL_INTERVAL = 3
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -

import jsonrpclib
import os
import re
import syslog

def log(msg):
    msg = '%%ZTPSERVER-4-DAEMON: %s' % msg
    syslog.syslog( msg )

class EApiClient( object ):

    def __init__( self ):
        url = '%s://%s:%s@localhost/command-api' % \
              ( EAPI_METHOD, EAPI_USERNAME, EAPI_PASSWORD )
        self.client = jsonrpclib.Server( url  )

    def run_enable_cmds( self, cmds ):
        result = self.client.runCmds( 
            1, [ { 'cmd': 'enable', 
                   'input': EAPI_ENABLE_PASSWORD } ] +
            cmds )[ 1: ]

        return result

    def connected_interfaces( self ):
        # Using text mode in order to be able to match interface shortname
        # in interfaceErrorCounters
        cmd = 'show interfaces status connected'
        res = self.run_enable_cmds( [ cmd ] )[ 0 ]
        return [ x for x in res[ 'interfaceStatuses' ].keys()
                 if not re.match( '^Ma*', x ) ]


def start_dhcpd( eapi ):
    log( 'Configuring ZTPServer VM requirements' )

    os.system( 'sudo cp /mnt/flash/dhcpd.conf /etc/dhcp/' )
    log( 'dhcpd configured' )

    interfaces = eapi.connected_interfaces()
    if interfaces:
        log( 'restarting dhcpd...' )
        os.system( 'sudo service dhcpd restart' )

def monitor_interfaces( eapi ):
    interfaces = []
    while True:
        new_interfaces = eapi.connected_interfaces()
        if [x for x in new_interfaces 
            if x not in interfaces]:
            log( 'restating dhcpd...' )
            os.system( 'sudo service dhcpd restart' )
        interfaces = new_interfaces
        
def main():
    eapi = EApiClient()
    start_dhcpd( eapi )
    monitor_interfaces( eapi )

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        pass
