#
# This computer program is the confidential information and proprietary trade
# secret of Anuta Networks, Inc. Possessions and use of this program must
# conform strictly to the license agreement between the user and
# Anuta Networks, Inc., and receipt or possession does not convey any rights
# to divulge, reproduce, or allow others to use this program without specific
# written authorization of Anuta Networks, Inc.
#
# Copyright (c) 2016-2017 Anuta Networks, Inc. All Rights Reserved.
#

#
#DO NOT EDIT THIS FILE ITS AUTOGENERATED ONE
#ALL THE CUSTOMIZATIONS REGARDING DATAPROCESSING SHOULD BE WRITTEN INTO service_customization.py FILE
#
"""
Tree Structure of Handled XPATH:

services
        |
        day1services
                    |
                    day1service
                               |
                               eem-scripts
                                          |
                                          eem-script
                                                    |
                                                    eem-zscaler
                                                                
Schema Representation:

/services/day1services/day1service/eem-scripts/eem-script/eem-zscaler
"""

from servicemodel import util
from servicemodel import yang
from servicemodel import devicemgr

from tclday0config.tclday0config_lib import getLocalObject
from tclday0config.tclday0config_lib import getDeviceObject
from tclday0config.tclday0config_lib import getCurrentObjectConfig
from tclday0config.tclday0config_lib import ServiceModelContext
from tclday0config.tclday0config_lib import getParentObject
from tclday0config.tclday0config_lib import log

#from servicemodel.controller import devices
from servicemodel.controller.devices.device import eem_applets
import service_customization


