Search in sources :

Example 16 with ParsedOperationRequestHeader

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

the class RolloutPlanCompleter method complete.

/* (non-Javadoc)
     * @see org.jboss.as.cli.CommandLineCompleter#complete(org.jboss.as.cli.CommandContext, java.lang.String, int, java.util.List)
     */
@Override
public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) {
    if (!ctx.isDomainMode()) {
        return -1;
    }
    if (buffer.isEmpty()) {
        candidates.add("{rollout");
        return 0;
    }
    try {
        parsedOp.parseOperation(null, buffer, ctx);
    } catch (CommandFormatException e) {
        return -1;
    }
    if (parsedOp.isRequestComplete()) {
        return -1;
    }
    if (parsedOp.endsOnHeaderListStart() || parsedOp.endsOnHeaderSeparator()) {
        candidates.add("rollout");
        return parsedOp.getLastSeparatorIndex() + 1;
    }
    if (parsedOp.getLastHeader() == null) {
        if (ctx.getParsedCommandLine().getOriginalLine().endsWith(" ")) /* '{rollout ' */
        {
            final String originalLine = ctx.getParsedCommandLine().getOriginalLine();
            int bufferIndex = originalLine.lastIndexOf(buffer);
            if (bufferIndex == -1) {
                // that's illegal state
                return -1;
            }
            candidates.add("name=");
            candidates.addAll(Util.getServerGroups(ctx.getModelControllerClient()));
            return originalLine.length() - bufferIndex;
        } else if (ctx.getParsedCommandLine().getOriginalLine().endsWith("rollout")) {
            // In order to have the completion to be allowed to continue
            candidates.add(" ");
        }
        return buffer.length();
    }
    final ParsedOperationRequestHeader lastHeader = parsedOp.getLastHeader();
    if (!(lastHeader instanceof ParsedRolloutPlanHeader)) {
        throw new IllegalStateException("Expected " + ParsedRolloutPlanHeader.class + " but got " + lastHeader.getName() + " of " + lastHeader);
    }
    final ParsedRolloutPlanHeader rollout = (ParsedRolloutPlanHeader) lastHeader;
    if (rollout.endsOnPlanIdValueSeparator()) {
        candidates.addAll(Util.getNodeNames(ctx.getModelControllerClient(), address, Util.ROLLOUT_PLAN));
        return rollout.getLastSeparatorIndex() + 1;
    }
    final String planRef = rollout.getPlanRef();
    if (planRef != null) {
        final List<String> nodeNames = Util.getNodeNames(ctx.getModelControllerClient(), address, Util.ROLLOUT_PLAN);
        for (String name : nodeNames) {
            if (name.startsWith(planRef)) {
                candidates.add(name);
            }
        }
        return rollout.getLastChunkIndex();
    }
    if (rollout.hasProperties()) {
        final String lastName = rollout.getLastPropertyName();
        if (Util.ROLLBACK_ACROSS_GROUPS.equals(lastName)) {
            if (rollout.getLastPropertyValue() != null) {
                return -1;
            }
            candidates.add("}");
            candidates.add(";");
            return rollout.getLastChunkIndex() + lastName.length();
        }
        if (Util.ROLLBACK_ACROSS_GROUPS.startsWith(lastName)) {
            candidates.add(Util.ROLLBACK_ACROSS_GROUPS);
        }
        return rollout.getLastChunkIndex();
    }
    final List<String> serverGroups = Util.getServerGroups(ctx.getModelControllerClient());
    boolean containsAllGroups = true;
    for (String group : serverGroups) {
        if (!rollout.containsGroup(group)) {
            containsAllGroups = false;
            break;
        }
    }
    if (rollout.endsOnGroupSeparator()) {
        if (containsAllGroups) {
            return -1;
        }
        for (String group : serverGroups) {
            if (!rollout.containsGroup(group)) {
                candidates.add(group);
            }
        }
        return buffer.length();
    }
    final SingleRolloutPlanGroup lastGroup = rollout.getLastGroup();
    if (lastGroup == null) {
        return -1;
    }
    if (lastGroup.endsOnPropertyListEnd()) {
        if (!containsAllGroups) {
            candidates.add("^");
            candidates.add(",");
        } else if (Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
            candidates.add(Util.ROLLBACK_ACROSS_GROUPS);
        } else {
            candidates.add(" ");
        }
        return buffer.length();
    }
    if (lastGroup.endsOnPropertyListStart()) {
        candidates.add(Util.MAX_FAILED_SERVERS);
        candidates.add(Util.MAX_FAILURE_PERCENTAGE);
        candidates.add(Util.ROLLING_TO_SERVERS);
        candidates.add(Util.NOT_OPERATOR);
        return buffer.length();
    }
    // Only return the set of boolean properties
    if (lastGroup.endsOnNotOperator()) {
        candidates.add(Util.ROLLING_TO_SERVERS);
        return buffer.length();
    }
    if (lastGroup.hasProperties()) {
        // To propose the right end character
        boolean containsAll = lastGroup.hasProperty(Util.MAX_FAILED_SERVERS) && lastGroup.hasProperty(Util.MAX_FAILURE_PERCENTAGE) && lastGroup.hasProperty(Util.ROLLING_TO_SERVERS);
        final String propValue = lastGroup.getLastPropertyValue();
        if (propValue != null) {
            if (Util.TRUE.startsWith(propValue)) {
                candidates.add(Util.TRUE);
            } else if (Util.FALSE.startsWith(propValue)) {
                candidates.add(Util.FALSE);
            } else {
                candidates.add(containsAll ? ")" : ",");
                return buffer.length();
            }
        } else if (lastGroup.endsOnPropertyValueSeparator()) {
            if (Util.ROLLING_TO_SERVERS.equals(lastGroup.getLastPropertyName())) {
                candidates.add(Util.FALSE);
                candidates.add(containsAll ? ")" : ",");
            }
            return buffer.length();
        } else if (lastGroup.endsOnPropertySeparator()) {
            if (!lastGroup.hasProperty(Util.MAX_FAILED_SERVERS)) {
                candidates.add(Util.MAX_FAILED_SERVERS);
            }
            if (!lastGroup.hasProperty(Util.MAX_FAILURE_PERCENTAGE)) {
                candidates.add(Util.MAX_FAILURE_PERCENTAGE);
            }
            if (!lastGroup.hasProperty(Util.ROLLING_TO_SERVERS)) {
                candidates.add(Util.ROLLING_TO_SERVERS);
                candidates.add(Util.NOT_OPERATOR);
            }
            return lastGroup.getLastSeparatorIndex() + 1;
        } else {
            final String propName = lastGroup.getLastPropertyName();
            if (Util.MAX_FAILED_SERVERS.startsWith(propName)) {
                candidates.add(Util.MAX_FAILED_SERVERS + '=');
            }
            if (Util.MAX_FAILURE_PERCENTAGE.startsWith(propName)) {
                candidates.add(Util.MAX_FAILURE_PERCENTAGE + '=');
            } else if (Util.ROLLING_TO_SERVERS.equals(propName)) {
                if (lastGroup.isLastPropertyNegated() && !containsAll) {
                    candidates.add(Util.ROLLING_TO_SERVERS + ",");
                } else {
                    candidates.add("=" + Util.FALSE);
                    if (!containsAll) {
                        candidates.add(",");
                    } else {
                        candidates.add(")");
                    }
                }
            } else if (Util.ROLLING_TO_SERVERS.startsWith(propName)) {
                candidates.add(Util.ROLLING_TO_SERVERS);
            }
        }
        if (candidates.isEmpty() && containsAll) {
            candidates.add(")");
        }
        return lastGroup.getLastChunkIndex();
    }
    if (Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
        candidates.add(Util.ROLLBACK_ACROSS_GROUPS);
        return buffer.length();
    }
    int result = lastGroup.getLastChunkIndex();
    final String groupName = lastGroup.getGroupName();
    boolean isLastGroup = false;
    for (String group : serverGroups) {
        if (group.equals(groupName)) {
            isLastGroup = true;
        }
        if (!isLastGroup && group.startsWith(groupName)) {
            candidates.add(group);
        }
    }
    if (Util.NAME.startsWith(groupName)) {
        candidates.add("name=");
    } else {
        if (candidates.isEmpty()) {
            if (isLastGroup) {
                candidates.add("(");
                if (containsAllGroups) {
                    candidates.add(Util.ROLLBACK_ACROSS_GROUPS);
                } else {
                    candidates.add(",");
                    candidates.add("^");
                }
            }
        }
    }
    return result;
}
Also used : ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) CommandFormatException(org.jboss.as.cli.CommandFormatException)

