Java Code Examples for org.jboss.dmr.ModelType#OBJECT

The following examples show how to use org.jboss.dmr.ModelType#OBJECT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlToJson.java    From revapi with Apache License 2.0 6 votes vote down vote up
public ModelNode navigate(ModelNode root) {
    if (tokens.isEmpty()) {
        return root;
    }

    ModelNode current = root;
    for (String token : tokens) {
        if (current.getType() == ModelType.OBJECT) {
            current = current.get(unescape(token));
        } else if (current.getType() == ModelType.LIST) {
            int idx = Integer.parseInt(token);
            current = current.get(idx);
        } else {
            throw new IllegalArgumentException("Cannot navigate to '" + this + "' in " + root);
        }
    }
    return current;
}
 
Example 2
Source File: PropagatingCorrector.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public ModelNode correct(ModelNode newValue, ModelNode currentValue) {
    if(newValue.getType() == ModelType.UNDEFINED) {
        return newValue;
    }
    if(newValue.getType() != ModelType.OBJECT || currentValue.getType() != ModelType.OBJECT) {
        return newValue;
    }
    final Set<String> operationKeys = newValue.keys();
    final Set<String> currentKeys = currentValue.keys();
    for(String currentKey : currentKeys) {
        if(!operationKeys.contains(currentKey)) {
            newValue.get(currentKey).set(currentValue.get(currentKey));
        }
    }
    return newValue;
}
 
Example 3
Source File: LoggingSubsystemTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public ModelNode fixModel(final ModelNode modelNode) {
    // Recursively remove the attributes
    if (modelNode.getType() == ModelType.OBJECT) {
        for (Property property : modelNode.asPropertyList()) {
            final String name = property.getName();
            final ModelNode value = property.getValue();
            if (value.isDefined()) {
                if (value.getType() == ModelType.OBJECT) {
                    modelNode.get(name).set(fixModel(value));
                } else if (value.getType() == ModelType.STRING) {
                    if (name.equals(attribute.getName())) {
                        modelNode.get(name).set(value.asString().replace(from, to));
                    }
                }
            }
        }
    }
    return modelNode;
}
 
Example 4
Source File: FilterConversionTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Removes undefined attributes from the model.
 *
 * @param model the model to remove undefined attributes from
 *
 * @return the new model
 */
static ModelNode removeUndefined(final ModelNode model) {
    final ModelNode result = new ModelNode();
    if (model.getType() == ModelType.OBJECT) {
        for (Property property : model.asPropertyList()) {
            final ModelNode value = property.getValue();
            if (value.isDefined()) {
                if (value.getType() == ModelType.OBJECT) {
                    final ModelNode m = removeUndefined(property.getValue());
                    if (m.isDefined()) {
                        result.get(property.getName()).set(m);
                    }
                } else {
                    result.get(property.getName()).set(value);
                }
            }
        }
    }
    return result;
}
 
Example 5
Source File: FileCorrector.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public ModelNode correct(final ModelNode newValue, final ModelNode currentValue) {
    if (newValue.getType() == ModelType.UNDEFINED) {
        return newValue;
    }
    if (newValue.getType() != ModelType.OBJECT || currentValue.getType() != ModelType.OBJECT) {
        return newValue;
    }
    final ModelNode newPath = newValue.get(PATH.getName());
    if (newPath.isDefined() && !AbstractPathService.isAbsoluteUnixOrWindowsPath(newPath.asString())) {
        if (currentValue.hasDefined(RELATIVE_TO.getName()) && !newValue.hasDefined(RELATIVE_TO.getName())) {
            newValue.get(RELATIVE_TO.getName()).set(currentValue.get(RELATIVE_TO.getName()));
        }
    }
    return newValue;
}
 
Example 6
Source File: ObjectTypeValidator.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ObjectTypeValidator(final boolean nullable, final AttributeDefinition... attributes) {
    super(nullable, true, false, ModelType.OBJECT);
    allowedValues = new HashMap<String, AttributeDefinition>(attributes.length);
    for (AttributeDefinition attribute : attributes) {
        allowedValues.put(attribute.getName(), attribute);
    }
}
 
