<commit_before>/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package com.bwsw.cloudstack.vault.server.cloudstack.util

import java.net.NoRouteToHostException
import java.util.UUID

import br.com.autonomiccs.apacheCloudStack.client.{ApacheCloudStackClient, ApacheCloudStackRequest}
import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser
import br.com.autonomiccs.apacheCloudStack.exceptions.{ApacheCloudStackClientRequestRuntimeException, ApacheCloudStackClientRuntimeException}
import com.bwsw.cloudstack.vault.server.cloudstack.entities.{Command, Tag}
import com.bwsw.cloudstack.vault.server.cloudstack.util.exception.{CloudStackEntityDoesNotExistException, CloudStackFatalException}
import com.bwsw.cloudstack.vault.server.util.HttpStatus
import org.slf4j.LoggerFactory

import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}

/**
  * Class is responsible for tasks creation for interaction with CloudStack server with help of CloudStack library
  *
  * @param settings contains settings for interaction with CloudStack
  */
class CloudStackTaskCreator(settings: CloudStackTaskCreator.Settings) {
  private val logger = LoggerFactory.getLogger(this.getClass)
  //cloud stack client config
  protected val apacheCloudStackUser = new ApacheCloudStackUser(settings.secretKey, settings.apiKey)
  protected val apacheCloudStackClientList: List[ApacheCloudStackClient] = settings.urlList.map { x =>
    new ApacheCloudStackClient(x, apacheCloudStackUser)
  }.toList

  private var threadLocalClientList = new ThreadLocal[List[ApacheCloudStackClient]](){
    override protected def initialValue(): List[ApacheCloudStackClient] = {
      apacheCloudStackClientList
    }
  }
  val idParameter = "id"
  val nameParameter = "name"

  /**
    * Creates task to extract tags for specified entity
    *
    * @param resourceType type of tags to extract
    * @param resourceId id of resource whose tags are extracted
    *
    * @return task for extracting tags
    */
  def createGetTagTask(resourceType: Tag.Type, resourceId: UUID): () => String = {
    val tagRequest = new ApacheCloudStackRequest(Command.toString(Command.ListTags))
    tagRequest.addParameter("response", "json")
    tagRequest.addParameter("resourcetype", Tag.Type.toString(resourceType))
    tagRequest.addParameter("listAll", "true")
    tagRequest.addParameter("resourceid", resourceId)

    createRequest(tagRequest, s"get tag by resource: ($resourceId, $resourceType)")
  }

  /**
    * Creates task to extract entity with specified parameters
    *
    * @param parameterValue value of filter parameter to extract entity
    * @param parameterName filter parameter to extract entity
    *
    * @return task for extracting entity
    */
  def createGetEntityTask(parameterValue: String, parameterName: String, command: Command): () => String = {
    val request = new ApacheCloudStackRequest(Command.toString(command))
    request.addParameter("response", "json")
    request.addParameter("listAll", "true")
    request.addParameter(parameterName, parameterValue)

    createRequest(request, s"get entity by command: $command")
  }

  /**
    * Creates task to include tags into tags of specified entity
    *
    * @param resourceId id of resource to include tag
    * @param resourceType type of resources tags
    * @param tagList List of tags to include in resource tags
    *
    * @return task to include tags into entity tags
    */
  def createSetResourceTagsTask(resourceId: UUID, resourceType: Tag.Type, tagList: List[Tag]):() => Unit = {
    @tailrec
    def addTagsToRequest(index: Int, tagList: List[Tag], request: ApacheCloudStackRequest): ApacheCloudStackRequest = {
      tagList match {
        case tag :: tail =>
          request.addParameter(s"tags[$index].key", Tag.Key.toString(tag.key))
          request.addParameter(s"tags[$index].value", tag.value)
          addTagsToRequest(index + 1, tail, request)
        case _ => request
      }
    }

    val request = new ApacheCloudStackRequest(Command.toString(Command.CreateTags))
    request.addParameter("response", "json")
    request.addParameter("resourcetype", Tag.Type.toString(resourceType))
    request.addParameter("resourceids", resourceId)

    val requestWithTags = addTagsToRequest(0, tagList, request)

    createRequest(requestWithTags, s"set tags to resource: ($resourceId, $resourceType)")
  }