Example 17 with ParsedOperationRequestHeader

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

the class RolloutPlanParsingTestCase method testEndsOnGroupPropertyValue.

@Test
public void testEndsOnGroupPropertyValue() throws Exception {
    parse(":do{ rollout groupA( prop = v");
    assertFalse(handler.hasAddress());
    assertTrue(handler.hasOperationName());
    assertFalse(handler.hasProperties());
    assertFalse(handler.endsOnAddressOperationNameSeparator());
    assertFalse(handler.endsOnPropertyListStart());
    assertFalse(handler.endsOnPropertySeparator());
    assertFalse(handler.endsOnPropertyValueSeparator());
    assertFalse(handler.endsOnNodeSeparator());
    assertFalse(handler.endsOnNodeTypeNameSeparator());
    assertFalse(handler.endsOnSeparator());
    assertFalse(handler.endsOnHeaderListStart());
    assertFalse(handler.isRequestComplete());
    assertTrue(handler.hasHeaders());
    final Collection<ParsedOperationRequestHeader> headers = handler.getHeaders();
    assertEquals(1, headers.size());
    final ParsedOperationRequestHeader header = headers.iterator().next();
    assertTrue(header instanceof ParsedRolloutPlanHeader);
    ParsedRolloutPlanHeader rollout = (ParsedRolloutPlanHeader) header;
    final SingleRolloutPlanGroup group = rollout.getLastGroup();
    assertNotNull(group);
    assertEquals("groupA", group.getGroupName());
    assertFalse(group.endsOnPropertyListStart());
    assertFalse(group.endsOnPropertyListEnd());
    assertTrue(group.hasProperties());
    assertFalse(group.endsOnPropertyValueSeparator());
    assertEquals("prop", group.getLastPropertyName());
    assertEquals("v", group.getLastPropertyValue());
    assertEquals(28, group.getLastChunkIndex());
}
Also used : ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) SingleRolloutPlanGroup(org.jboss.as.cli.operation.impl.SingleRolloutPlanGroup) ParsedRolloutPlanHeader(org.jboss.as.cli.operation.impl.ParsedRolloutPlanHeader) Test(org.junit.Test)

