Search in sources :

Example 1 with DispatchFailure

use of org.jboss.hal.dmr.dispatch.DispatchFailure in project console by hal.

the class RrdTask method call.

@Override
public Completable call(LookupContext context) {
    boolean recursive = context.recursive;
    List<Completable> completables = new ArrayList<>();
    // create and partition non-optional operations
    List<Operation> operations = rrdOps.create(context, recursive, false);
    List<List<Operation>> piles = Lists.partition(operations, batchSize);
    List<Composite> composites = piles.stream().map(Composite::new).collect(toList());
    for (Composite composite : composites) {
        completables.add(dispatcher.execute(composite).doOnSuccess(parseRrdAction(context, composite)).toCompletable());
    }
    // create optional operations w/o partitioning!
    List<Operation> optionalOperations = rrdOps.create(context, recursive, true);
    // Do not refactor to
    // List<Composite> optionalComposites = optionalOperations.stream().map(Composite::new).collect(toList());
    // the GWT compiler will crash with an ArrayIndexOutOfBoundsException!
    List<Composite> optionalComposites = new ArrayList<>();
    optionalOperations.forEach(operation -> optionalComposites.add(new Composite(operation)));
    for (Composite composite : optionalComposites) {
        completables.add(dispatcher.execute(composite).onErrorResumeNext(throwable -> {
            if (throwable instanceof DispatchFailure) {
                logger.debug("Ignore errors on optional resource operation {}", composite.asCli());
                return Single.just(new CompositeResult(new ModelNode()));
            } else {
                return Single.error(throwable);
            }
        }).doOnSuccess(parseRrdAction(context, composite)).toCompletable());
    }
    if (!completables.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.debug("About to execute {} ({}+{}) composite operations (regular+optional)", composites.size() + optionalComposites.size(), composites.size(), optionalComposites.size());
            String compositeOps = composites.stream().map(Composite::asCli).collect(Collectors.joining(", "));
            logger.debug("Composite operations: {}", compositeOps);
            if (!optionalComposites.isEmpty()) {
                String optionalOps = optionalComposites.stream().map(Composite::asCli).collect(Collectors.joining(", "));
                logger.debug("Optional operations: {}", optionalOps);
            }
        }
        return Completable.concat(completables);
    } else {
        logger.debug("No DMR operations necessary");
        return Completable.complete();
    }
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) Completable(rx.Completable) Logger(org.slf4j.Logger) CompositeResult(org.jboss.hal.dmr.CompositeResult) Environment(org.jboss.hal.config.Environment) Settings(org.jboss.hal.config.Settings) Operation(org.jboss.hal.dmr.Operation) StatementContext(org.jboss.hal.meta.StatementContext) LoggerFactory(org.slf4j.LoggerFactory) DispatchFailure(org.jboss.hal.dmr.dispatch.DispatchFailure) Action1(rx.functions.Action1) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Composite(org.jboss.hal.dmr.Composite) List(java.util.List) Dispatcher(org.jboss.hal.dmr.dispatch.Dispatcher) Task(org.jboss.hal.flow.Task) Single(rx.Single) Collectors.toList(java.util.stream.Collectors.toList) Lists(com.google.common.collect.Lists) Completable(rx.Completable) Composite(org.jboss.hal.dmr.Composite) CompositeResult(org.jboss.hal.dmr.CompositeResult) ArrayList(java.util.ArrayList) Operation(org.jboss.hal.dmr.Operation) DispatchFailure(org.jboss.hal.dmr.dispatch.DispatchFailure) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ModelNode(org.jboss.hal.dmr.ModelNode)

Example 2 with DispatchFailure

use of org.jboss.hal.dmr.dispatch.DispatchFailure in project console by hal.

the class ReadAuthentication method call.

