/**
   * Creates a CLI script for adding socket-binding to AS7
   *
   * @param socketBinding object representing socket-binding
   * @return string containing created CLI script
   * @throws CliScriptException if required attributes are missing
   * @deprecated Generate this from the ModelNode.
   */
  private static String createSocketBindingScript(SocketBindingBean socketBinding)
      throws CliScriptException {
    String errMsg = " in socket-binding must be set.";
    Utils.throwIfBlank(socketBinding.getSocketPort(), errMsg, "Port");
    Utils.throwIfBlank(socketBinding.getSocketName(), errMsg, "Name");

    CliAddScriptBuilder builder = new CliAddScriptBuilder();
    StringBuilder resultScript =
        new StringBuilder("/socket-binding-group=standard-sockets/socket-binding=");

    resultScript.append(socketBinding.getSocketName()).append(":add(");
    resultScript.append("port=").append(socketBinding.getSocketPort());

    builder.addProperty("interface", socketBinding.getSocketInterface());

    resultScript.append(builder.asString()).append(")");

    return resultScript.toString();
  }
  /**
   * Creates CliCommandAction for adding a Socket-Binding
   *
   * @param socket Socket-Binding
   * @return created CliCommandAction for adding the Socket-Binding
   * @throws CliScriptException if required attributes for a creation of the CLI command of the
   *     Security-Domain are missing or are empty (security-domain-name)
   */
  public static CliCommandAction createSocketBindingCliAction(SocketBindingBean socket)
      throws CliScriptException {
    String errMsg = " in socket-binding must be set.";
    Utils.throwIfBlank(socket.getSocketPort(), errMsg, "Port");
    Utils.throwIfBlank(socket.getSocketName(), errMsg, "Name");

    ModelNode serverCmd = new ModelNode();
    serverCmd.get(ClientConstants.OP).set(ClientConstants.ADD);
    serverCmd.get(ClientConstants.OP_ADDR).add("socket-binding-group", "standard-sockets");
    serverCmd.get(ClientConstants.OP_ADDR).add("socket-binding", socket.getSocketName());

    serverCmd.get("port").set(socket.getSocketPort());

    CliApiCommandBuilder builder = new CliApiCommandBuilder(serverCmd);
    builder.addProperty("interface", socket.getSocketInterface());

    return new CliCommandAction(
        ServerMigrator.class, createSocketBindingScript(socket), builder.getCommand());
  }
  /**
   * Loads socket-bindings, which are already defined in fresh standalone files.
   *
   * @param ctx migration context
   * @throws LoadMigrationException if unmarshalling socket-bindings from standalone file fails
   */
  private void createDefaultSockets(MigrationContext ctx, ServerMigratorResource resource)
      throws LoadMigrationException {
    try {
      Unmarshaller unmarshaller =
          JAXBContext.newInstance(SocketBindingBean.class).createUnmarshaller();

      // TODO:  Read over Management API. MIGR-71
      NodeList bindings = ctx.getAS7ConfigXmlDoc().getElementsByTagName("socket-binding");
      for (int i = 0; i < bindings.getLength(); i++) {
        if (!(bindings.item(i) instanceof Element)) {
          continue;
        }
        SocketBindingBean socketBinding =
            (SocketBindingBean) unmarshaller.unmarshal(bindings.item(i));
        if ((socketBinding.getSocketName() != null) || (socketBinding.getSocketPort() != null)) {
          resource.getSocketTemp().add(socketBinding);
        }
      }
    } catch (JAXBException e) {
      throw new LoadMigrationException(
          "Parsing of socket-bindings in standalone file failed: " + e.getMessage(), e);
    }
  }
  /**
   * Creates a socket-binding if it doesn't already exists.
   *
   * @param port port of the connector, which will be converted to socket-binding
   * @param name name of the protocol which is used by connector (ajp/http/https)
   * @return name of the socket-binding so it cant be referenced in connector
   * @throws NodeGenerationException if createDefaultSocket fails to unmarshall socket-bindings
   */
  private static String createSocketBinding(
      String port, String name, ServerMigratorResource resource) throws NodeGenerationException {
    // TODO: Refactor and change the logic MIGR-71
    for (SocketBindingBean sb : resource.getSocketTemp()) {
      if (sb.getSocketPort().equals(port)) {
        return sb.getSocketName();
      }
      if (sb.getSocketName().equals(name)) {
        name = "createdSocket";
      }
    }

    SocketBindingBean socketBinding = new SocketBindingBean();

    for (SocketBindingBean sb : resource.getSocketBindings()) {
      if (sb.getSocketPort().equals(port)) {
        return sb.getSocketName();
      }
    }

    for (SocketBindingBean sb : resource.getSocketBindings()) {
      if (sb.getSocketName().equals(name)) {
        name = name.concat(resource.getRandomSocket().toString());
      }
    }

    socketBinding.setSocketName(name);
    socketBinding.setSocketPort(port);

    resource.getSocketBindings().add(socketBinding);

    return name;
  }