Example 18 with ParsedOperationRequestHeader

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

the class RolloutPlanParsingTestCase method testRolloutSingleGroupWithProps.

@Test
public void testRolloutSingleGroupWithProps() throws Exception {
    // parse("/profile=default/subsystem=threads/thread-factory=mytf:do{ rollout in-series=groupA(rolling-to-servers=true,max-failure-percentage=20)");
    parse("/profile=default/subsystem=threads/thread-factory=mytf:do{ rollout groupA(rolling-to-servers=true,max-failure-percentage=20)");
    assertTrue(handler.hasAddress());
    assertTrue(handler.hasOperationName());
    assertFalse(handler.hasProperties());
    assertFalse(handler.endsOnAddressOperationNameSeparator());
    assertFalse(handler.endsOnPropertyListStart());
    assertFalse(handler.endsOnPropertySeparator());
    assertFalse(handler.endsOnPropertyValueSeparator());
    assertFalse(handler.endsOnNodeSeparator());
    assertFalse(handler.endsOnNodeTypeNameSeparator());
    assertFalse(handler.endsOnSeparator());
    assertFalse(handler.endsOnHeaderListStart());
    assertFalse(handler.isRequestComplete());
    assertTrue(handler.hasHeaders());
    final Collection<ParsedOperationRequestHeader> headers = handler.getHeaders();
    assertEquals(1, headers.size());
    final ParsedOperationRequestHeader header = headers.iterator().next();
    assertTrue(header instanceof ParsedRolloutPlanHeader);
    final ModelNode node = new ModelNode();
    final ModelNode inSeries = node.get(Util.ROLLOUT_PLAN).get(Util.IN_SERIES);
    final ModelNode groupA = new ModelNode();
    final ModelNode groupProps = groupA.get("groupA");
    groupProps.get("rolling-to-servers").set("true");
    groupProps.get("max-failure-percentage").set("20");
    inSeries.add().get(Util.SERVER_GROUP).set(groupA);
    final ModelNode headersNode = new ModelNode();
    header.addTo(ctx, headersNode);
    assertEquals(node, headersNode);
}
Also used : ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) ModelNode(org.jboss.dmr.ModelNode) ParsedRolloutPlanHeader(org.jboss.as.cli.operation.impl.ParsedRolloutPlanHeader) Test(org.junit.Test)

Example 19 with ParsedOperationRequestHeader

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

the class RolloutPlanParsingTestCase method testRolloutIdWithValue.

