Search in sources :

Example 16 with Node

use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.

the class BaseOperationCommand method isAddressValid.

protected boolean isAddressValid(CommandContext ctx) {
    final ModelNode request = new ModelNode();
    final ModelNode address = request.get(Util.ADDRESS);
    address.setEmptyList();
    request.get(Util.OPERATION).set(Util.VALIDATE_ADDRESS);
    final ModelNode addressValue = request.get(Util.VALUE);
    for (OperationRequestAddress.Node node : requiredAddress) {
        addressValue.add(node.getType(), node.getName());
    }
    final ModelNode response;
    try {
        response = ctx.getModelControllerClient().execute(request);
    } catch (IOException e) {
        return false;
    }
    final ModelNode result = response.get(Util.RESULT);
    if (!result.isDefined()) {
        return false;
    }
    final ModelNode valid = result.get(Util.VALID);
    if (!valid.isDefined()) {
        return false;
    }
    return valid.asBoolean();
}
Also used : Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) DefaultOperationRequestAddress(org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress)

Example 17 with Node

use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.

the class DefaultPrefixFormatter method format.

/* (non-Javadoc)
     * @see org.jboss.as.cli.PrefixFormatter#format(org.jboss.as.cli.Prefix)
     */
@Override
public String format(OperationRequestAddress prefix) {
    Iterator<Node> iterator = prefix.iterator();
    if (!iterator.hasNext()) {
        return "/";
    }
    StringBuilder builder = new StringBuilder();
    builder.append('/');
    Node next = iterator.next();
    builder.append(next.getType());
    if (next.getName() != null) {
        builder.append('=').append(next.getName());
    }
    while (iterator.hasNext()) {
        builder.append('/');
        next = iterator.next();
        builder.append(next.getType());
        if (next.getName() != null) {
            builder.append('=').append(next.getName());
        }
    }
    return builder.toString();
}
Also used : Node(org.jboss.as.cli.operation.OperationRequestAddress.Node)

Example 18 with Node

use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.

the class Util method buildRequest.

public static ModelNode buildRequest(CommandContext ctx, final OperationRequestAddress address, String operation) throws CommandFormatException {
    final ModelNode request = new ModelNode();
    request.get(Util.OPERATION).set(operation);
    final ModelNode addressNode = request.get(Util.ADDRESS);
    if (address.isEmpty()) {
        addressNode.setEmptyList();
    } else {
        if (address.endsOnType()) {
            throw new CommandFormatException("The address ends on a type: " + address.getNodeType());
        }
        for (OperationRequestAddress.Node node : address) {
            addressNode.add(node.getType(), node.getName());
        }
    }
    return request;
}
Also used : Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) ModelNode(org.jboss.dmr.ModelNode) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress)

Example 19 with Node

use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.

the class Util method toOperationRequest.

