Search in sources :

Example 21 with Property

use of org.jboss.hal.dmr.Property in project console by hal.

the class ReadChildrenProcessor method results.

protected List<ReadChildrenResult> results(List<ResourceAddress> addresses) {
    List<ReadChildrenResult> results = new ArrayList<>();
    if (!addresses.isEmpty()) {
        // it's expected that all addresses are of the same type (same length of segments)
        Iterator<ResourceAddress> iterator = addresses.iterator();
        int length = iterator.next().asList().size();
        while (iterator.hasNext()) {
            if (iterator.next().asList().size() != length) {
                logger.error("Different address types in result processor");
                return Collections.emptyList();
            }
        }
        // turn the addresses into a list of models
        for (ResourceAddress address : addresses) {
            ReadChildrenResult result = new ReadChildrenResult(address.lastValue());
            for (Property property : address.getParent().asPropertyList()) {
                result.addresses.put(property.getName(), property.getValue().asString());
            }
            results.add(result);
        }
    }
    return results;
}
Also used : ResourceAddress(org.jboss.hal.dmr.ResourceAddress) ArrayList(java.util.ArrayList) Property(org.jboss.hal.dmr.Property)

Example 22 with Property

use of org.jboss.hal.dmr.Property in project console by hal.

the class ModelBrowser method unflattenModel.

private void unflattenModel(ModelNode model) {
    if (!model.isDefined()) {
        return;
    }
    for (Property p : model.asPropertyList()) {
        if (p.getName().indexOf('.') < 0) {
            continue;
        }
        String[] split = p.getName().split("\\.");
        model.remove(p.getName());
        model.get(split[0]).get(split[1]).set(p.getValue());
    }
}
Also used : Property(org.jboss.hal.dmr.Property)

Example 23 with Property

use of org.jboss.hal.dmr.Property in project console by hal.

the class DefaultFormItemProvider method createFrom.

