Java Code Examples for org.jboss.as.controller.registry.Resource#writeModel()

The following examples show how to use org.jboss.as.controller.registry.Resource#writeModel() . 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: FindNonProgressingOpUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Resource getHCResource(boolean domainRollout) {
    Resource host = Resource.Factory.create(true);
    ModelNode model = new ModelNode();
    model.get(EXCLUSIVE_RUNNING_TIME).set(100);
    model.get(DOMAIN_ROLLOUT).set(domainRollout);
    model.get(EXECUTION_STATUS).set(OperationContext.ExecutionStatus.COMPLETING.toString());
    model.get(DOMAIN_UUID).set("uuid");
    host.writeModel(model);
    return host;
}
 
Example 2
Source File: FindNonProgressingOpUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Resource getServerResource(long runningTime) {
    Resource server = Resource.Factory.create(true);
    ModelNode model = new ModelNode();
    model.get(RUNNING_TIME).set(runningTime);
    model.get(DOMAIN_ROLLOUT).set(true);
    model.get(EXECUTION_STATUS).set(OperationContext.ExecutionStatus.EXECUTING.toString());
    model.get(DOMAIN_UUID).set("uuid");
    server.writeModel(model);
    return server;
}
 
Example 3
Source File: ResourceTransformationContextImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ResourceTransformationContext addTransformedResourceFromRoot(PathAddress absoluteAddress, PathAddress read, Resource toAdd) {
    // Only keep the mode, drop all children
    final Resource copy;
    if (toAdd != null) {
        copy = Resource.Factory.create(false, toAdd.getOrderedChildTypes());
        copy.writeModel(toAdd.getModel());
    } else {
        copy = Resource.Factory.create();
    }
    return addTransformedRecursiveResourceFromRoot(absoluteAddress, read, copy);
}
 
Example 4
Source File: ManagedDMRContentStoreHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    final Resource resource = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS);
    final byte[] oldHash = ManagedDMRContentResourceDefinition.HASH.validateOperation(operation).asBytes();
    final byte[] currentHash = resource.getModel().get(ManagedDMRContentResourceDefinition.HASH.getName()).asBytes();
    if (!Arrays.equals(oldHash, currentHash)) {
        throw ManagedDMRContentLogger.ROOT_LOGGER.invalidHash(HashUtil.bytesToHexString(oldHash), PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)), HashUtil.bytesToHexString(currentHash));
    }
    ModelNode model = new ModelNode();
    contentAttribute.validateAndSet(operation, model);

    // IMPORTANT: Use writeModel, as this is what causes the content to be flushed to the content repo!
    resource.writeModel(model);
}
 
Example 5
Source File: ManagedDMRContentWriteAttributeHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    ModelNode model = new ModelNode();
    ModelNode synthOp = new ModelNode();
    synthOp.get(contentAttribute.getName()).set(operation.get(ModelDescriptionConstants.VALUE));
    contentAttribute.validateAndSet(synthOp, model);

    final Resource resource = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS);
    // IMPORTANT: Use writeModel, as this is what causes the content to be flushed to the content repo!
    resource.writeModel(model);
}
 
Example 6
Source File: HostServerJvmModelTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void populateModel(Resource rootResource) {
    Resource host = Resource.Factory.create();
    rootResource.registerChild(HOST_ELEMENT, host);
    ModelNode serverConfig = new ModelNode();
    serverConfig.get(GROUP).set("test");
    Resource server1 = Resource.Factory.create();
    server1.writeModel(serverConfig);
    Resource server2 = Resource.Factory.create();
    server2.writeModel(serverConfig);
    host.registerChild(SERVER_ONE_ELEMENT, server1);
    host.registerChild(SERVER_TWO_ELEMENT, server2);
}
 
Example 7
Source File: HostServerSystemPropertyTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void populateModel(Resource rootResource) {
    Resource host = Resource.Factory.create();
    rootResource.registerChild(HOST_ELEMENT, host);
    ModelNode serverConfig = new ModelNode();
    serverConfig.get(GROUP).set("test");
    Resource server1 = Resource.Factory.create();
    server1.writeModel(serverConfig);
    Resource server2 = Resource.Factory.create();
    server2.writeModel(serverConfig);
    host.registerChild(SERVER_ONE_ELEMENT, server1);
    host.registerChild(SERVER_TWO_ELEMENT, server2);
}
 
