<commit_before># rubocop:disable Style/AccessorMethodName
module ManageIQ::Providers::Lenovo
  class PhysicalInfraManager::RefreshParser < EmsRefresh::Parsers::Infra
    include ManageIQ::Providers::Lenovo::RefreshHelperMethods

    def initialize(ems, options = nil)
      ems_auth = ems.authentications.first

      @ems               = ems
      @connection        = ems.connect(:user => ems_auth.userid,
                                       :pass => <PASSWORD>,
                                       :host => ems.endpoints.first.hostname)
      @options           = options || {}
      @data              = {}
      @data_index        = {}
      @host_hash_by_name = {}
    end

    def ems_inv_to_hashes
      log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS : [#{@ems.name}] id: [#{@ems.id} ref: #{@ems.uid_ems}]"

      $log.info("#{log_header}...")

      get_physical_servers

      $log.info("#{log_header}...Complete")

      @data
    end

    def self.miq_template_type
      "ManageIQ::Providers::Lenovo::PhysicalInfraManager::Template"
    end

    private

    def get_physical_servers
      nodes = all_server_resources

      nodes = nodes.map do |node|
        XClarityClient::Node.new node
      end
      process_collection(nodes, :physical_servers) { |node| parse_physical_server(node) }
    end

    def get_hardwares(node)
      {:firmwares => get_firmwares(node.firmware)}
    end

    def get_firmwares(node)
      firmwares = node.map do |firmware|
        parse_firmware(firmware)
      end
      firmwares
    end

    def parse_firmware(firmware)
      {
        :name         => firmware["name"],
        :build        => firmware["build"],
        :version      => firmware["version"],
        :release_date => firmware["date"],
      }
    end

    def parse_physical_server(node)
      new_result = {
        :type                   => ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalServer.name,
        :name                   => node.name,
        :ems_ref                => node.uuid,
        :uid_ems                => @ems.uid_ems,
        :hostname               => node.hostname,
        :product_name           => node.productName,
        :manufacturer           => node.manufacturer,
        :machine_type           => node.machineType,
        :model                  => node.model,
        :serial_number          => node.serialNumber,
        :field_replaceable_unit => node.FRU,
        :hardware               => {
          :networks  => [
            :mac_addresses => node.macAddress.split(",").flatten,
            :ipaddresses   => node.ipv4Addresses.split.flatten,
            :ipv6address   => node.ipv6Addresses.split.flatten
          ],
          :firmwares => [] # Filled in later conditionally on what's available
        }
      }
      new_result[:hardware] = get_hardwares(node)
      return node.uuid, new_result
    end

    def all_server_resources
      return @all_server_resources if @all_server_resources

      cabinets = @connection.discover_cabinet(:status => "includestandalone")
<commit_msg>Assign physical to virtual relationship<commit_after># rubocop:disable Style/AccessorMethodName
module ManageIQ::Providers::Lenovo
  class PhysicalInfraManager::RefreshParser < EmsRefresh::Parsers::Infra
    include ManageIQ::Providers::Lenovo::RefreshHelperMethods

    def initialize(ems, options = nil)
      ems_auth = ems.authentications.first

      @ems               = ems
      @connection        = ems.connect(:user => ems_auth.userid,
                                       :pass => <PASSWORD>,
                                       :host => ems.endpoints.first.hostname)
      @options           = options || {}
      @data              = {}
      @data_index        = {}
      @host_hash_by_name = {}
    end

    def ems_inv_to_hashes
      log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS : [#{@ems.name}] id: [#{@ems.id} ref: #{@ems.uid_ems}]"

      $log.info("#{log_header}...")

      get_physical_servers

      $log.info("#{log_header}...Complete")

      @data
    end

    def self.miq_template_type
      "ManageIQ::Providers::Lenovo::PhysicalInfraManager::Template"
    end

    private

    def get_physical_servers
      nodes = all_server_resources

      nodes = nodes.map do |node|
        XClarityClient::Node.new node
      end
      process_collection(nodes, :physical_servers) { |node| parse_physical_server(node) }
    end

    def get_hardwares(node)
      {:firmwares => get_firmwares(node.firmware)}
    end

    def get_firmwares(node)
      firmwares = node.map do |firmware|
        parse_firmware(firmware)
      end
      firmwares
    end

    def parse_firmware(firmware)
      {
        :name         => firmware["name"],
        :build        => firmware["build"],
        :version      => firmware["version"],
        :release_date => firmware["date"],
      }
    end

    def parse_physical_server(node)
      new_result = {
        :type                   => ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalServer.name,
        :name                   => node.name,
        :ems_ref                => node.uuid,
        :uid_ems                => @ems.uid_ems,
        :hostname               => node.hostname,
        :product_name           => node.productName,
        :manufacturer           => node.manufacturer,
        :machine_type           => node.machineType,
        :model                  => node.model,
        :serial_number          => node.serialNumber,
        :field_replaceable_unit => node.FRU,
        :hardware               => {
          :networks  => [
            :mac_addresses => node.macAddress.split(",").flatten,
            :ipaddresses   => node.ipv4Addresses.split.flatten,
            :ipv6address   => node.ipv6Addresses.split.flatten
          ],
          :firmwares => [] # Filled in later conditionally on what's available
        }
      }
      new_result[:hardware] = get_hardwares(node)
      save_host_relationship(node)
      return node.uuid, new_result
    end

    def save_host_relationship(node)
      # Assign a physicalserver and host if server already exists and 
      # some host match with physical Server's serial number
      server = PhysicalServer.where(:uuid => node.uuid, :ems_id => @ems.id)
      if server
        host = Host.where(:service_tag => node.serialNumber)

        if host
          server.host = host
          server.save!
        end
      end
    end
    def all_server_resources
      return @all_server_resources if @all_server_resources

      cabinets = @connection.discover_cabinet(:status => "includestandalone")

{'fraction_non_alphanumeric': 0.10880597014925374, 'fraction_numerical': 0.0008955223880597015, 'mean_word_length': 1.9742565468264537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376037', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1932, 'n_words': 347}