Example 7
Source File: ValueTypeCompleter.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<String> getCompletedValueCandidates(ModelNode propType, PropertyVisibility visibility) {
    // In this case we need to reach the end of the stream and add separator.
    valLength = buffer.length() - lastStateIndex;
    // Do we have some properties to propose?
    if (propType.getType() == ModelType.OBJECT) {
        if (visibility.hasMore()) {
            return Collections.singletonList(",");
        } else {
            return Collections.singletonList("}");
        }
    } else {
        return Collections.emptyList();
    }
}
 
Example 8
Source File: ModelTestUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ModelNode trimUndefinedChildren(ModelNode model) {
    ModelNode copy = model.clone();
    for (String key : new HashSet<String>(copy.keys())) {
        if (!copy.hasDefined(key)) {
            copy.remove(key);
        } else if (copy.get(key).getType() == ModelType.OBJECT) {
            for (ModelNode mn : model.get(key).asList()) {
                boolean undefined = true;
                Property p = mn.asProperty();
                if (p.getValue().getType() != ModelType.OBJECT) { continue; }
                for (String subKey : new HashSet<String>(p.getValue().keys())) {
                    if (copy.get(key, p.getName()).hasDefined(subKey)) {
                        undefined = false;
                        break;
                    } else {
                        copy.get(key, p.getName()).remove(subKey);
                    }
                }
                if (undefined) {
                    copy.get(key).remove(p.getName());
                    if (!copy.hasDefined(key)) {
                        copy.remove(key);
                    } else if (copy.get(key).getType() == ModelType.OBJECT) {     //this is stupid workaround
                        if (copy.get(key).keys().size() == 0) {
                            copy.remove(key);
                        }
                    }
                }
            }
        }
    }
    return copy;
}
 
Example 9
Source File: KnownIssuesValidationConfiguration.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void replaceLocalDomainControllerType(ModelNode description) {
    /*
     The description looks like, and we don't want the local to be OBJECT for validation so we replace it with STRING
    "domain-controller" => {
        "type" => OBJECT,
        "description" => "Configuration of how the host should interact with the Domain Controller",
        "expressions-allowed" => false,
        "nillable" => false,
        "value-type" => {
            "local" => {
                "type" => OBJECT,
                "description" => "Configure a local Domain Controller",
                "expressions-allowed" => false,
                "nillable" => true
            },
            "remote" => {
                "type" => OBJECT,
                "description" => "Remote Domain Controller connection configuration",
                "expressions-allowed" => false,
                "nillable" => true,
                "value-type" => {"host" => {
                    "type" => INT,
                    "description" => "The address used for the Domain Controller connection",
                    "expressions-allowed" => false,
                    "nillable" => false
                }}
            }
        },
        "access-type" => "read-only",
        "storage" => "runtime"
    }*/
    ModelNode node = find(description, ATTRIBUTES, DOMAIN_CONTROLLER, VALUE_TYPE, LOCAL, TYPE);
    if (node != null) {
        if (node.getType() != ModelType.TYPE && node.asType() != ModelType.OBJECT) {
            //Validate it here before we do the naughty replacement
            throw new IllegalStateException("Bad local domain controller " + node);
        }
        node.set(ModelType.STRING);
    }
}
 
Example 10
Source File: ObjectTypeAttributeDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Overrides the superclass implementation to allow the AttributeDefinition for each field in the
 * object to in turn resolve that field.
 *
 * {@inheritDoc}
 */