class EemZscaler(yang.AbstractYangServiceHandler):
    _instance = None

    def __init__(self):
        self.delete_pre_processor = service_customization.DeletePreProcessor()
        self.create_pre_processor = service_customization.CreatePreProcessor()
        self.opaque_args = {}

    def create(self, id, sdata):
        sdata.getSession().addYangSessionPreReserveProcessor(self.create_pre_processor)

        #Fetch Local Config Object
        config = getCurrentObjectConfig(id, sdata, 'eem_zscaler')

        #Fetch Service Model Context Object
        smodelctx = None

        #Fetch Parent Object
        parentobj = None

        dev = []
        inputkeydict = {}
        devbindobjs={}
        inputdict = {}
        opaque_args = self.opaque_args

        # START OF FETCHING THE LEAF PARAMETERS
        inputdict['enable_eem_zscaler'] = config.get_field_value('enable_eem_zscaler')
        inputdict['zscaler_track_list'] = config.get_field_value('zscaler_track_list')
        # END OF FETCHING THE LEAF PARAMETERS

        _Gen_obj = getLocalObject(sdata, 'day1service')
        device_mgmt_ip_address = _Gen_obj.day1service.device_ip

        #Fetch Device Object
        dev = getDeviceObject(device_mgmt_ip_address, sdata)

        # START OF FETCHING THE PARENT KEY LEAF PARAMETERS
        inputkeydict['day1services_day1service_device_ip'] = sdata.getRcPath().split('/')[-4].split('=')[1]
        # END OF FETCHING THE PARENT KEY LEAF PARAMETERS

        eem_applet_payload = '''
                                    <eem-applets xmlns="http://anutanetworks.com/l3features">
                    <event-manager-applet>
                        <applet-name>ZSCALER</applet-name>
                        <actions>
                            <action>
                                <snmp-trap-type>strdata</snmp-trap-type>
                                <label>1.0</label>
                                <snmp-trap-data>ZScaler Trap : No internet connectivity</snmp-trap-data>
                                <action-statement>snmp-trap</action-statement>
                            </action>
                            <action>
                                <syslog-msg>EEM Script : ZScaler Trap : No internet connectivity. Trap sent!</syslog-msg>
                                <label>2.0</label>
                                <syslog>msg</syslog>
                                <action-statement>syslog</action-statement>
                            </action>
                            <action>
                                <label>9.0</label>
                                <action-statement>exit</action-statement>
                            </action>
                        </actions>
                        <events>
                            <none>true</none>
                        </events>
                    </event-manager-applet>
                    <event-manager-applet>
                        <applet-name>ZSCALER-DET</applet-name>
                        <actions>
                            <action>
                                <compare>eq</compare>
                                <second-operand>down</second-operand>
                                <first-operand>$_track_state</first-operand>
                                <label>2.1</label>
                                <action-statement>if</action-statement>
                            </action>
                            <action>
                                <cli-string>enable</cli-string>
                                <cli-type>command</cli-type>
                                <label>3.1</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>conf t</cli-string>
                                <cli-type>command</cli-type>
                                <label>3.3</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>event manager applet ZSCALER authorization bypass</cli-string>
                                <cli-type>command</cli-type>
                                <label>3.5</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>event timer countdown time 600</cli-string>
                                <cli-type>command</cli-type>
                                <label>3.7</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>end</cli-string>
                                <cli-type>command</cli-type>
                                <label>3.9</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <label>5.1</label>
                                <action-statement>else</action-statement>
                            </action>
                            <action>
                                <cli-string>enable</cli-string>
                                <cli-type>command</cli-type>
                                <label>6.1</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>config t</cli-string>
                                <cli-type>command</cli-type>
                                <label>6.3</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>event manager applet ZSCALER authorization bypass</cli-string>
                                <cli-type>command</cli-type>
                                <label>6.5</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>event none</cli-string>
                                <cli-type>command</cli-type>
                                <label>6.7</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <cli-string>end</cli-string>
                                <cli-type>command</cli-type>
                                <label>6.9</label>
                                <action-statement>cli</action-statement>
                            </action>
                            <action>
                                <label>8.1</label>
                                <action-statement>end</action-statement>
                            </action>
                            <action>
                                <label>9.1</label>
                                <exit-result>0</exit-result>
                                <action-statement>exit</action-statement>
                            </action>
                        </actions>
                        <events>
                            <track>
                                <track-number>''' + inputdict['zscaler_track_list'] + ''' </track-number>
                            </track>
                        </events>
                    </event-manager-applet>
                </eem-applets>

                '''

        #Use the custom methods to process the data
        service_customization.ServiceDataCustomization.process_service_create_data(smodelctx, sdata, dev, id=id, device=dev, parentobj=parentobj, inputdict=inputdict, inputkeydict=inputkeydict, config=config, hopaque=opaque_args)

        for dev_iterator in dev:
          if not yang.Sdk.dataExists(dev_iterator.url + '/l3features:eem-applets'):
              yang.Sdk.createData(dev_iterator.url,'<eem-applets/>', sdata.getSession(), False)

        if  inputdict['enable_eem_zscaler'] == "true":
            for dev_iterator in dev:
                yang.Sdk.patchData(dev_iterator.url+'/l3features:eem-applets', eem_applet_payload, sdata, True)

    def update(self, id, sdata):
        #Fetch Local Config Object
        config = getCurrentObjectConfig(id, sdata, 'eem_zscaler')
        opaque_args = self.opaque_args

        #Fetch Service Model Context Object
        smodelctx = None

        #Fetch Parent Object
        parentobj = None

        dev = []
        inputkeydict = {}
        devbindobjs={}
        inputdict = {}
        opaque_args = self.opaque_args

        # START OF FETCHING THE LEAF PARAMETERS
        inputdict['enable_eem_zscaler'] = config.get_field_value('enable_eem_zscaler')
        inputdict['zscaler_track_list'] = config.get_field_value('zscaler_track_list')
        # END OF FETCHING THE LEAF PARAMETERS

        _Gen_obj = getLocalObject(sdata, 'day1service')
        device_mgmt_ip_address = _Gen_obj.day1service.device_ip

        #Fetch Device Object
        dev = getDeviceObject(device_mgmt_ip_address, sdata)

        #Use the custom method to process the data
        service_customization.ServiceDataCustomization.process_service_update_data(smodelctx, sdata, id=id, dev=dev, parentobj=parentobj, config=config, hopaque=opaque_args, inputdict=inputdict)

    def delete(self, id, sdata):
        sdata.getSession().addYangSessionPreReserveProcessor(self.delete_pre_processor)

        #Fetch Local Config Object
        config = getCurrentObjectConfig(id, sdata, 'eem_zscaler')
        opaque_args = self.opaque_args

        #Fetch Service Model Context Object
        smodelctx = None

        #Fetch Parent Object
        parentobj = None

        dev = []
        inputkeydict = {}
        devbindobjs={}
        inputdict = {}
        opaque_args = self.opaque_args

        # START OF FETCHING THE LEAF PARAMETERS
        inputdict['enable_eem_zscaler'] = config.get_field_value('enable_eem_zscaler')
        inputdict['zscaler_track_list'] = config.get_field_value('zscaler_track_list')
        # END OF FETCHING THE LEAF PARAMETERS

        _Gen_obj = getLocalObject(sdata, 'day1service')
        device_mgmt_ip_address = _Gen_obj.day1service.device_ip

        #Fetch Device Object
        dev = getDeviceObject(device_mgmt_ip_address, sdata)

        #Use the custom method to process the data
        service_customization.ServiceDataCustomization.process_service_delete_data(smodelctx, sdata, id=id, dev=dev, parentobj=parentobj, config=config, hopaque=opaque_args, inputdict=inputdict)

    @staticmethod
    def getInstance():
        if(EemZscaler._instance == None):
            EemZscaler._instance = EemZscaler()
        return EemZscaler._instance

    def rollbackCreate(self, id, sdata):
        log('rollback: id = %s, sdata = %s' % (id, sdata))
        self.delete(id,sdata)

if __name__ == 'eem_zscaler':
  from servicemodel.yang import YangServiceData
  sdata = YangServiceData()
  instance = EemZscaler().getInstance()
  instance.create(None, sdata)
  instance.delete(None, sdata)
  instance.update(None, sdata)