@Override
public FormItem<?> createFrom(Property property) {
    FormItem<?> formItem = null;
    String name = property.getName();
    String label = labelBuilder.label(property);
    ModelNode attributeDescription = property.getValue();
    // don't use 'required' here!
    boolean required = attributeDescription.hasDefined(NILLABLE) && !attributeDescription.get(NILLABLE).asBoolean();
    boolean expressionAllowed = attributeDescription.hasDefined(EXPRESSIONS_ALLOWED) && attributeDescription.get(EXPRESSIONS_ALLOWED).asBoolean();
    boolean readOnly = attributeDescription.hasDefined(ACCESS_TYPE) && (READ_ONLY.equals(attributeDescription.get(ACCESS_TYPE).asString()) || METRIC.equals(attributeDescription.get(ACCESS_TYPE).asString()));
    String unit = attributeDescription.hasDefined(UNIT) ? attributeDescription.get(UNIT).asString() : null;
    Deprecation deprecation = attributeDescription.hasDefined(DEPRECATED) ? new Deprecation(attributeDescription.get(DEPRECATED)) : null;
    if (attributeDescription.hasDefined(TYPE)) {
        ModelType type = attributeDescription.get(TYPE).asType();
        ModelType valueType = (attributeDescription.has(VALUE_TYPE) && attributeDescription.get(VALUE_TYPE).getType() != ModelType.OBJECT) ? ModelType.valueOf(attributeDescription.get(VALUE_TYPE).asString()) : null;
        switch(type) {
            case BOOLEAN:
                {
                    SwitchItem switchItem = new SwitchItem(name, label);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        switchItem.assignDefaultValue(attributeDescription.get(DEFAULT).asBoolean());
                    }
                    formItem = switchItem;
                    break;
                }
            case BIG_INTEGER:
            case INT:
            case LONG:
                {
                    long min, max;
                    if (type == ModelType.INT) {
                        min = attributeDescription.get(MIN).asLong(Integer.MIN_VALUE);
                        max = attributeDescription.get(MAX).asLong(Integer.MAX_VALUE);
                    } else {
                        min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                        max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    }
                    NumberItem numberItem = new NumberItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        long defaultValue = attributeDescription.get(DEFAULT).asLong();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case DOUBLE:
                {
                    long min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                    long max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    NumberDoubleItem numberItem = new NumberDoubleItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        double defaultValue = attributeDescription.get(DEFAULT).asDouble();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case LIST:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                        if (!allowedValues.isEmpty()) {
                            MultiSelectBoxItem multiSelectBoxItem = new MultiSelectBoxItem(name, label, allowedValues);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    multiSelectBoxItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = multiSelectBoxItem;
                        } else {
                            ListItem listItem = new ListItem(name, label);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    listItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = listItem;
                            checkCapabilityReference(attributeDescription, formItem);
                        }
                    } else if (isSimpleTuple(attributeDescription)) {
                        // process OBJECT type attribute if all of its subattributes are simple types
                        formItem = new TuplesListItem(name, label, metadata.forComplexAttribute(property.getName()));
                    } else {
                        logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                        break;
                    }
                    break;
                }
            case OBJECT:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        PropertiesItem propertiesItem = new PropertiesItem(name, label);
                        List<Property> properties = ModelNodeHelper.getOrDefault(attributeDescription, DEFAULT, () -> attributeDescription.get(DEFAULT).asPropertyList(), emptyList());
                        if (!properties.isEmpty()) {
                            Map<String, String> defaultValues = new HashMap<>();
                            for (Property p : properties) {
                                defaultValues.put(p.getName(), p.getValue().asString());
                            }
                            propertiesItem.assignDefaultValue(defaultValues);
                        }
                        formItem = propertiesItem;
                    }
                    break;
                }
            case STRING:
                {
                    List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                    if (allowedValues.isEmpty()) {
                        FormItem<String> textBoxItem = new TextBoxItem(name, label, null);
                        boolean sensitive = failSafeGet(attributeDescription, ACCESS_CONSTRAINTS + "/" + SENSITIVE).isDefined();
                        if (PASSWORD.equals(name) || sensitive) {
                            textBoxItem.mask();
                        }
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            textBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = textBoxItem;
                        checkCapabilityReference(attributeDescription, formItem);
                    } else {
                        SingleSelectBoxItem singleSelectBoxItem = new SingleSelectBoxItem(name, label, allowedValues, !required);
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            singleSelectBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = singleSelectBoxItem;
                    }
                    break;
                }
            // unsupported types
            case BIG_DECIMAL:
            case BYTES:
            case EXPRESSION:
            case PROPERTY:
            case TYPE:
            case UNDEFINED:
                logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                break;
            default:
                break;
        }
        if (formItem != null) {
            formItem.setRequired(required);
            formItem.setDeprecated(deprecation);
            if (formItem.supportsExpressions()) {
                formItem.setExpressionAllowed(expressionAllowed);
                formItem.addResolveExpressionHandler(event -> {
                    // resend as application event
                    Core.INSTANCE.eventBus().fireEvent(event);
                });
            }
            if (readOnly) {
                formItem.setEnabled(false);
                // if the attribute is read-only and required, the form validation prevents to save the form
                // remove the required constraint to allow the save operation.
                formItem.setRequired(false);
            }
        }
    }
    return formItem;
}
Also used : TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) SingleSelectBoxItem(org.jboss.hal.ballroom.form.SingleSelectBoxItem) FormItem(org.jboss.hal.ballroom.form.FormItem) NumberItem(org.jboss.hal.ballroom.form.NumberItem) MultiSelectBoxItem(org.jboss.hal.ballroom.form.MultiSelectBoxItem) PropertiesItem(org.jboss.hal.ballroom.form.PropertiesItem) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) Deprecation(org.jboss.hal.dmr.Deprecation) ModelType(org.jboss.hal.dmr.ModelType) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NumberDoubleItem(org.jboss.hal.ballroom.form.NumberDoubleItem) ListItem(org.jboss.hal.ballroom.form.ListItem) TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) ModelNode(org.jboss.hal.dmr.ModelNode) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.jboss.hal.dmr.Property) SwitchItem(org.jboss.hal.ballroom.form.SwitchItem)

Example 24 with Property

use of org.jboss.hal.dmr.Property in project console by hal.

the class ModelNodeMapping method populateFormItem.