@Override
public ModelNode resolveValue(ExpressionResolver resolver, ModelNode value) throws OperationFailedException {

    // Pass non-OBJECT values through the superclass so it can reject weird values and, in the odd chance
    // that's how this object is set up, turn undefined into a default list value.
    ModelNode superResult = value.getType() == ModelType.OBJECT ? value : super.resolveValue(resolver, value);

    // If it's not an OBJECT (almost certainly UNDEFINED), then nothing more we can do
    if (superResult.getType() != ModelType.OBJECT) {
        return superResult;
    }

    // Resolve each field.
    // Don't mess with the original value
    ModelNode clone = superResult == value ? value.clone() : superResult;
    ModelNode result = new ModelNode();
    for (AttributeDefinition field : valueTypes) {
        String fieldName = field.getName();
        if (clone.has(fieldName)) {
            result.get(fieldName).set(field.resolveValue(resolver, clone.get(fieldName)));
        } else {
            // Input doesn't have a child for this field.
            // Don't create one in the output unless the AD produces a default value.
            // TBH this doesn't make a ton of sense, since any object should have
            // all of its fields, just some may be undefined. But doing it this
            // way may avoid breaking some code that is incorrectly checking node.has("xxx")
            // instead of node.hasDefined("xxx")
            ModelNode val = field.resolveValue(resolver, new ModelNode());
            if (val.isDefined()) {
                result.get(fieldName).set(val);
            }
        }
    }
    // Validate the entire object
    getValidator().validateParameter(getName(), result);
    return result;
}
 
Example 11
Source File: ModelParserUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void compare(ModelNode node1, ModelNode node2) {
    Assert.assertEquals(node1.getType(), node2.getType());
    if (node1.getType() == ModelType.OBJECT) {
        final Set<String> keys1 = node1.keys();
        final Set<String> keys2 = node2.keys();
        Assert.assertEquals(node1 + "\n" + node2, keys1.size(), keys2.size());

        for (String key : keys1) {
            final ModelNode child1 = node1.get(key);
            Assert.assertTrue("Missing: " + key + "\n" + node1 + "\n" + node2, node2.has(key));
            final ModelNode child2 = node2.get(key);
            if (child1.isDefined()) {
                Assert.assertTrue(child1.toString(), child2.isDefined());
                compare(child1, child2);
            } else {
                Assert.assertFalse(child2.asString(), child2.isDefined());
            }
        }
    } else if (node1.getType() == ModelType.LIST) {
        List<ModelNode> list1 = node1.asList();
        List<ModelNode> list2 = node2.asList();
        Assert.assertEquals(list1 + "\n" + list2, list1.size(), list2.size());

        for (int i = 0; i < list1.size(); i++) {
            compare(list1.get(i), list2.get(i));
        }

    } else if (node1.getType() == ModelType.PROPERTY) {
        Property prop1 = node1.asProperty();
        Property prop2 = node2.asProperty();
        Assert.assertEquals(prop1 + "\n" + prop2, prop1.getName(), prop2.getName());
        compare(prop1.getValue(), prop2.getValue());

    } else {
        Assert.assertEquals("\n\"" + node1.asString() + "\"\n\"" + node2.asString() + "\"\n-----", node1.asString().trim(), node2.asString().trim());
    }
}
 
Example 12
Source File: FileValidator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FileValidator() {
    super(ModelType.OBJECT);
}
 
Example 13
Source File: PropertyObjectTypeAttributeDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Builder(final String name, final AttributeDefinition... valueTypes) {
    super(name, ModelType.OBJECT, true);
    this.valueTypes = valueTypes;
}
 
Example 14
Source File: FixedObjectTypeAttributeDefinition.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public Builder(final String name, final AttributeDefinition... valueTypes) {
  super(name, ModelType.OBJECT, true);
  this.valueTypes = valueTypes;

}
 