Example 8
Source File: HostServerSpecifiedPathsTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected ModelInitializer createEmptyModelInitalizer() {
    return new ModelInitializer() {
        @Override
        public void populateModel(Resource rootResource) {
            Resource host = Resource.Factory.create();
            rootResource.registerChild(PARENT.getLastElement(), host);
            ModelNode serverConfig = new ModelNode();
            serverConfig.get(GROUP).set("test");
            Resource server1 = Resource.Factory.create();
            server1.writeModel(serverConfig);
            host.registerChild(getPathsParent().getLastElement(), server1);
        }
    };
}
 
Example 9
Source File: SyncModelOperationHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
        // In case we want to automatically ignore extensions we would need to add them before describing the operations
        // This is also required for resolving the corresponding OperationStepHandler here
//        final ManagementResourceRegistration registration = context.getResourceRegistrationForUpdate();
//        for (String extension : missingExtensions) {
//            final PathElement element = PathElement.pathElement(EXTENSION, extension);
//            if (ignoredResourceRegistry.isResourceExcluded(PathAddress.pathAddress(element))) {
//                continue;
//            }
//            context.addResource(PathAddress.pathAddress(element), new ExtensionResource(extension, extensionRegistry));
//            initializeExtension(extension, registration);
//        }
        // There should be no missing extensions for now, unless they are manually ignored
        if (!missingExtensions.isEmpty()) {
            throw DomainControllerLogger.HOST_CONTROLLER_LOGGER.missingExtensions(missingExtensions);
        }

        final ModelNode readOp = new ModelNode();
        readOp.get(OP).set(ReadMasterDomainOperationsHandler.OPERATION_NAME);
        readOp.get(OP_ADDR).setEmptyList();

        // Describe the operations based on the remote model
        final ReadMasterDomainOperationsHandler readOperationsHandler = new ReadMasterDomainOperationsHandler();
        final HostControllerRegistrationHandler.OperationExecutor operationExecutor = parameters.getOperationExecutor();
        final ModelNode result = operationExecutor.executeReadOnly(readOp, remoteModel, readOperationsHandler, ModelController.OperationTransactionControl.COMMIT);
        if (result.hasDefined(FAILURE_DESCRIPTION)) {
            context.getFailureDescription().set(result.get(FAILURE_DESCRIPTION));
            return;
        }

        final List<ModelNode> remoteOperations = result.get(RESULT).asList();

        // Create the node models based on the operations
        final Node currentRoot = new Node(null, PathAddress.EMPTY_ADDRESS);
        final Node remoteRoot = new Node(null, PathAddress.EMPTY_ADDRESS);

        // Process the local and remote operations
        process(currentRoot, localOperations, localOrderedChildTypes);
        process(remoteRoot, remoteOperations, readOperationsHandler.getOrderedChildTypes());

        // Compare the nodes and create the operations to sync the model
        //final List<ModelNode> operations = new ArrayList<>();
        OrderedOperationsCollection operations = new OrderedOperationsCollection(context);
        processAttributes(currentRoot, remoteRoot, operations, context.getRootResourceRegistration());
        processChildren(currentRoot, remoteRoot, operations, context.getRootResourceRegistration());

        //Process root domain attributes manually as those are read-only
        if(context.getCurrentAddress().size() == 0) {
            Resource rootResource = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS);
            ModelNode rootModel = rootResource.getModel().clone();
            ModelNode remoteRootModel = remoteModel.getModel();
            for (String attributeName : ROOT_ATTRIBUTES) {
                if ((remoteRootModel.hasDefined(attributeName))) {
                    rootModel.get(attributeName).set(remoteRootModel.get(attributeName));
                }
            }
            rootResource.writeModel(rootModel);
        }

        // Reverse, since we are adding the steps on top of the queue
        final List<ModelNode> ops = operations.getReverseList();

        for (final ModelNode op : ops) {

            final String operationName = op.require(OP).asString();
            final PathAddress address = PathAddress.pathAddress(op.require(OP_ADDR));
            if (parameters.getIgnoredResourceRegistry().isResourceExcluded(address)) {
                continue;
            }
            // Ignore all extension:add operations, since we've added them before
//            if (address.size() == 1 && EXTENSION.equals(address.getElement(0).getKey()) && ADD.equals(operationName)) {
//                continue;
//            }

            final ImmutableManagementResourceRegistration rootRegistration = context.getRootResourceRegistration();
            final OperationStepHandler stepHandler = rootRegistration.getOperationHandler(address, operationName);
            if(stepHandler != null) {
                context.addStep(op, stepHandler, OperationContext.Stage.MODEL, true);
            } else {
                final ImmutableManagementResourceRegistration child = rootRegistration.getSubModel(address);
                if (child == null) {
                    context.getFailureDescription().set(ControllerLogger.ROOT_LOGGER.noSuchResourceType(address));
                } else {
                    context.getFailureDescription().set(ControllerLogger.ROOT_LOGGER.noHandlerForOperation(operationName, address));
                }
            }

        }

        if (!context.isBooting() && operations.getAllOps().size() > 0 && parameters.isFullModelTransfer()) {
            //Only do this is if it is a full model transfer as a result of a _reconnect_ to the DC.
            //When fetching missing configuration while connected, the servers will get put into reload-required as a
            // result of changing the server-group, profile or the socket-binding-group
            context.addStep(new SyncServerStateOperationHandler(parameters, operations.getAllOps()),
                    OperationContext.Stage.MODEL,
                    true);
        }
    }
 