@SuppressWarnings("unchecked")
public void populateFormItem(String id, String name, ModelNode attributeDescription, ModelNode value, FormItem formItem) {
    ModelType descriptionType = attributeDescription.get(TYPE).asType();
    try {
        switch(descriptionType) {
            case BOOLEAN:
                formItem.setValue(value.asBoolean());
                break;
            case INT:
                // NumberItem uses *always* long
                try {
                    formItem.setValue((long) value.asInt());
                } catch (IllegalArgumentException e) {
                    logger.error("{}: Unable to populate form item '{}': Metadata says it's an INT, but value is not '{}'", id, name, value.asString());
                }
                break;
            case DOUBLE:
                try {
                    formItem.setValue(value.asDouble());
                } catch (IllegalArgumentException e) {
                    logger.error("{}: Unable to populate form item '{}': Metadata says it's an DOUBLE, but value is not '{}'", id, name, value.asString());
                }
                break;
            case BIG_INTEGER:
            case LONG:
                try {
                    formItem.setValue(value.asLong());
                } catch (IllegalArgumentException e) {
                    logger.error("{}: Unable to populate form item '{}': Metadata says it's a {}, but value is not '{}'", id, name, descriptionType.name(), value.asString());
                }
                break;
            case LIST:
                List<String> list = value.asList().stream().map(ModelNode::asString).collect(toList());
                formItem.setValue(list);
                break;
            case OBJECT:
                boolean stringValueType = attributeDescription.get(VALUE_TYPE).getType().equals(ModelType.TYPE) && attributeDescription.get(VALUE_TYPE).asType().equals(ModelType.STRING);
                if (stringValueType) {
                    List<Property> properties = value.asPropertyList();
                    Map<String, String> map = new HashMap<>();
                    for (Property property : properties) {
                        map.put(property.getName(), property.getValue().asString());
                    }
                    formItem.setValue(map);
                } else {
                    formItem.setValue(value);
                }
                break;
            case STRING:
                formItem.setValue(value.asString());
                break;
            // unsupported types
            case BIG_DECIMAL:
            case BYTES:
            case EXPRESSION:
            case PROPERTY:
            case TYPE:
            case UNDEFINED:
                logger.warn("{}: populating form field '{}' of type '{}' not implemented", id, name, descriptionType);
                break;
            default:
                break;
        }
    } catch (IllegalArgumentException e) {
        logger.error("{}: Unable to populate form item '{}'. Declared type in r-r-d does not match type in model node: '{}' != '{}'", id, name, descriptionType.name(), value.getType());
        formItem.setEnabled(false);
    }
}
Also used : HashMap(java.util.HashMap) ModelType(org.jboss.hal.dmr.ModelType) Property(org.jboss.hal.dmr.Property)

Example 25 with Property

use of org.jboss.hal.dmr.Property in project console by hal.

the class PatchesColumn method checkHostState.

/**
 * Checks if the host or standalone server is in restart mode, if yes then asks user to restart host/server, as it must be
 * restarted before a patch can be installed or to call a rollback on an installed patch.
 */
private void checkHostState(Callback callback) {
    Messages messages = resources.messages();
    if (environment.isStandalone()) {
        Operation operation = new Operation.Builder(ResourceAddress.root(), READ_RESOURCE_OPERATION).param(INCLUDE_RUNTIME, true).param(ATTRIBUTES_ONLY, true).build();
        dispatcher.execute(operation, result -> {
            Server.STANDALONE.addServerAttributes(result);
            if (Server.STANDALONE.needsRestart()) {
                serverActions.restartStandalone(Server.STANDALONE, messages.patchRestartStandaloneQuestion());
            } else {
                callback.execute();
            }
        });
    } else {
        ResourceAddress address = new ResourceAddress().add(HOST, statementContext.selectedHost());
        Operation operation = new Operation.Builder(address, READ_RESOURCE_OPERATION).param(INCLUDE_RUNTIME, true).param(ATTRIBUTES_ONLY, true).build();
        dispatcher.execute(operation, result -> {
            Property prop = new Property(statementContext.selectedHost(), result);
            Host host = new Host(prop);
            if (host.needsRestart()) {
                SafeHtml question = host.isDomainController() ? messages.patchRestartDomainControllerQuestion(host.getName()) : messages.patchRestartHostControllerQuestion(host.getName());
                hostActions.restart(host, question);
            } else {
                callback.execute();
            }
        });
    }
}
Also used : Messages(org.jboss.hal.resources.Messages) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) SafeHtml(com.google.gwt.safehtml.shared.SafeHtml) Host(org.jboss.hal.core.runtime.host.Host) Operation(org.jboss.hal.dmr.Operation) Property(org.jboss.hal.dmr.Property)

Aggregations

Property (org.jboss.hal.dmr.Property)39 ModelNode (org.jboss.hal.dmr.ModelNode)26 Operation (org.jboss.hal.dmr.Operation)15 ResourceAddress (org.jboss.hal.dmr.ResourceAddress)15 Ids (org.jboss.hal.resources.Ids)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Inject (javax.inject.Inject)6 Composite (org.jboss.hal.dmr.Composite)6 ModelType (org.jboss.hal.dmr.ModelType)6 Metadata (org.jboss.hal.meta.Metadata)6 Resources (org.jboss.hal.resources.Resources)6 Map (java.util.Map)5 Form (org.jboss.hal.ballroom.form.Form)5 EventBus (com.google.web.bindery.event.shared.EventBus)4 HashMap (java.util.HashMap)4 Collectors.toList (java.util.stream.Collectors.toList)4 Collectors.toMap (java.util.stream.Collectors.toMap)4 PropertiesItem (org.jboss.hal.ballroom.form.PropertiesItem)4 Environment (org.jboss.hal.config.Environment)4