  /**
    * Processes request execution
    * does not swallow ApacheCloudStackClientRuntimeException if it wraps NoRouteToHostException
    * @throws CloudStackEntityDoesNotExistException if ApacheCloudStackClientRequestRuntimeException which have
    *                                               StatusCode == 431 was thrown
    *
    */
  protected def createRequest(request: ApacheCloudStackRequest, requestDescription: String)(): String = {
    logger.debug(s"Request was executed for action: $requestDescription")
    val clientList = threadLocalClientList.get()
    Try {
      clientList.head.executeRequest(request)
    } match {
      case Success(x) => x
      case Failure(e: ApacheCloudStackClientRuntimeException) if e.getCause.isInstanceOf[NoRouteToHostException] =>
        logger.warn(s"CloudStack server is unavailable")
        if (clientList.tail.isEmpty) {
          threadLocalClientList.set(apacheCloudStackClientList)
        } else {
          threadLocalClientList.set(clientList.tail)
        }
        throw e
      case Failure(e: ApacheCloudStackClientRequestRuntimeException)
        if e.getStatusCode == HttpStatus.CLOUD_STACK_ENTITY_DOES_NOT_EXIST =>
        throw new CloudStackEntityDoesNotExistException(e.toString)
      case Failure(e: Throwable) =>
        logger.error(s"Request execution thrown an exception: $e")
        throw new CloudStackFatalException(e.toString)
    }
  }
}

object CloudStackTaskCreator {
  case class Settings(urlList: Array[String], secretKey: String, apiKey: String)<commit_msg>CSVS-54 After merge fix<commit_after>/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package com.bwsw.cloudstack.vault.server.cloudstack.util

import java.net.NoRouteToHostException
import java.util.UUID

import br.com.autonomiccs.apacheCloudStack.client.{ApacheCloudStackClient, ApacheCloudStackRequest}
import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser
import br.com.autonomiccs.apacheCloudStack.exceptions.{ApacheCloudStackClientRequestRuntimeException, ApacheCloudStackClientRuntimeException}
import com.bwsw.cloudstack.vault.server.cloudstack.entities.{Command, Tag}
import com.bwsw.cloudstack.vault.server.cloudstack.util.exception.{CloudStackEntityDoesNotExistException, CloudStackFatalException}
import com.bwsw.cloudstack.vault.server.util.HttpStatus
import org.slf4j.LoggerFactory

import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}

/**
  * Class is responsible for tasks creation for interaction with CloudStack server with help of CloudStack library
  *
  * @param settings contains settings for interaction with CloudStack
  */
class CloudStackTaskCreator(settings: CloudStackTaskCreator.Settings) {
  private val logger = LoggerFactory.getLogger(this.getClass)
  //cloud stack client config
  protected val apacheCloudStackUser = new ApacheCloudStackUser(settings.secretKey, settings.apiKey)
  protected val apacheCloudStackClientList: List[ApacheCloudStackClient] = settings.urlList.map { x =>
    new ApacheCloudStackClient(x, apacheCloudStackUser)
  }.toList

  private var threadLocalClientList = new ThreadLocal[List[ApacheCloudStackClient]](){
    override protected def initialValue(): List[ApacheCloudStackClient] = {
      apacheCloudStackClientList
    }
  }
  val idParameter = "id"
  val nameParameter = "name"

  /**
    * Creates task to retrieve tags for specified entity
    *
    * @param resourceType type of tags to extract
    * @param resourceId id of resource whose tags are retrieved
    *
    * @return task to retrieve tags
    */
  def createGetTagsTask(resourceType: Tag.Type, resourceId: UUID): () => String = {
    val tagRequest = new ApacheCloudStackRequest(Command.toString(Command.ListTags))
    tagRequest.addParameter("response", "json")
    tagRequest.addParameter("resourcetype", Tag.Type.toString(resourceType))
    tagRequest.addParameter("listAll", "true")
    tagRequest.addParameter("resourceid", resourceId)

    createRequest(tagRequest, s"Retrieve tag by resource: ($resourceId, $resourceType)")
  }

