<commit_before>#
# The NXAPI provider for cisco_bgp_neighbor_af.
#
# August 2015 <NAME>
#
# Copyright (c) 2015 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 'cisco_node_utils' if Puppet.features.cisco_node_utils?
begin
  require 'puppet_x/cisco/autogen'
rescue LoadError # seen on master, not on agent
  # See longstanding Puppet issues #4248, #7316, #14073, #14149, etc. Ugh.
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..',
                                     'puppet_x', 'cisco', 'autogen.rb'))
end

Puppet::Type.type(:cisco_bgp_neighbor_af).provide(:nxapi) do
  confine :feature => :cisco_node_utils
  defaultfor :operatingsystem => :nexus

  mk_resource_methods

  # Property symbol array for method auto-generation.
  # NOTE: For maintainability please keep this list in alphabetical order.
  BGP_NBR_AF_NON_BOOL_PROPS = [
    :allowas_in_max,
    :advertise_map_exist,
    :advertise_map_non_exist,
    :default_originate_route_map,
    :filter_list_in,
    :filter_list_out,
    :max_prefix_limit,
    :max_prefix_interval,
    :max_prefix_threshold,
    :send_community,
    :soo,
    :unsuppress_map,
    :weight,
  ]
  BGP_NBR_AF_BOOL_PROPS = [
    :allowas_in,
    :as_override,
    :cap_add_paths_send,
    :cap_add_paths_send_disable,
    :cap_add_paths_receive,
    :cap_add_paths_receive_disable,
    :default_originate,
    :disable_peer_as_check,
    :max_prefix_warning,
    :next_hop_self,
    :next_hop_third_party,
    :route_reflector_client,
    :soft_reconfiguration_in,
    :soft_reconfiguration_in_always,
    :suppress_inactive,
  ]

  BGP_NBR_AF_ALL_PROPS = BGP_NBR_AF_NON_BOOL_PROPS + BGP_NBR_AF_BOOL_PROPS

  PuppetX::Cisco::AutoGen.mk_puppet_methods(:non_bool, self, '@af',
                                            BGP_NBR_AF_NON_BOOL_PROPS)
  PuppetX::Cisco::AutoGen.mk_puppet_methods(:bool, self, '@af',
                                            BGP_NBR_AF_BOOL_PROPS)

  def initialize(value = {})
    super(value)
    asn = @property_hash[:asn]
    vrf = @property_hash[:vrf]
    nbr = @property_hash[:neighbor]
    afi = @property_hash[:afi]
    safi = @property_hash[:safi]
    af = afi, safi

    @af = Cisco::RouterBgpNeighborAF.afs[asn][vrf][nbr][af] unless asn.nil?
    @property_flush = {}
  end

  def self.properties_get(asn, vrf, nbr, af, obj)
    current_state = {
      :name     => [asn, vrf, nbr, af.first, af.last].join(' '),
      :asn      => asn,
      :vrf      => vrf,
      :neighbor => nbr,
      :afi      => af.first,
      :safi     => af.last,
      :ensure   => :present,
    }
    # Call node_utils getter for every property
    BGP_NBR_AF_NON_BOOL_PROPS.each do |prop|
      current_state[prop] = obj.send(prop)
    end
    BGP_NBR_AF_BOOL_PROPS.each do |prop|
      val = obj.send(prop)
      current_state[prop] = val.nil? ? nil : val.to_s.to_sym
    end
    new(current_state)
  end

  def self.instances
    af_objs = []
    Cisco::RouterBgpNeighborAF.afs.each do |asn, vrfs|
      vrfs.each do |vrf, nbrs|
        nbrs.each do |nbr, afs|
          afs.each do |af, af_obj|
            af_objs << properties_get(asn, vrf, nbr, af, af_obj)
          end
        end
      end
    end
    af_objs
  end

  def self.prefetch(resources)
    afs = instances
    resources.keys.each do |name|
      provider = afs.find do |af|
        af.asn.to_s == resources[name][:asn].to_s &&
        af.vrf == resources[name][:vrf] &&
        af.neighbor == resources[name][:neighbor] &&
        af.afi.to_s == resources[name][:afi].to_s &&
        af.safi.to_s == resources[name][:safi].to_s
      end
      resources[name].provider = provider unless provider.nil?
    end
  end

  def exists?
    @property_hash[:ensure] == :present
  end

  def create
    @property_flush[:ensure] = :present
  end

  def destroy
    @property_flush[:ensure] = :absent
  end

  def properties_set(new_af = false)
    BGP_NBR_AF_ALL_PROPS.each do |prop|
      next unless @resource[prop]
      if new_af
        # Set @property_flush for the current object
        send("#{prop}=", @resource[prop])
      end
      next if @property_flush[prop].nil?
      # Call the AutoGen setters for the @af node_utils object.
      @af.send("#{prop}=", @property_flush[prop]) if
        @af.respond_to?("#{prop}=")
    end

    # Non-AutoGen custom setters follow
    allowas_in_set
    cap_add_paths_receive_set
    cap_add_paths_send_set
    default_originate_set
    max_prefix_set
    soft_reconfiguration_in_set
  end

  # Non-AutoGen custom setters
  # The following properties are setters have additional complexity and cannot
  # be handled by PuppetX::Cisco::AutoGen.mk_puppet_methods.
  def advertise_map_exist=(val)
    val = @af.default_advertise_map_exist if val.first == :default
    @property_flush[:advertise_map_exist] = val
  end

  def advertise_map_non_exist=(val)
    val = @af.default_advertise_map_non_exist if val.first == :default
    @property_flush[:advertise_map_non_exist] = val
  end

  def allowas_in_set
    return unless
      @property_flush.key?(:allowas_in) ||
      @property_flush.key?(:allowas_in_max)

    # max is optional
    max = @property_flush[:allowas_in_max]
    if @property_flush.key?(:allowas_in_max)<commit_msg>Minor fix for advertise_map_exist when default<commit_after>#
