/**
   * Update the Storage Provider. This is useful when we move arrays to some other provider.
   *
   * @param id the URN of a ViPR Storage Provider
   * @brief Update Storage provider
   * @return Updated Storage Provider information.
   */
  @PUT
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Path("/{id}")
  @CheckPermission(roles = {Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN})
  public StorageProviderRestRep updateStorageProvider(
      @PathParam("id") URI id, StorageProviderUpdateParam param) {
    StorageProvider storageProvider = _dbClient.queryObject(StorageProvider.class, id);
    if (null == storageProvider || storageProvider.getInactive()) {
      throw APIException.notFound.unableToFindEntityInURL(id);
    } else {
      /* Usecase is not to remove the provider instead we can update the old storage provider with
      new provider details. */
      if (param.getName() != null
          && !param.getName().equals("")
          && !param.getName().equalsIgnoreCase(storageProvider.getLabel())) {
        checkForDuplicateName(param.getName(), StorageProvider.class);
        storageProvider.setLabel(param.getName());
      }

      // if the ip or port passed are different from the existing one
      // check to ensure a provider does not exist with the new ip + port combo
      String existingIPAddress = storageProvider.getIPAddress();
      Integer existingPortNumber = storageProvider.getPortNumber();
      if ((param.getIpAddress() != null && !param.getIpAddress().equals(existingIPAddress))
          || (param.getPortNumber() != null && !param.getPortNumber().equals(existingPortNumber))) {
        String ipAddress =
            (param.getIpAddress() != null) ? param.getIpAddress() : existingIPAddress;
        Integer portNumber =
            (param.getPortNumber() != null) ? param.getPortNumber() : existingPortNumber;
        ArgValidator.checkFieldRange(portNumber, 1, 65535, "port_number");

        String providerKey = ipAddress + "-" + portNumber;
        List<StorageProvider> providers =
            CustomQueryUtility.getActiveStorageProvidersByProviderId(_dbClient, providerKey);
        if (providers != null && !providers.isEmpty()) {
          throw APIException.badRequests.invalidParameterStorageProviderAlreadyRegistered(
              providerKey);
        }

        // User can update IP address for an existing Provider object
        // if and only if the connection with old IP is not alive.
        if (!existingIPAddress.equals(param.getIpAddress())
            && isOldConnectionAlive(
                existingIPAddress, existingPortNumber, storageProvider.getInterfaceType())
            && (storageProvider.getStorageSystems() != null
                && !storageProvider.getStorageSystems().isEmpty())) {
          throw APIException.badRequests.cannotUpdateProviderIP(
              existingIPAddress + "-" + existingPortNumber);
        }

        storageProvider.setIPAddress(ipAddress);
        storageProvider.setPortNumber(portNumber);
      }
      if (param.getUserName() != null && StringUtils.isNotBlank(param.getUserName())) {
        storageProvider.setUserName(param.getUserName());
      }
      if (param.getPassword() != null && StringUtils.isNotBlank(param.getPassword())) {
        storageProvider.setPassword(param.getPassword());
      }
      if (param.getUseSSL() != null) {
        storageProvider.setUseSSL(param.getUseSSL());
      }

      if (param.getInterfaceType() != null) {
        ArgValidator.checkFieldValueFromEnum(
            param.getInterfaceType(),
            "interface_type",
            EnumSet.of(
                StorageProvider.InterfaceType.hicommand,
                StorageProvider.InterfaceType.smis,
                StorageProvider.InterfaceType.scaleio,
                StorageProvider.InterfaceType.ibmxiv));
        storageProvider.setInterfaceType(param.getInterfaceType());
      }

      if (param.getSecondaryUsername() != null) {
        storageProvider.setSecondaryUsername(param.getSecondaryUsername());
      }
      if (param.getSecondaryPassword() != null) {
        storageProvider.setSecondaryPassword(param.getSecondaryPassword());
      }
      if (param.getElementManagerURL() != null) {
        storageProvider.setElementManagerURL(param.getElementManagerURL());
      }

      _dbClient.persistObject(storageProvider);
    }

    auditOp(
        OperationTypeEnum.UPDATE_STORAGEPROVIDER,
        true,
        null,
        storageProvider.getId().toString(),
        storageProvider.getLabel(),
        storageProvider.getIPAddress(),
        storageProvider.getPortNumber(),
        storageProvider.getUserName(),
        storageProvider.getInterfaceType());

    return map(storageProvider);
  }
  @POST
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @CheckPermission(roles = {Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN})
  public TaskResourceRep registerStorageProvider(StorageProviderCreateParam param)
      throws ControllerException {
    ArgValidator.checkFieldNotEmpty(param.getName(), "name");
    checkForDuplicateName(param.getName(), StorageProvider.class);

    ArgValidator.checkFieldNotEmpty(param.getIpAddress(), "ip_address");
    ArgValidator.checkFieldNotNull(param.getPortNumber(), "port_number");
    ArgValidator.checkFieldNotEmpty(param.getUserName(), "user_name");
    ArgValidator.checkFieldNotEmpty(param.getPassword(), "password");
    ArgValidator.checkFieldRange(param.getPortNumber(), 1, 65535, "port_number");
    ArgValidator.checkFieldValueFromEnum(
        param.getInterfaceType(), "interface_type", StorageProvider.InterfaceType.class);
    String providerKey = param.getIpAddress() + "-" + param.getPortNumber();
    List<StorageProvider> providers =
        CustomQueryUtility.getActiveStorageProvidersByProviderId(_dbClient, providerKey);
    if (providers != null && !providers.isEmpty()) {
      throw APIException.badRequests.invalidParameterStorageProviderAlreadyRegistered(providerKey);
    }

    // Set the SSL parameter
    Boolean useSSL = param.getUseSSL();
    if (useSSL == null) {
      useSSL = StorageProviderCreateParam.USE_SSL_DEFAULT;
    }

    StorageProvider provider = new StorageProvider();
    provider.setId(URIUtil.createId(StorageProvider.class));
    provider.setLabel(param.getName());
    provider.setIPAddress(param.getIpAddress());
    provider.setPortNumber(param.getPortNumber());
    provider.setUserName(param.getUserName());
    provider.setPassword(param.getPassword());
    provider.setUseSSL(useSSL);
    provider.setInterfaceType(param.getInterfaceType());
    provider.setRegistrationStatus(RegistrationStatus.REGISTERED.toString());
    provider.setConnectionStatus(ConnectionStatus.INITIALIZING.name());
    provider.setSecondaryUsername(param.getSecondaryUsername());
    provider.setSecondaryPassword(param.getSecondaryPassword());
    provider.setElementManagerURL(param.getElementManagerURL());
    if (param.getSioCLI() != null) {
      // TODO: Validate the input?
      provider.addKey(StorageProvider.GlobalKeys.SIO_CLI.name(), param.getSioCLI());
    }

    if (StorageProvider.InterfaceType.ibmxiv.name().equalsIgnoreCase(provider.getInterfaceType())) {
      provider.setManufacturer("IBM");
    }

    _dbClient.createObject(provider);

    auditOp(
        OperationTypeEnum.REGISTER_STORAGEPROVIDER,
        true,
        null,
        provider.getLabel(),
        provider.getId().toString(),
        provider.getIPAddress(),
        provider.getPortNumber(),
        provider.getUserName(),
        provider.getInterfaceType());

    ArrayList<AsyncTask> tasks = new ArrayList<AsyncTask>(1);
    String taskId = UUID.randomUUID().toString();
    tasks.add(new AsyncTask(StorageProvider.class, provider.getId(), taskId));

    BlockController controller = getController(BlockController.class, provider.getInterfaceType());
    log.debug("controller.getClass().getName() :{}", controller.getClass().getName());
    log.debug("controller.getClass().getSimpleName() :{}", controller.getClass().getSimpleName());
    /**
     * Creates MonitoringJob token for vnxblock/vmax, hds, cinder and IBM XIV device on zooKeeper
     * queue
     */
    // TODO : If all interface types have monitoring impl class added (scaleIO is missing),
    // this check can be removed.
    String interfaceType = provider.getInterfaceType();
    if (StorageProvider.InterfaceType.hicommand.name().equalsIgnoreCase(interfaceType)
        || StorageProvider.InterfaceType.smis.name().equalsIgnoreCase(interfaceType)
        || StorageProvider.InterfaceType.cinder.name().equalsIgnoreCase(interfaceType)
        || StorageProvider.InterfaceType.ibmxiv.name().equalsIgnoreCase(interfaceType)) {
      controller.startMonitoring(
          new AsyncTask(StorageProvider.class, provider.getId(), taskId),
          getSystemTypeByInterface(interfaceType));
    }

    DiscoveredObjectTaskScheduler scheduler =
        new DiscoveredObjectTaskScheduler(_dbClient, new ScanJobExec(controller));
    TaskList taskList = scheduler.scheduleAsyncTasks(tasks);
    return taskList.getTaskList().listIterator().next();
  }