Example 15
Source File: ParsedInterfaceCriteria.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static ParsedInterfaceCriteria parse(final ModelNode model, final boolean specified, final ExpressionResolver expressionResolver) {
    if (model.getType() != ModelType.OBJECT) {
        return new ParsedInterfaceCriteria(ControllerLogger.ROOT_LOGGER.illegalInterfaceCriteria(model.getType(), ModelType.OBJECT));
    }
    // Remove operation params
    final ModelNode subModel = model.clone();
    subModel.remove(ModelDescriptionConstants.OP);
    subModel.remove(ModelDescriptionConstants.OP_ADDR);
    subModel.remove(ModelDescriptionConstants.OPERATION_HEADERS);
    final ParsedInterfaceCriteria parsed;
    if(subModel.hasDefined(ANY_ADDRESS) && subModel.get(ANY_ADDRESS).asBoolean(false)) {
        parsed = ParsedInterfaceCriteria.ANY;
    } else {
        try {
            final List<Property> nodes = subModel.asPropertyList();
            final Set<InterfaceCriteria> criteriaSet = new LinkedHashSet<InterfaceCriteria>();
            for (final Property property : nodes) {
                final InterfaceCriteria criterion = parseCriteria(property, false, expressionResolver);
                if (criterion instanceof WildcardInetAddressInterfaceCriteria) {
                    // AS7-1668: stop processing and just return the any binding.
                    if (nodes.size() > 1) {
                        MGMT_OP_LOGGER.wildcardAddressDetected();
                    }
                    return ParsedInterfaceCriteria.ANY;
                }
                else if (criterion != null) {
                    criteriaSet.add(criterion);
                }
            }
            String validation = new CriteriaValidator(criteriaSet).validate();
            parsed = validation == null ? new ParsedInterfaceCriteria(criteriaSet) : new ParsedInterfaceCriteria(validation);
        } catch (ParsingException p) {
            return new ParsedInterfaceCriteria(p.msg);
        } catch (OperationFailedException e) {
            return new ParsedInterfaceCriteria(e.getMessage());
        }
    }
    if (specified && parsed.getFailureMessage() == null && ! parsed.isAnyLocal() && parsed.getCriteria().size() == 0) {
        return new ParsedInterfaceCriteria(ControllerLogger.ROOT_LOGGER.noInterfaceCriteria());
    }
    return parsed;
}
 
Example 16
Source File: ObjectTypeAttributeDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Builder(final String name, final AttributeDefinition... valueTypes) {
    super(name, ModelType.OBJECT, true);
    this.valueTypes = valueTypes;
    setAttributeParser(AttributeParser.OBJECT_PARSER);
    setAttributeMarshaller(AttributeMarshaller.ATTRIBUTE_OBJECT);
}
 
Example 17
Source File: ModelTestUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void compare(ModelNode node1, ModelNode node2, boolean ignoreUndefined, boolean ignoreType, Stack<String> stack) {
    if (! ignoreType) {
        Assert.assertEquals(getCompareStackAsString(stack) + " types", node1.getType(), node2.getType());
    }
    if (node1.getType() == ModelType.OBJECT) {
        ModelNode model1 = ignoreUndefined ? trimUndefinedChildren(node1) : node1;
        ModelNode model2 = ignoreUndefined ? trimUndefinedChildren(node2) : node2;
        final Set<String> keys1 = new TreeSet<String>(model1.keys());
        final Set<String> keys2 = new TreeSet<String>(model2.keys());

        // compare string representations of the keys to help see the difference
        if (!keys1.toString().equals(keys2.toString())){
            //Just to make debugging easier
            System.out.print("");
        }
        Assert.assertEquals(getCompareStackAsString(stack) + ": " + node1 + "\n" + node2, keys1.toString(), keys2.toString());
        Assert.assertTrue(keys1.containsAll(keys2));

        for (String key : keys1) {
            final ModelNode child1 = model1.get(key);
            Assert.assertTrue("Missing: " + key + "\n" + node1 + "\n" + node2, model2.has(key));
            final ModelNode child2 = model2.get(key);

            if (child1.isDefined()) {
                if (!ignoreUndefined) {
                    Assert.assertTrue(getCompareStackAsString(stack) + " key=" + key + "\n with child1 \n" + child1.toString() + "\n has child2 not defined\n node2 is:\n" + node2.toString(), child2.isDefined());
                }
                stack.push(key + "/");
                compare(child1, child2, ignoreUndefined, ignoreType, stack);
                stack.pop();
            } else if (!ignoreUndefined) {
                Assert.assertFalse(getCompareStackAsString(stack) + " key=" + key + "\n with child1 undefined has child2 \n" + child2.asString(), child2.isDefined());
            }
        }
    } else if (node1.getType() == ModelType.LIST) {
        List<ModelNode> list1 = node1.asList();
        List<ModelNode> list2 = node2.asList();
        Assert.assertEquals(list1 + "\n" + list2, list1.size(), list2.size());

        for (int i = 0; i < list1.size(); i++) {
            stack.push(i + "/");
            compare(list1.get(i), list2.get(i), ignoreUndefined, ignoreType, stack);
            stack.pop();
        }

    } else if (node1.getType() == ModelType.PROPERTY) {
        Property prop1 = node1.asProperty();
        Property prop2 = node2.asProperty();
        Assert.assertEquals(prop1 + "\n" + prop2, prop1.getName(), prop2.getName());
        stack.push(prop1.getName() + "/");
        compare(prop1.getValue(), prop2.getValue(), ignoreUndefined, ignoreType, stack);
        stack.pop();

    } else {
        Assert.assertEquals(getCompareStackAsString(stack) +
                    "\n\"" + node1.asString() + "\"\n\"" + node2.asString() + "\"\n-----", node1.asString().trim(), node2.asString().trim());
    }
}
 
