<commit_before># Mar 2016, <NAME>
#
# Copyright (c) 2016 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative 'node_util'

module Cisco
  # node_utils class for itd_device_group
  class ItdService < NodeUtil
    attr_reader :name

    def initialize(name, instantiate=true)
      fail TypeError unless name.is_a?(String)
      fail ArgumentError unless name.length > 0
      @name = name

      set_args_keys_default
      create if instantiate
    end

    def self.itds
      hash = {}
      list = []
      services = config_get('itd_service',
                            'all_itds')
      return hash if services.nil?

      services.each do |service|
        # The show cmd shows more than service,
        # we get other things like device-groups etc.
        # so filter it out to just get the service
        next if service.include?('device-group')
        next if service.include?('session')
        next if service.include?('statistics')
        list << service
      end

      list.each do |id|
        hash[id] = ItdService.new(id, false)
      end
      hash
    end

    ########################################################
    #                      PROPERTIES                      #
    ########################################################

    def create
      Feature.itd_enable
      config_set('itd_service', 'create', name: @name)
    end

    def destroy
      # the service MUST be shutdown before it can be destroyed
      config_set('itd_service', 'shutdown', name: @name, state: '')
      config_set('itd_service', 'destroy', name: @name)
    end

    # Helper method to delete @set_args hash keys
    def set_args_keys_default
      keys = { name: @name }
      @get_args = @set_args = keys
    end

    # rubocop:disable Style/AccessorMethodName
    def set_args_keys(hash={})
      set_args_keys_default
      @set_args = @get_args.merge!(hash) unless hash.empty?
    end

    # extract value of property from load-balance
    def extract_value(prop, prefix=nil)
      prefix = prop if prefix.nil?
      lb_match = lb_get

      # matching lb not found
      return nil if lb_match.nil? # no matching lb found

      # property not defined for matching lb
      return nil unless lb_match.names.include?(prop)

      # extract and return value that follows prefix + <space>
      regexp = Regexp.new("#{Regexp.escape(prefix)} (?<extracted>.*)")
      value_match = regexp.match(lb_match[prop])
      return nil if value_match.nil?
      value_match[:extracted]
    end

    # prepend property name prefix/keyword to value
    def attach_prefix(val, prop, prefix=nil)
      prefix = prop.to_s if prefix.nil?
      @set_args[prop] = val.to_s.empty? ? val : "#{prefix} #{val}"
    end

    def access_list
      config_get('itd_service', 'access_list', @get_args)
    end

    def access_list=(val)
      if val
        @set_args[:state] = ''
        @set_args[:al] = val
        config_set('itd_service', 'access_list', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:al] = access_list
        config_set('itd_service', 'access_list', @set_args) if
        access_list
      end
      set_args_keys_default
    end

    def default_access_list
      config_get_default('itd_service', 'access_list')
    end

    def device_group
      config_get('itd_service', 'device_group', @get_args)
    end

    def device_group=(val)
      if val
        @set_args[:state] = ''
        @set_args[:dg] = val
        config_set('itd_service', 'device_group', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:dg] = device_group
        config_set('itd_service', 'device_group', @set_args) if
        device_group
      end
      set_args_keys_default
    end

    def default_device_group
      config_get_default('itd_service', 'device_group')
    end

    def exclude_access_list
      config_get('itd_service', 'exclude_access_list', @get_args)
    end

    def exclude_access_list=(val)
      if val
        @set_args[:state] = ''
        @set_args[:al] = val
        config_set('itd_service', 'exclude_access_list', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:al] = exclude_access_list
        config_set('itd_service', 'exclude_access_list', @set_args) if
        exclude_access_list
      end
      set_args_keys_default
    end

    def default_exclude_access_list
      config_get_default('itd_service', 'exclude_access_list')
    end

    def fail_action
      config_get('itd_service', 'fail_action', @get_args)
    end

    def fail_action=(state)
      no_cmd = (state ? '' : 'no')
      @set_args[:state] = no_cmd
      config_set('itd_service', 'fail_action', @set_args)
      set_args_keys_default
    end

    def default_fail_action
      config_get_default('itd_service', 'fail_action')
    end

    # this is an array like:
    # [['ethernet 1/1', '1.1.1.1'], ['port-channel 100', '2.2.2.2'],
    # ['vlan 2', '3.3.3.3']]
    # show command output is like: Eth1/1, Po100, Vlan2
    # so translate back to the input format
    def ingress_interface
      list = config_get('itd_service', 'ingress_interface', @get_args)
      list.each do |intf, _next_hop|
        # intf[/Eth/] = 'ethernet ' if intf.include?('Eth')
        # intf[/Po/] = 'port-channel ' if intf.include?('Po')
        # intf[/Vlan/] = 'vlan ' if intf.include?('Vlan')
        intf.gsub!('Eth', 'ethernet ')
        intf.gsub!('Po', 'port-channel ')
        intf.gsub!('Vlan', 'vlan ')
      end
      list
    end

    def ingress_interface_cleanup
      cur_list = ingress_interface
      @set_args[:state] = 'no'
      @set_args[:next] = ''
      @set_args[:nhop] = ''
      # clean up the current list first
      cur_list.each do |intf, _next_hop|
        @set_args[:interface] = intf
        config_set('itd_service', 'ingress_interface', @set_args)
      end
    end