@Test
public void testRolloutIdWithValue() throws Exception {
    parse(":do{ rollout id = myplan}");
    assertFalse(handler.hasAddress());
    assertTrue(handler.hasOperationName());
    assertFalse(handler.hasProperties());
    assertFalse(handler.endsOnAddressOperationNameSeparator());
    assertFalse(handler.endsOnPropertyListStart());
    assertFalse(handler.endsOnPropertySeparator());
    assertFalse(handler.endsOnPropertyValueSeparator());
    assertFalse(handler.endsOnNodeSeparator());
    assertFalse(handler.endsOnNodeTypeNameSeparator());
    assertFalse(handler.endsOnSeparator());
    assertFalse(handler.endsOnHeaderListStart());
    assertTrue(handler.isRequestComplete());
    assertTrue(handler.hasHeaders());
    final Collection<ParsedOperationRequestHeader> headers = handler.getHeaders();
    assertEquals(1, headers.size());
    final ParsedOperationRequestHeader header = headers.iterator().next();
    assertTrue(header instanceof ParsedRolloutPlanHeader);
    final ParsedRolloutPlanHeader rollout = (ParsedRolloutPlanHeader) header;
    assertEquals("myplan", rollout.getPlanRef());
    assertEquals(18, rollout.getLastChunkIndex());
}
Also used : ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) ParsedRolloutPlanHeader(org.jboss.as.cli.operation.impl.ParsedRolloutPlanHeader) Test(org.junit.Test)

Example 20 with ParsedOperationRequestHeader

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

the class RolloutPlanParsingTestCase method testTwoConcurrentGroups.

@Test
public void testTwoConcurrentGroups() throws Exception {
    parse("/profile=default/subsystem=threads/thread-factory=mytf:do{ rollout " + "groupA(rolling-to-servers=true,max-failure-percentage=20) ^  groupB");
    assertTrue(handler.hasAddress());
    assertTrue(handler.hasOperationName());
    assertFalse(handler.hasProperties());
    assertFalse(handler.endsOnAddressOperationNameSeparator());
    assertFalse(handler.endsOnPropertyListStart());
    assertFalse(handler.endsOnPropertySeparator());
    assertFalse(handler.endsOnPropertyValueSeparator());
    assertFalse(handler.endsOnNodeSeparator());
    assertFalse(handler.endsOnNodeTypeNameSeparator());
    assertFalse(handler.endsOnSeparator());
    assertFalse(handler.endsOnHeaderListStart());
    assertFalse(handler.isRequestComplete());
    assertTrue(handler.hasHeaders());
    final Collection<ParsedOperationRequestHeader> headers = handler.getHeaders();
    assertEquals(1, headers.size());
    final ParsedOperationRequestHeader header = headers.iterator().next();
    assertTrue(header instanceof ParsedRolloutPlanHeader);
    final ModelNode node = new ModelNode();
    final ModelNode inSeries = node.get(Util.ROLLOUT_PLAN).get(Util.IN_SERIES);
    final ModelNode concurrent = new ModelNode();
    final ModelNode cg = concurrent.get(Util.CONCURRENT_GROUPS);
    ModelNode group = cg.get("groupA");
    group.get("rolling-to-servers").set("true");
    group.get("max-failure-percentage").set("20");
    group = cg.get("groupB");
    inSeries.add().set(concurrent);
    final ModelNode headersNode = new ModelNode();
    header.addTo(ctx, headersNode);
    assertEquals(node, headersNode);
}
Also used : ParsedOperationRequestHeader(org.jboss.as.cli.operation.ParsedOperationRequestHeader) ModelNode(org.jboss.dmr.ModelNode) ParsedRolloutPlanHeader(org.jboss.as.cli.operation.impl.ParsedRolloutPlanHeader) Test(org.junit.Test)

Aggregations

ParsedOperationRequestHeader (org.jboss.as.cli.operation.ParsedOperationRequestHeader)27 Test (org.junit.Test)22 ParsedRolloutPlanHeader (org.jboss.as.cli.operation.impl.ParsedRolloutPlanHeader)19 ModelNode (org.jboss.dmr.ModelNode)13 SingleRolloutPlanGroup (org.jboss.as.cli.operation.impl.SingleRolloutPlanGroup)7 CommandFormatException (org.jboss.as.cli.CommandFormatException)2 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)1 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)1 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)1 Node (org.jboss.as.cli.operation.OperationRequestAddress.Node)1