@Override
public Completable call(FlowContext context) {
    logger.debug("Read authentication");
    ResourceAddress address = CORE_SERVICE_TEMPLATE.resolve(statementContext);
    Operation opAuthorization = new Operation.Builder(address, READ_CHILDREN_RESOURCES_OPERATION).param(CHILD_TYPE, ACCESS).param(INCLUDE_RUNTIME, true).param(RECURSIVE_DEPTH, 1).build();
    Operation opWhoami = new Operation.Builder(ResourceAddress.root(), WHOAMI).param(VERBOSE, true).build();
    return dispatcher.execute(new Composite(opAuthorization, opWhoami)).doOnSuccess((CompositeResult compositeResult) -> {
        ModelNode result = compositeResult.step(0).get(RESULT);
        if (result.hasDefined(AUTHORIZATION)) {
            result = result.get(AUTHORIZATION);
            // provider
            AccessControlProvider accessControlProvider = asEnumValue(result, PROVIDER, AccessControlProvider::valueOf, SIMPLE);
            environment.setAccessControlProvider(accessControlProvider);
            // standard roles
            if (result.hasDefined(STANDARD_ROLE_NAMES)) {
                result.get(STANDARD_ROLE_NAMES).asList().stream().map(node -> new Role(node.asString())).forEach(role -> environment.getRoles().add(role));
            }
            // scoped roles
            if (!environment.isStandalone()) {
                if (result.hasDefined(HOST_SCOPED_ROLE)) {
                    result.get(HOST_SCOPED_ROLE).asPropertyList().stream().map(property -> scopedRole(property, Role.Type.HOST, HOSTS)).forEach(role -> environment.getRoles().add(role));
                }
                if (result.hasDefined(SERVER_GROUP_SCOPED_ROLE)) {
                    result.get(SERVER_GROUP_SCOPED_ROLE).asPropertyList().stream().map(property -> scopedRole(property, Role.Type.SERVER_GROUP, SERVER_GROUPS)).forEach(role -> environment.getRoles().add(role));
                }
            }
        } else {
            logger.warn("Unable to read {} (insufficient rights?). Use :whoami values as fallback.", CORE_SERVICE_TEMPLATE.append("access=authorization"));
            ModelNode resultWhoami = compositeResult.step(1).get(RESULT);
            environment.setAccessControlProvider(RBAC);
            environment.getRoles().clear();
            if (resultWhoami.hasDefined(ROLES)) {
                resultWhoami.get(ROLES).asList().stream().map(node -> new Role(node.asString())).forEach(role -> environment.getRoles().add(role));
            } else if (resultWhoami.hasDefined(MAPPED_ROLES)) {
                resultWhoami.get(MAPPED_ROLES).asList().stream().map(node -> new Role(node.asString())).forEach(role -> environment.getRoles().add(role));
            }
        }
    }).onErrorResumeNext(throwable -> {
        if (throwable instanceof DispatchFailure) {
            logger.error("Unable to read {}. Use :whoami values as fallback.", CORE_SERVICE_TEMPLATE);
            return Single.just(new CompositeResult(new ModelNode()));
        } else {
            return Single.error(throwable);
        }
    }).toCompletable();
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) Completable(rx.Completable) Environment(org.jboss.hal.config.Environment) StatementContext(org.jboss.hal.meta.StatementContext) AccessControlProvider(org.jboss.hal.config.AccessControlProvider) LoggerFactory(org.slf4j.LoggerFactory) DispatchFailure(org.jboss.hal.dmr.dispatch.DispatchFailure) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) FlowContext(org.jboss.hal.flow.FlowContext) ModelNodeHelper.asEnumValue(org.jboss.hal.dmr.ModelNodeHelper.asEnumValue) Inject(javax.inject.Inject) Property(org.jboss.hal.dmr.Property) Single(rx.Single) AddressTemplate(org.jboss.hal.meta.AddressTemplate) SIMPLE(org.jboss.hal.config.AccessControlProvider.SIMPLE) Collectors.toSet(java.util.stream.Collectors.toSet) Logger(org.slf4j.Logger) RBAC(org.jboss.hal.config.AccessControlProvider.RBAC) CompositeResult(org.jboss.hal.dmr.CompositeResult) Operation(org.jboss.hal.dmr.Operation) Ids(org.jboss.hal.resources.Ids) Set(java.util.Set) Composite(org.jboss.hal.dmr.Composite) Dispatcher(org.jboss.hal.dmr.dispatch.Dispatcher) ModelDescriptionConstants(org.jboss.hal.dmr.ModelDescriptionConstants) Role(org.jboss.hal.config.Role) Collections(java.util.Collections) Role(org.jboss.hal.config.Role) Composite(org.jboss.hal.dmr.Composite) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) CompositeResult(org.jboss.hal.dmr.CompositeResult) AccessControlProvider(org.jboss.hal.config.AccessControlProvider) DispatchFailure(org.jboss.hal.dmr.dispatch.DispatchFailure) Operation(org.jboss.hal.dmr.Operation) ModelNode(org.jboss.hal.dmr.ModelNode)

Aggregations

Environment (org.jboss.hal.config.Environment)2 Composite (org.jboss.hal.dmr.Composite)2 CompositeResult (org.jboss.hal.dmr.CompositeResult)2 ModelNode (org.jboss.hal.dmr.ModelNode)2 Operation (org.jboss.hal.dmr.Operation)2 DispatchFailure (org.jboss.hal.dmr.dispatch.DispatchFailure)2 Dispatcher (org.jboss.hal.dmr.dispatch.Dispatcher)2 StatementContext (org.jboss.hal.meta.StatementContext)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Completable (rx.Completable)2 Single (rx.Single)2 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1