Example 10
Source File: SyncModelServerStateTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Resource initModel(final ManagementModel managementModel) {
    this.managementModel = managementModel;
    ManagementResourceRegistration rootRegistration = managementModel.getRootResourceRegistration();
    //Use the saved hostResourceDefinition
    hostRegistration = rootRegistration.registerSubModel(hostResourceDefinition);

    rootRegistration.registerOperationHandler(TRIGGER_SYNC, new TriggerSyncHandler());
    GlobalOperationHandlers.registerGlobalOperations(rootRegistration, processType);
    rootRegistration.registerOperationHandler(CompositeOperationHandler.DEFINITION, CompositeOperationHandler.INSTANCE);


    Resource rootResource = managementModel.getRootResource();
    CoreManagementResourceDefinition.registerDomainResource(rootResource, null);

    final Resource host = Resource.Factory.create();
    final Resource serverOneConfig = Resource.Factory.create();
    final ModelNode serverOneModel = new ModelNode();
    serverOneModel.get(GROUP).set("group-one");
    serverOneModel.get(SOCKET_BINDING_GROUP).set("binding-one");
    serverOneConfig.writeModel(serverOneModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-one"), serverOneConfig);

    final Resource serverTwoConfig = Resource.Factory.create();
    final ModelNode serverTwoModel = new ModelNode();
    serverTwoModel.get(GROUP).set("group-one");
    serverTwoConfig.writeModel(serverTwoModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-two"), serverTwoConfig);

    final Resource serverThreeConfig = Resource.Factory.create();
    final ModelNode serverThreeModel = new ModelNode();
    serverThreeModel.get(GROUP).set("group-two");
    serverThreeConfig.writeModel(serverThreeModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-three"), serverThreeConfig);

    rootResource.registerChild(PathElement.pathElement(HOST, hostName), host);
    final Resource serverGroup1 = Resource.Factory.create();
    serverGroup1.getModel().get(PROFILE).set("profile-one");
    serverGroup1.getModel().get(SOCKET_BINDING_GROUP).set("binding-one");
    rootResource.registerChild(PathElement.pathElement(SERVER_GROUP, "group-one"), serverGroup1);

    final Resource serverGroup2 = Resource.Factory.create();
    serverGroup2.getModel().get(PROFILE).set("profile-two");
    serverGroup2.getModel().get(SOCKET_BINDING_GROUP).set("binding-two");
    rootResource.registerChild(PathElement.pathElement(SERVER_GROUP, "group-two"), serverGroup2);

    // group three has no servers assigned
    final Resource serverGroup3 = Resource.Factory.create();
    serverGroup3.getModel().get(PROFILE).set("profile-three");
    serverGroup3.getModel().get(SOCKET_BINDING_GROUP).set("binding-three");
    rootResource.registerChild(PathElement.pathElement(SERVER_GROUP, "group-three"), serverGroup3);

    final Resource profile1 = Resource.Factory.create();
    profile1.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(PROFILE, "profile-one"), profile1);
    final Resource profile2 = Resource.Factory.create();
    profile2.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(PROFILE, "profile-two"), profile2);
    final Resource profile3 = Resource.Factory.create();
    profile3.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(PROFILE, "profile-three"), profile3);

    final Resource binding1 = Resource.Factory.create();
    binding1.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(SOCKET_BINDING_GROUP, "binding-one"), binding1);
    final Resource binding2 = Resource.Factory.create();
    binding2.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(SOCKET_BINDING_GROUP, "binding-two"), binding2);
    final Resource binding3 = Resource.Factory.create();
    binding3.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(SOCKET_BINDING_GROUP, "binding-three"), binding3);

    registerServer("server-one");
    registerServer("server-two");
    registerServer("server-three");
    return rootResource;
}
 