Example 18
Source File: ModelHarmonizer.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void harmonizeModel(ModelVersion modelVersion, ModelNode source, ModelNode target) {
    if (source.getType() == ModelType.OBJECT && source.asInt() == 0 && !target.isDefined()) {
        target.setEmptyObject();
    }
}
 
Example 19
Source File: DMRDriver.java    From hawkular-agent with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Map<DMRNodeLocation, ModelNode> fetchNodes(DMRNodeLocation query) throws ProtocolException {

    ReadResourceOperationBuilder<?> opBuilder = OperationBuilder
            .readResource()//
            .address(query.getPathAddress()) //
            .includeRuntime();

    // time the execute separately - we want to time ONLY the execute call
    OperationResult<?> opResult;
    try (Context timerContext = diagnostics.getRequestTimer().time()) {
        opResult = opBuilder.execute(client);
    } catch (Exception e) {
        diagnostics.getErrorRate().mark(1);
        throw new ProtocolException("Error fetching nodes for query [" + query + "]", e);
    }

    Optional<ModelNode> resultNode = opResult.getOptionalResultNode();
    if (resultNode.isPresent()) {
        ModelNode n = resultNode.get();
        if (n.getType() == ModelType.OBJECT) {
            return Collections.singletonMap(query, n);
        } else if (n.getType() == ModelType.LIST) {
            Map<DMRNodeLocation, ModelNode> result = new HashMap<>();
            List<ModelNode> list = n.asList();
            for (ModelNode item : list) {
                ModelNode pathAddress = item.get(JBossASClient.ADDRESS);
                pathAddress = makePathAddressFullyQualified_WFLY6628(query.getPathAddress(), pathAddress);
                result.put(DMRNodeLocation.of(pathAddress, true, true), JBossASClient.getResults(item));
            }
            return Collections.unmodifiableMap(result);
        } else {
            throw new IllegalStateException("Invalid type - please report this bug: " + n.getType()
                    + " [[" + n.toString() + "]]");
        }

    } else {
        return Collections.emptyMap();
    }
}
 
Example 20
Source File: DefaultOperationDescriptionProvider.java    From wildfly-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Hook for subclasses to provide a description object for any complex "value-type" description of the operation reply.
 * <p>This default implementation throws an {@code IllegalStateException}; it is the responsibility of
 * subclasses to override this method if a complex "value-type" description is required.</p>
 *
 * @param descriptionResolver resolver for localizing any text in the description
 * @param locale              locale for any text description
 * @param bundle              resource bundle previously {@link ResourceDescriptionResolver#getResourceBundle(Locale) obtained from the description resolver}
 * @return a node describing the reply's "value-type"
 * @throws IllegalStateException if not overridden by an implementation that does not
 */
protected ModelNode getReplyValueTypeDescription(ResourceDescriptionResolver descriptionResolver, Locale locale, ResourceBundle bundle) {
    // bug -- user specifies a complex reply type but does not override this method to describe it
    return new ModelNode(ModelType.OBJECT); //todo rethink this
    //throw MESSAGES.operationReplyValueTypeRequired(operationName);
}