<commit_before># @see SonCatalogue
class SonataCatalogue < Sinatra::Application
  require 'addressable/uri'

	before do
		# Gatekeepr authn. code will go here for future implementation
		# --> Gatekeeper authn. disabled
		#if request.path_info == '/gk_credentials'
		#	return
		#end
		if settings.environment == 'development'
			return
		end
		#authorized?
  end

  DEFAULT_OFFSET = "0"
  DEFAULT_LIMIT = "10"
  DEFAULT_MAX_LIMIT = "100"

	# @method get_root
	# @overload get '/catalogues/'
	# Get all available interfaces
	# -> Get all interfaces
	get '/' do
    	headers "Content-Type" => "text/plain; charset=utf8"
		halt 200, interfaces_list.to_yaml
	end

	############################################ NSD API METHODS ############################################

	# @method get_nss
	# @overload get '/catalogues/network-services/?'
	#	Returns a list of NSs
	# -> List many descriptors
  get '/network-services/?' do
    params['offset'] ||= DEFAULT_OFFSET
    params['limit'] ||= DEFAULT_LIMIT

    uri = Addressable::URI.new
    uri.query_values = params
    puts 'params', params
    puts 'query_values', uri.query_values
    logger.info "Catalogue: entered GET /network-services?#{uri.query}"

    # Transform 'string' params Hash into keys
    keyed_params = keyed_hash(params)
    puts 'keyed_params', keyed_params

    # Set headers
    case request.content_type
      when 'application/x-yaml'
        headers = { 'Accept' => 'application/x-yaml', 'Content-Type' => 'application/x-yaml' }
      else
        headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
    end
    headers[:params] = params unless params.empty?

    # Get rid of :offset and :limit
    [:offset, :limit].each { |k| keyed_params.delete(k) }
    puts 'keyed_params(1)', keyed_params

    # Check for special case (:version param == last)
    if keyed_params.key?(:version) && keyed_params[:version] == 'last'
      # Do query for last version -> get_nsd_ns_vendor_last_version

      keyed_params.delete(:version)
      puts 'keyed_params(2)', keyed_params

      nss = Ns.where((keyed_params)).sort({"version" => -1})#.limit(1).first()
      #puts 'nss: ', nss.to_json

      if nss && nss.size.to_i > 0
        logger.info "Catalogue: leaving GET /network-services?#{uri.query} with #{nss}"

        # Paginate results
        nss = nss.paginate(:offset => params[:offset], :limit => params[:limit])

        nss_list = []
        checked_list = []

        nss_name_vendor = Pair.new(nss.first.name, nss.first.vendor)
        p 'nss_name_vendor:', nss_name_vendor.one, nss_name_vendor.two

        checked_list.push(nss_name_vendor)
        nss_list.push(nss.first)

        nss.each do |nsd|
          if (nsd.name && nsd.vendor) != (nss_name_vendor.one && nss_name_vendor.two)
            nss_name_vendor = Pair.new(nsd.name, nsd.vendor)
            p 'nss_name_vendor(x):', nss_name_vendor.one, nss_name_vendor.two
            nss_list.push(nsd) unless checked_list.include?(nss_name_vendor)
          end
        end
        puts 'nss_list:', nss_list
      else
          logger.error "ERROR: 'No NSDs were found'"
          return 404
      end
      nss = nss_list

      else
        # Do the query
        nss = Ns.where(keyed_params)
        logger.info "Catalogue: NSDs=#{nss}"
        #puts nss.to_json

        if nss && nss.size.to_i > 0
          logger.info "Catalogue: leaving GET /network-services?#{uri.query} with #{nss}"

          # Paginate results
          nss = nss.paginate(:offset => params[:offset], :limit => params[:limit])
        end

      case request.content_type
        when 'application/json'
          response = nss.to_json
        when 'application/x-yaml'
          response = json_to_yaml(nss.to_json)
        else
          halt 415
      end
      halt 200, response

    else
      logger.info "Catalogue: leaving GET /network-services?#{uri.query} with 'No NSDs were found'"
      halt 400, 'No NSDs were found'
    end
  end

  # @method get_ns_sp_ns_id
  # @overload get '/catalogues/network-services/:id/?'
  #	GET one specific descriptor
  #	@param [String] sp_ns_id NS sp ID
  # Show a NS by internal ID (uuid)
  get '/network-services/:id/?' do
    unless params[:id].nil?
      logger.debug "Catalogue: GET /network-services/#{params[:id]}"

      begin
        ns = Ns.find(params[:id] )
      rescue Mongoid::Errors::DocumentNotFound => e
        logger.error e
        json_error 404, "The NSD ID #{params[:id]} does not exist" unless ns
      end
      logger.debug "Catalogue: leaving GET /network-services/#{params[:id]}\" with NSD #{ns}"

      case request.content_type
        when 'application/json'
          response = ns.to_json
        when 'application/x-yaml'
          response = json_to_yaml(ns.to_json)
        else
          halt 415
      end
      halt 200, response<commit_msg>Partial implementation for new API routes<commit_after># @see SonCatalogue