Example 11
Source File: AbstractOperationTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
static Resource createRootResource() {
    final Resource rootResource = Resource.Factory.create();

    CoreManagementResourceDefinition.registerDomainResource(rootResource, null);

    final Resource host = Resource.Factory.create();
    final Resource serverOneConfig = Resource.Factory.create();
    final ModelNode serverOneModel = new ModelNode();
    serverOneModel.get(GROUP).set("group-one");
    serverOneModel.get(SOCKET_BINDING_GROUP).set("binding-one");
    serverOneConfig.writeModel(serverOneModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-one"), serverOneConfig);

    final Resource serverTwoConfig = Resource.Factory.create();
    final ModelNode serverTwoModel = new ModelNode();
    serverTwoModel.get(GROUP).set("group-one");
    serverTwoConfig.writeModel(serverTwoModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-two"), serverTwoConfig);

    final Resource serverThreeConfig = Resource.Factory.create();
    final ModelNode serverThreeModel = new ModelNode();
    serverThreeModel.get(GROUP).set("group-two");
    serverThreeConfig.writeModel(serverThreeModel);
    host.registerChild(PathElement.pathElement(SERVER_CONFIG, "server-three"), serverThreeConfig);

    rootResource.registerChild(PathElement.pathElement(HOST, "localhost"), host);

    final Resource serverGroup1 = Resource.Factory.create();
    serverGroup1.getModel().get(PROFILE).set("profile-one");
    serverGroup1.getModel().get(SOCKET_BINDING_GROUP).set("binding-one");
    rootResource.registerChild(PathElement.pathElement(SERVER_GROUP, "group-one"), serverGroup1);

    final Resource serverGroup2 = Resource.Factory.create();
    serverGroup2.getModel().get(PROFILE).set("profile-two");
    serverGroup2.getModel().get(SOCKET_BINDING_GROUP).set("binding-two");
    rootResource.registerChild(PathElement.pathElement(SERVER_GROUP, "group-two"), serverGroup2);

    final Resource profile1 = Resource.Factory.create();
    profile1.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(PROFILE, "profile-one"), profile1);
    final Resource profile2 = Resource.Factory.create();
    profile2.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(PROFILE, "profile-two"), profile2);

    final Resource binding1 = Resource.Factory.create();
    binding1.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(SOCKET_BINDING_GROUP, "binding-one"), binding1);
    final Resource binding2 = Resource.Factory.create();
    binding2.getModel().setEmptyObject();
    rootResource.registerChild(PathElement.pathElement(SOCKET_BINDING_GROUP, "binding-two"), binding2);

    final Resource management = rootResource.getChild(PathElement.pathElement(CORE_SERVICE, MANAGEMENT));
    final Resource accessControl = management.getChild(AccessAuthorizationResourceDefinition.PATH_ELEMENT);

    final Resource superUser = Resource.Factory.create();
    accessControl.registerChild(PathElement.pathElement(ROLE_MAPPING, "SuperUser"), superUser);
    final Resource include = Resource.Factory.create();
    superUser.registerChild(PathElement.pathElement(INCLUDE, "user-$local"), include);
    include.getModel().get("name").set("local");

    hack(rootResource, EXTENSION);
    hack(rootResource, PATH);
    hack(rootResource, SYSTEM_PROPERTY);
    hack(rootResource, INTERFACE);
    hack(rootResource, DEPLOYMENT);
    return rootResource;
}
 