# The NXAPI provider for cisco_bgp_neighbor_af.
#
# August 2015 <NAME>
#
# Copyright (c) 2015 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 'cisco_node_utils' if Puppet.features.cisco_node_utils?
begin
  require 'puppet_x/cisco/autogen'
rescue LoadError # seen on master, not on agent
  # See longstanding Puppet issues #4248, #7316, #14073, #14149, etc. Ugh.
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..',
                                     'puppet_x', 'cisco', 'autogen.rb'))
end

Puppet::Type.type(:cisco_bgp_neighbor_af).provide(:nxapi) do
  confine :feature => :cisco_node_utils
  defaultfor :operatingsystem => :nexus

  mk_resource_methods

  # Property symbol array for method auto-generation.
  # NOTE: For maintainability please keep this list in alphabetical order.
  BGP_NBR_AF_NON_BOOL_PROPS = [
    :allowas_in_max,
    :advertise_map_exist,
    :advertise_map_non_exist,
    :default_originate_route_map,
    :filter_list_in,
    :filter_list_out,
    :max_prefix_limit,
    :max_prefix_interval,
    :max_prefix_threshold,
    :send_community,
    :soo,
    :unsuppress_map,
    :weight,
  ]
  BGP_NBR_AF_BOOL_PROPS = [
    :allowas_in,
    :as_override,
    :cap_add_paths_send,
    :cap_add_paths_send_disable,
    :cap_add_paths_receive,
    :cap_add_paths_receive_disable,
    :default_originate,
    :disable_peer_as_check,
    :max_prefix_warning,
    :next_hop_self,
    :next_hop_third_party,
    :route_reflector_client,
    :soft_reconfiguration_in,
    :soft_reconfiguration_in_always,
    :suppress_inactive,
  ]

  BGP_NBR_AF_ALL_PROPS = BGP_NBR_AF_NON_BOOL_PROPS + BGP_NBR_AF_BOOL_PROPS

  PuppetX::Cisco::AutoGen.mk_puppet_methods(:non_bool, self, '@af',
                                            BGP_NBR_AF_NON_BOOL_PROPS)
  PuppetX::Cisco::AutoGen.mk_puppet_methods(:bool, self, '@af',
                                            BGP_NBR_AF_BOOL_PROPS)

  def initialize(value = {})
    super(value)
    asn = @property_hash[:asn]
    vrf = @property_hash[:vrf]
    nbr = @property_hash[:neighbor]
    afi = @property_hash[:afi]
    safi = @property_hash[:safi]
    af = afi, safi

    @af = Cisco::RouterBgpNeighborAF.afs[asn][vrf][nbr][af] unless asn.nil?
    @property_flush = {}
  end

  def self.properties_get(asn, vrf, nbr, af, obj)
    current_state = {
      :name     => [asn, vrf, nbr, af.first, af.last].join(' '),
      :asn      => asn,
      :vrf      => vrf,
      :neighbor => nbr,
      :afi      => af.first,
      :safi     => af.last,
      :ensure   => :present,
    }
    # Call node_utils getter for every property
    BGP_NBR_AF_NON_BOOL_PROPS.each do |prop|
      current_state[prop] = obj.send(prop)
    end
    BGP_NBR_AF_BOOL_PROPS.each do |prop|
      val = obj.send(prop)
      current_state[prop] = val.nil? ? nil : val.to_s.to_sym
    end
    new(current_state)
  end

  def self.instances
    af_objs = []
    Cisco::RouterBgpNeighborAF.afs.each do |asn, vrfs|
      vrfs.each do |vrf, nbrs|
        nbrs.each do |nbr, afs|
          afs.each do |af, af_obj|
            af_objs << properties_get(asn, vrf, nbr, af, af_obj)
          end
        end
      end
    end
    af_objs
  end

  def self.prefetch(resources)
    afs = instances
    resources.keys.each do |name|
      provider = afs.find do |af|
        af.asn.to_s == resources[name][:asn].to_s &&
        af.vrf == resources[name][:vrf] &&
        af.neighbor == resources[name][:neighbor] &&
        af.afi.to_s == resources[name][:afi].to_s &&
        af.safi.to_s == resources[name][:safi].to_s
      end
      resources[name].provider = provider unless provider.nil?
    end
  end

  def exists?
    @property_hash[:ensure] == :present
  end

  def create
    @property_flush[:ensure] = :present
  end

  def destroy
    @property_flush[:ensure] = :absent
  end

  def properties_set(new_af = false)
    BGP_NBR_AF_ALL_PROPS.each do |prop|
      next unless @resource[prop]
      if new_af
        # Set @property_flush for the current object
        send("#{prop}=", @resource[prop])
      end
      next if @property_flush[prop].nil?
      # Call the AutoGen setters for the @af node_utils object.
      @af.send("#{prop}=", @property_flush[prop]) if
        @af.respond_to?("#{prop}=")
    end

    # Non-AutoGen custom setters follow
    allowas_in_set
    cap_add_paths_receive_set
    cap_add_paths_send_set
    default_originate_set
    max_prefix_set
    soft_reconfiguration_in_set
  end

  # Non-AutoGen custom getters/setters
  # The following properties have additional complexity and cannot
  # be handled by PuppetX::Cisco::AutoGen.mk_puppet_methods.
  def advertise_map_exist
    return [:default] if
      @resource[:advertise_map_exist] == [:default] &&
      @property_hash[:advertise_map_exist] == @af.default_advertise_map_exist
    @property_hash[:advertise_map_exist]
  end

  def advertise_map_exist=(val)
    val = @af.default_advertise_map_exist if val.first == :default
    @property_flush[:advertise_map_exist] = val
  end

  def advertise_map_non_exist
    return [:default] if
      @resource[:advertise_map_non_exist] == [:default] &&
      @property_hash[:advertise_map_non_exist] ==
      @af.default_advertise_map_non_exist
    @property_hash[:advertise_map_non_exist]
  end

  def advertise_map_non_exist=(val)
    val = @af.default_advertise_map_non_exist if val.first == :default
    @property_flush[:advertise_map_non_exist] = val
  end

  def allowas_in_set
    return unless
      @property_flush.key?(:allowas_in) ||
      @property_flush.key?(:allowas_in_max)

    # max is optional
    max = @property_flush[:allowas_in_max]
    if @property_flush.key?(:allowas_in_max)
{'fraction_non_alphanumeric': 0.10505771945851673, 'fraction_numerical': 0.004982974835977079, 'mean_word_length': 3.133882595262616, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3428640', 'n_tokens_mistral': 4358, 'n_tokens_neox': 4279, 'n_words': 894}