#
# 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
                               |
                               netflow
                                      |
                                      netflow
                                             |
                                             flow-records
                                                         |
                                                         flow-record
                                                                    |
                                                                    collect
                                                                           |
                                                                           timestamp
                                                                                    |
                                                                                    timestamp-options
                                                                                                     
Schema Representation:

/services/day1services/day1service/netflow/netflow/flow-records/flow-record/collect/timestamp/timestamp-options
"""

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
import service_customization


class TimestampOptions(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, 'timestamp_options')

        #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['name'] = config.get_field_value('name')
        inputdict['timestamp_option'] = config.get_field_value('timestamp_option')
        inputdict['timestamps_time'] = config.get_field_value('timestamps_time')
        # END OF FETCHING THE LEAF PARAMETERS

        dev_pro_name = inputdict['name']
        if util.isNotEmpty(inputdict['timestamp_option']):
		if util.isNotEmpty(inputdict['timestamps_time']):
        		dev_pro_name = "collect_timestamp_%s_%s" % (inputdict['timestamp_option'].replace('-', '_'), inputdict['timestamps_time'])
		else:
        		dev_pro_name = "collect_timestamp_%s" % (inputdict['timestamp_option'].replace('-', '_'))

        _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_netflow_netflow_flow_records_flow_record_name'] = sdata.getRcPath().split('/')[-4].split('=')[1]
        inputkeydict['day1services_day1service_device_ip'] = sdata.getRcPath().split('/')[-8].split('=')[1]
        # END OF FETCHING THE PARENT KEY LEAF PARAMETERS

        #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)

        #Start of Device binding with python bindings
        netflow_object = devices.device.netflow.netflow()
        netflow_flow_records_object = devices.device.netflow.flow_records.flow_records()
        _Gen_obj = getLocalObject(sdata, 'flow-record')
        inputdict['day1services_day1service_netflow_netflow_flow_records_flow_record_name'] = _Gen_obj.flow_record.name
        if util.isNotEmpty(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name')):
          netflow_flow_records_flow_record_object = devices.device.netflow.flow_records.flow_record.flow_record()
          netflow_flow_records_flow_record_collect_object = devices.device.netflow.flow_records.flow_record.collect.collect()
          netflow_flow_records_flow_record_collect_timestamp_object = devices.device.netflow.flow_records.flow_record.collect.timestamp.timestamp()
          if util.isNotEmpty(inputdict.get('name')):
            netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object = devices.device.netflow.flow_records.flow_record.collect.timestamp.timestamp_options.timestamp_options()
            netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object.name = dev_pro_name
            netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object.timestamp_option = inputdict.get('timestamp_option')
            netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object.timestamps_time = inputdict.get('timestamps_time')


        #End of Device binding
        devbindobjs['netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object'] = netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object
        #Use the custom method to process/create payload
        service_customization.ServiceDataCustomization.process_service_device_bindings(smodelctx, sdata, dev, id=id, device=dev, inputdict=inputdict, inputkeydict=inputkeydict, parentobj=parentobj, config=config, devbindobjs=devbindobjs, hopaque=opaque_args)
        for dev_iterator in dev:
            if not yang.Sdk.dataExists(dev_iterator.url+'/l3features:netflow/flow-records/flow-record=%s/collect'%(util.make_interfacename(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name')))):
                yang.Sdk.createData(dev_iterator.url+'/l3features:netflow/flow-records/flow-record=%s'%(util.make_interfacename(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name'))),'<collect/>', sdata.getSession(), False)

            if not yang.Sdk.dataExists(dev_iterator.url+'/l3features:netflow/flow-records/flow-record=%s/collect/timestamp'%(util.make_interfacename(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name')))):
                yang.Sdk.createData(dev_iterator.url+'/l3features:netflow/flow-records/flow-record=%s/collect'%(util.make_interfacename(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name'))),'<timestamp/>', sdata.getSession(), False)


        netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object_payload = netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object.getxml(filter=True)
        log('netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object_payload: %s' % (netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object_payload))
        for dev_iterator in dev:
          yang.Sdk.createData(dev_iterator.url+'/netflow/flow-records/flow-record=%s/collect/timestamp'%(util.make_interfacename(inputdict.get('day1services_day1service_netflow_netflow_flow_records_flow_record_name'))),netflow_flow_records_flow_record_collect_timestamp_timestamp_options_object_payload, sdata.getSession(), True)

    def update(self, id, sdata):
        #Fetch Local Config Object
        config = getCurrentObjectConfig(id, sdata, 'timestamp_options')
        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['name'] = config.get_field_value('name')
        inputdict['timestamp_option'] = config.get_field_value('timestamp_option')
        inputdict['timestamps_time'] = config.get_field_value('timestamps_time')
        # 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, 'timestamp_options')
        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['name'] = config.get_field_value('name')
        inputdict['timestamp_option'] = config.get_field_value('timestamp_option')
        inputdict['timestamps_time'] = config.get_field_value('timestamps_time')
        # 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(TimestampOptions._instance == None):
            TimestampOptions._instance = TimestampOptions()
        return TimestampOptions._instance

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