private static ModelNode toOperationRequest(CommandContext ctx, ParsedCommandLine parsedLine, Attachments attachments, boolean description) throws CommandFormatException {
    if (parsedLine.getFormat() != OperationFormat.INSTANCE) {
        throw new OperationFormatException("The line does not follow the operation request format");
    }
    ModelNode request = new ModelNode();
    ModelNode addressNode = request.get(Util.ADDRESS);
    if (parsedLine.getAddress().isEmpty()) {
        addressNode.setEmptyList();
    } else {
        Iterator<Node> iterator = parsedLine.getAddress().iterator();
        while (iterator.hasNext()) {
            OperationRequestAddress.Node node = iterator.next();
            if (node.getName() != null) {
                addressNode.add(node.getType(), node.getName());
            } else if (iterator.hasNext()) {
                throw new OperationFormatException("The node name is not specified for type '" + node.getType() + "'");
            }
        }
    }
    final String operationName = parsedLine.getOperationName();
    if (operationName == null || operationName.isEmpty()) {
        throw new OperationFormatException("The operation name is missing or the format of the operation request is wrong.");
    }
    request.get(Util.OPERATION).set(operationName);
    ModelNode outcome = null;
    if (description) {
        outcome = (ModelNode) ctx.get(Scope.REQUEST, DESCRIPTION_RESPONSE);
        if (outcome == null) {
            outcome = retrieveDescription(ctx, request, false);
            if (outcome != null) {
                ctx.set(Scope.REQUEST, DESCRIPTION_RESPONSE, outcome);
            }
        }
        if (outcome != null) {
            if (!outcome.has(Util.RESULT)) {
                throw new CommandFormatException("Failed to perform " + Util.READ_OPERATION_DESCRIPTION + " to validate the request: result is not available.");
            } else {
                outcome = outcome.get(Util.RESULT).get(Util.REQUEST_PROPERTIES);
            }
        }
    }
    for (String propName : parsedLine.getPropertyNames()) {
        String value = parsedLine.getPropertyValue(propName);
        if (propName == null || propName.trim().isEmpty())
            throw new OperationFormatException("The argument name is not specified: '" + propName + "'");
        if (value == null || value.trim().isEmpty()) {
            throw new OperationFormatException("The argument value is not specified for " + propName + ": '" + value + "'");
        }
        final ModelNode toSet = ArgumentValueConverter.DEFAULT.fromString(ctx, value);
        if (outcome != null && outcome.hasDefined(propName)) {
            try {
                ModelNode p = outcome.get(propName);
                if (p.hasDefined("type")) {
                    applyReplacements(ctx, propName, toSet, p, p.get("type").asType(), attachments);
                }
            } catch (Throwable ex) {
                // System.err.println("FAULTY " + propName + " " + outcome.get(propName));
                throw ex;
            }
        }
        request.get(propName).set(toSet);
    }
    if (parsedLine.getLastHeaderName() != null) {
        throw new OperationFormatException("Header '" + parsedLine.getLastHeaderName() + "' is not complete.");
    }
    final Collection<ParsedOperationRequestHeader> headers = parsedLine.getHeaders();
    if (headers != null && !headers.isEmpty()) {
        final ModelNode headersNode = request.get(Util.OPERATION_HEADERS);
        for (ParsedOperationRequestHeader header : headers) {
            header.addTo(ctx, headersNode);
        }
    }
    return request;
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) ModelNode(org.jboss.dmr.ModelNode) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) ModelNode(org.jboss.dmr.ModelNode) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress)

Example 20 with Node

use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.

the class AddressOnlyParsingTestCase method testSlashNodeName.

@Test
public void testSlashNodeName() throws Exception {
    DefaultCallbackHandler handler = new DefaultCallbackHandler();
    parser.parse("/subsystem=undertow/server=default-server/host=default-host/location=\\/", handler);
    OperationRequestAddress address = handler.getAddress();
    assertNotNull(address);
    Iterator<Node> nodes = address.iterator();
    assertTrue(nodes.hasNext());
    Node node = nodes.next();
    assertEquals("subsystem", node.getType());
    assertEquals("undertow", node.getName());
    assertTrue(nodes.hasNext());
    node = nodes.next();
    assertEquals("server", node.getType());
    assertEquals("default-server", node.getName());
    assertTrue(nodes.hasNext());
    node = nodes.next();
    assertEquals("host", node.getType());
    assertEquals("default-host", node.getName());
    assertTrue(nodes.hasNext());
    node = nodes.next();
    assertEquals("location", node.getType());
    assertEquals("/", node.getName());
    assertFalse(nodes.hasNext());
}
Also used : Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) DefaultCallbackHandler(org.jboss.as.cli.operation.impl.DefaultCallbackHandler) DefaultOperationRequestAddress(org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress) Test(org.junit.Test)

Aggregations

Node (org.jboss.as.cli.operation.OperationRequestAddress.Node)36 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)35 DefaultOperationRequestAddress (org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress)32 DefaultCallbackHandler (org.jboss.as.cli.operation.impl.DefaultCallbackHandler)29 Test (org.junit.Test)28 ModelNode (org.jboss.dmr.ModelNode)14 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)4 IOException (java.io.IOException)3 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)1 CommandArgument (org.jboss.as.cli.CommandArgument)1 CommandContext (org.jboss.as.cli.CommandContext)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)1 ParsedOperationRequestHeader (org.jboss.as.cli.operation.ParsedOperationRequestHeader)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1