Example 12
Source File: ResourceTransformationContextImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private ResourceTransformationContext addTransformedRecursiveResourceFromRoot(final PathAddress absoluteAddress, final PathAddress read, final Resource toAdd) {
    Resource model = this.root;
    Resource parent = null;
    if (absoluteAddress.size() > 0) {
        final Iterator<PathElement> i = absoluteAddress.iterator();
        while (i.hasNext()) {
            final PathElement element = i.next();
            if (element.isMultiTarget()) {
                throw ControllerLogger.ROOT_LOGGER.cannotWriteTo("*");
            }
            if (!i.hasNext()) {
                if (model.hasChild(element)) {
                    throw ControllerLogger.ROOT_LOGGER.duplicateResourceAddress(absoluteAddress);
                } else {
                    parent = model;
                    model.registerChild(element, toAdd);
                    model = toAdd;
                    if (read.size() > 0) {
                        //We might be able to deal with this better in the future, but for now
                        //throw an error if the address was renamed and it was an ordered child type.
                        Set<String> parentOrderedChildren = parent.getOrderedChildTypes();
                        String readType = read.getLastElement().getKey();
                        if (parentOrderedChildren.contains(readType)) {
                            if (absoluteAddress.size() == 0 || !absoluteAddress.getLastElement().getKey().equals(readType)) {
                                throw ControllerLogger.ROOT_LOGGER.orderedChildTypeRenamed(read, absoluteAddress, readType, parentOrderedChildren);
                            }
                        }
                    }
                }
            } else {
                model = model.getChild(element);
                if (model == null) {
                    PathAddress ancestor = PathAddress.EMPTY_ADDRESS;
                    for (PathElement pe : absoluteAddress) {
                        ancestor = ancestor.append(pe);
                        if (element.equals(pe)) {
                            break;
                        }
                    }
                    throw ControllerLogger.ROOT_LOGGER.resourceNotFound(ancestor, absoluteAddress);
                }
            }
        }
    } else {
        //If this was the root address, replace the resource model
        model.writeModel(toAdd.getModel());
    }
    return new ResourceTransformationContextImpl(root, absoluteAddress, read, originalModel,
            transformerOperationAttachment, ignoredTransformationRegistry);
}
 
Example 13
Source File: TransformationUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static Resource modelToResource(final PathAddress startAddress, final ImmutableManagementResourceRegistration reg, final ModelNode model, boolean includeUndefined, PathAddress fullPath) {
    Resource res = Resource.Factory.create();
    ModelNode value = new ModelNode();
    Set<String> allFields = new HashSet<String>(model.keys());
    for (String name : reg.getAttributeNames(PathAddress.EMPTY_ADDRESS)) {
        AttributeAccess aa = reg.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name);
        if (aa.getStorageType() == AttributeAccess.Storage.RUNTIME){
            allFields.remove(name);
            continue;
        }

        if (includeUndefined) {
            value.get(name).set(model.get(name));
        } else {
            if (model.hasDefined(name)) {
                value.get(name).set(model.get(name));
            }
        }
        allFields.remove(name);
    }
    if (!value.isDefined() && model.isDefined() && reg.getChildAddresses(PathAddress.EMPTY_ADDRESS).size() == 0) {
        value.setEmptyObject();
    }
    res.writeModel(value);

    for (String childType : reg.getChildNames(PathAddress.EMPTY_ADDRESS)) {
        if (model.hasDefined(childType)) {
            for (Property property : model.get(childType).asPropertyList()) {
                PathElement childPath = PathElement.pathElement(childType, property.getName());
                ImmutableManagementResourceRegistration subRegistration = reg.getSubModel(PathAddress.pathAddress(childPath));
                Resource child = modelToResource(startAddress, subRegistration, property.getValue(), includeUndefined, fullPath.append(childPath));
                res.registerChild(childPath, child);
            }
        }
        allFields.remove(childType);
    }

    if (!allFields.isEmpty()){
        throw ControllerLogger.ROOT_LOGGER.modelFieldsNotKnown(allFields, startAddress.append(fullPath));
    }
    return res;
}