<commit_msg>Remove old comments<commit_after># Mar 2016, <NAME>
#
# Copyright (c) 2016 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative 'node_util'

module Cisco
  # node_utils class for itd_device_group
  class ItdService < NodeUtil
    attr_reader :name

    def initialize(name, instantiate=true)
      fail TypeError unless name.is_a?(String)
      fail ArgumentError unless name.length > 0
      @name = name

      set_args_keys_default
      create if instantiate
    end

    def self.itds
      hash = {}
      list = []
      services = config_get('itd_service',
                            'all_itds')
      return hash if services.nil?

      services.each do |service|
        # The show cmd shows more than service,
        # we get other things like device-groups etc.
        # so filter it out to just get the service
        next if service.include?('device-group')
        next if service.include?('session')
        next if service.include?('statistics')
        list << service
      end

      list.each do |id|
        hash[id] = ItdService.new(id, false)
      end
      hash
    end

    ########################################################
    #                      PROPERTIES                      #
    ########################################################

    def create
      Feature.itd_enable
      config_set('itd_service', 'create', name: @name)
    end

    def destroy
      # the service MUST be shutdown before it can be destroyed
      config_set('itd_service', 'shutdown', name: @name, state: '')
      config_set('itd_service', 'destroy', name: @name)
    end

    # Helper method to delete @set_args hash keys
    def set_args_keys_default
      keys = { name: @name }
      @get_args = @set_args = keys
    end

    # rubocop:disable Style/AccessorMethodName
    def set_args_keys(hash={})
      set_args_keys_default
      @set_args = @get_args.merge!(hash) unless hash.empty?
    end

    # extract value of property from load-balance
    def extract_value(prop, prefix=nil)
      prefix = prop if prefix.nil?
      lb_match = lb_get

      # matching lb not found
      return nil if lb_match.nil? # no matching lb found

      # property not defined for matching lb
      return nil unless lb_match.names.include?(prop)

      # extract and return value that follows prefix + <space>
      regexp = Regexp.new("#{Regexp.escape(prefix)} (?<extracted>.*)")
      value_match = regexp.match(lb_match[prop])
      return nil if value_match.nil?
      value_match[:extracted]
    end

    # prepend property name prefix/keyword to value
    def attach_prefix(val, prop, prefix=nil)
      prefix = prop.to_s if prefix.nil?
      @set_args[prop] = val.to_s.empty? ? val : "#{prefix} #{val}"
    end

    def access_list
      config_get('itd_service', 'access_list', @get_args)
    end

    def access_list=(val)
      if val
        @set_args[:state] = ''
        @set_args[:al] = val
        config_set('itd_service', 'access_list', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:al] = access_list
        config_set('itd_service', 'access_list', @set_args) if
        access_list
      end
      set_args_keys_default
    end

    def default_access_list
      config_get_default('itd_service', 'access_list')
    end

    def device_group
      config_get('itd_service', 'device_group', @get_args)
    end

    def device_group=(val)
      if val
        @set_args[:state] = ''
        @set_args[:dg] = val
        config_set('itd_service', 'device_group', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:dg] = device_group
        config_set('itd_service', 'device_group', @set_args) if
        device_group
      end
      set_args_keys_default
    end

    def default_device_group
      config_get_default('itd_service', 'device_group')
    end

    def exclude_access_list
      config_get('itd_service', 'exclude_access_list', @get_args)
    end

    def exclude_access_list=(val)
      if val
        @set_args[:state] = ''
        @set_args[:al] = val
        config_set('itd_service', 'exclude_access_list', @set_args)
      else
        @set_args[:state] = 'no'
        @set_args[:al] = exclude_access_list
        config_set('itd_service', 'exclude_access_list', @set_args) if
        exclude_access_list
      end
      set_args_keys_default
    end

    def default_exclude_access_list
      config_get_default('itd_service', 'exclude_access_list')
    end

    def fail_action
      config_get('itd_service', 'fail_action', @get_args)
    end

    def fail_action=(state)
      no_cmd = (state ? '' : 'no')
      @set_args[:state] = no_cmd
      config_set('itd_service', 'fail_action', @set_args)
      set_args_keys_default
    end

    def default_fail_action
      config_get_default('itd_service', 'fail_action')
    end

    # this is an array like:
    # [['ethernet 1/1', '1.1.1.1'], ['port-channel 100', '2.2.2.2'],
    # ['vlan 2', '3.3.3.3']]
    # show command output is like: Eth1/1, Po100, Vlan2
    # so translate back to the input format
    def ingress_interface
      list = config_get('itd_service', 'ingress_interface', @get_args)
      list.each do |intf, _next_hop|
        intf.gsub!('Eth', 'ethernet ')
        intf.gsub!('Po', 'port-channel ')
        intf.gsub!('Vlan', 'vlan ')
      end
      list
    end

    def ingress_interface_cleanup
      cur_list = ingress_interface
      @set_args[:state] = 'no'
      @set_args[:next] = ''
      @set_args[:nhop] = ''
      # clean up the current list first
      cur_list.each do |intf, _next_hop|
        @set_args[:interface] = intf
        config_set('itd_service', 'ingress_interface', @set_args)
      end
    end

{'fraction_non_alphanumeric': 0.11980729742536723, 'fraction_numerical': 0.005844258410993524, 'mean_word_length': 2.6556004618937643, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293131', 'n_tokens_mistral': 4066, 'n_tokens_neox': 3918, 'n_words': 1034}