class SonataCatalogue < Sinatra::Application
  require 'addressable/uri'

	before do
		# Gatekeepr authn. code will go here for future implementation
		# --> Gatekeeper authn. disabled
		#if request.path_info == '/gk_credentials'
		#	return
		#end
		if settings.environment == 'development'
			return
		end
		#authorized?
  end

  DEFAULT_OFFSET = "0"
  DEFAULT_LIMIT = "10"
  DEFAULT_MAX_LIMIT = "100"

	# @method get_root
	# @overload get '/catalogues/'
	# Get all available interfaces
	# -> Get all interfaces
	get '/' do
    	headers "Content-Type" => "text/plain; charset=utf8"
		halt 200, interfaces_list.to_yaml
	end

	############################################ NSD API METHODS ############################################

	# @method get_nss
	# @overload get '/catalogues/network-services/?'
	#	Returns a list of NSs
	# -> List many descriptors
  get '/network-services/?' do
    params['offset'] ||= DEFAULT_OFFSET
    params['limit'] ||= DEFAULT_LIMIT

    uri = Addressable::URI.new
    uri.query_values = params
    puts 'params', params
    puts 'query_values', uri.query_values
    logger.info "Catalogue: entered GET /network-services?#{uri.query}"

    # Transform 'string' params Hash into keys
    keyed_params = keyed_hash(params)
    puts 'keyed_params', keyed_params

    # Set headers
    case request.content_type
      when 'application/x-yaml'
        headers = { 'Accept' => 'application/x-yaml', 'Content-Type' => 'application/x-yaml' }
      else
        headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
    end
    headers[:params] = params unless params.empty?

    # Get rid of :offset and :limit
    [:offset, :limit].each { |k| keyed_params.delete(k) }
    puts 'keyed_params(1)', keyed_params

    # Check for special case (:version param == last)
    if keyed_params.key?(:version) && keyed_params[:version] == 'last'
      # Do query for last version -> get_nsd_ns_vendor_last_version

      keyed_params.delete(:version)
      puts 'keyed_params(2)', keyed_params

      nss = Ns.where((keyed_params)).sort({"version" => -1})#.limit(1).first()
      logger.info "Catalogue: NSDs=#{nss}"
      #nss = nss.sort({"version" => -1})
      #puts 'nss: ', nss.to_json

      if nss && nss.size.to_i > 0
        logger.info "Catalogue: leaving GET /network-services?#{uri.query} with #{nss}"

        # Paginate results
        #nss = nss.paginate(:offset => params[:offset], :limit => params[:limit]).sort({"version" => -1})

        nss_list = []
        checked_list = []

        nss_name_vendor = Pair.new(nss.first.name, nss.first.vendor)
        #p 'nss_name_vendor:', [nss_name_vendor.one, nss_name_vendor.two]

        checked_list.push(nss_name_vendor)
        nss_list.push(nss.first)

        nss.each do |nsd|
          #p 'Comparison: ', [nsd.name, nsd.vendor].to_s + [nss_name_vendor.one, nss_name_vendor.two].to_s
          if (nsd.name != nss_name_vendor.one) || (nsd.vendor != nss_name_vendor.two)
            nss_name_vendor = Pair.new(nsd.name, nsd.vendor)
            #p 'nss_name_vendor(x):', [nss_name_vendor.one, nss_name_vendor.two]
            #checked_list.each do |pair|
            #  p [pair.one, nss_name_vendor.one], [pair.two, nss_name_vendor.two]
            #  p pair.one == nss_name_vendor.one && pair.two == nss_name_vendor.two
            end
            nss_list.push(nsd) unless
                checked_list.any? {|pair| pair.one == nss_name_vendor.one && pair.two == nss_name_vendor.two}
            checked_list.push(nss_name_vendor)
          end

          #puts 'nss_list:', nss_list.each {|ns| p ns.name, ns.vendor}
        else
            logger.error "ERROR: 'No NSDs were found'"
            return 404
        end
        #nss = nss_list.paginate(:page => params[:offset], :per_page =>params[:limit])
      nss = nss_list

    else
      # Do the query
      nss = Ns.where(keyed_params)
      logger.info "Catalogue: NSDs=#{nss}"
      #puts nss.to_json
      if nss && nss.size.to_i > 0
        logger.info "Catalogue: leaving GET /network-services?#{uri.query} with #{nss}"

        # Paginate results
        nss = nss.paginate(:offset => params[:offset], :limit => params[:limit])

      else
        logger.info "Catalogue: leaving GET /network-services?#{uri.query} with 'No NSDs were found'"
        halt 400, 'No NSDs were found'
      end
    end

      case request.content_type
        when 'application/json'
          response = nss.to_json
        when 'application/x-yaml'
          response = json_to_yaml(nss.to_json)
        else
          halt 415
        end
        halt 200, response

  end

  # @method get_ns_sp_ns_id
  # @overload get '/catalogues/network-services/:id/?'
  #	GET one specific descriptor
  #	@param [String] sp_ns_id NS sp ID
  # Show a NS by internal ID (uuid)
  get '/network-services/:id/?' do
    unless params[:id].nil?
      logger.debug "Catalogue: GET /network-services/#{params[:id]}"

      begin
        ns = Ns.find(params[:id] )
      rescue Mongoid::Errors::DocumentNotFound => e
        logger.error e
        json_error 404, "The NSD ID #{params[:id]} does not exist" unless ns
      end
      logger.debug "Catalogue: leaving GET /network-services/#{params[:id]}\" with NSD #{ns}"

      case request.content_type
        when 'application/json'
          response = ns.to_json
        when 'application/x-yaml'
          response = json_to_yaml(ns.to_json)
        else
          halt 415
      end
      halt 200, response
{'fraction_non_alphanumeric': 0.13879582655307743, 'fraction_numerical': 0.007274815736575093, 'mean_word_length': 2.6788732394366197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4008734', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3390, 'n_words': 803}