  /**
    * Creates task to retrieve entity with specified parameters
    *
    * @param parameterValue value of filter parameter to retrieve entity
    * @param parameterName filter parameter to retrieve entity
    *
    * @return task to retrieve entity
    */
  def createGetEntityTask(parameterValue: String, parameterName: String, command: Command): () => String = {
    val request = new ApacheCloudStackRequest(Command.toString(command))
    request.addParameter("response", "json")
    request.addParameter("listAll", "true")
    request.addParameter(parameterName, parameterValue)

    createRequest(request, s"Retrieve entity by command: $command")
  }

  /**
    * Creates task to include tags into tags of specified entity
    *
    * @param resourceId id of resource to include tag
    * @param resourceType type of resources tags
    * @param tagList List of tags to include in resource tags
    *
    * @return task to include tags into entity tags
    */
  def createSetResourceTagsTask(resourceId: UUID, resourceType: Tag.Type, tagList: List[Tag]):() => Unit = {
    @tailrec
    def addTagsToRequest(index: Int, tagList: List[Tag], request: ApacheCloudStackRequest): ApacheCloudStackRequest = {
      tagList match {
        case tag :: tail =>
          request.addParameter(s"tags[$index].key", Tag.Key.toString(tag.key))
          request.addParameter(s"tags[$index].value", tag.value)
          addTagsToRequest(index + 1, tail, request)
        case _ => request
      }
    }

    val request = new ApacheCloudStackRequest(Command.toString(Command.CreateTags))
    request.addParameter("response", "json")
    request.addParameter("resourcetype", Tag.Type.toString(resourceType))
    request.addParameter("resourceids", resourceId)

    val requestWithTags = addTagsToRequest(0, tagList, request)

    createRequest(requestWithTags, s"Include tags into resource: ($resourceId, $resourceType)")
  }

  /**
    * Processes request execution
    * does not swallow ApacheCloudStackClientRuntimeException if it wraps NoRouteToHostException
    * @throws CloudStackEntityDoesNotExistException if ApacheCloudStackClientRequestRuntimeException with
    *                                               StatusCode equal to 431 was thrown
    *
    *
    */
  protected def createRequest(request: ApacheCloudStackRequest, requestDescription: String)(): String = {
    logger.debug(s"Request executed for action: $requestDescription")
    val clientList = threadLocalClientList.get()
    Try {
      clientList.head.executeRequest(request)
    } match {
      case Success(x) => x
      case Failure(e: ApacheCloudStackClientRuntimeException) if e.getCause.isInstanceOf[NoRouteToHostException] =>
        logger.warn("CloudStack server is unavailable")
        if (clientList.tail.isEmpty) {
          threadLocalClientList.set(apacheCloudStackClientList)
        } else {
          threadLocalClientList.set(clientList.tail)
        }
        throw e
      case Failure(e: ApacheCloudStackClientRequestRuntimeException)
        if e.getStatusCode == HttpStatus.CLOUD_STACK_ENTITY_DOES_NOT_EXIST =>
        logger.error(s"Request execution thrown an exception: $e")
        throw new CloudStackEntityDoesNotExistException(e.toString)
      case Failure(e: Throwable) =>
        logger.error(s"Request execution thrown an exception: $e")
        throw new CloudStackFatalException(e.toString)
    }
  }
}

object CloudStackTaskCreator {
  case class Settings(urlList: Array[String], secretKey: String, apiKey: String)
{'fraction_non_alphanumeric': 0.0855062773939529, 'fraction_numerical': 0.0016343510883292474, 'mean_word_length': 4.225931677018633, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155373', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3532, 'n_words': 1016}