Skip to content

Commit

Permalink
Merge pull request wildfly#6433 from rhusar/WFLY-3485_modcluster_tran…
Browse files Browse the repository at this point in the history
…sformers

[WFLY-3485] Refactor mod_cluster transformers and move transformations to its resources
  • Loading branch information
bstansberry committed Jul 11, 2014
2 parents c9a1cc9 + 064a7b6 commit ad9ae24
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.extension.mod_cluster;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.transform.TransformationContext;
import org.jboss.as.controller.transform.description.DefaultCheckersAndConverter;
import org.jboss.dmr.ModelNode;

import java.util.Map;

import static org.wildfly.extension.mod_cluster.LoadMetricDefinition.CAPACITY;

/**
* Converts doubles to ints, rejects expressions or double values that are not equivalent to integers.
* <p/>
* Package protected solely to allow unit testing.
*/
class CapacityCheckerAndConverter extends DefaultCheckersAndConverter {

static final CapacityCheckerAndConverter INSTANCE = new CapacityCheckerAndConverter();

@Override
public String getRejectionLogMessage(Map<String, ModelNode> attributes) {
return ModClusterLogger.ROOT_LOGGER.capacityIsExpressionOrGreaterThanIntegerMaxValue(attributes.get(CAPACITY.getName()));
}

@Override
protected boolean rejectAttribute(PathAddress address, String attributeName, ModelNode attributeValue, TransformationContext context) {
if (checkForExpression(attributeValue) || (attributeValue.isDefined() && !isIntegerValue(attributeValue.asDouble()))) {
return true;
}
Long converted = convert(attributeValue);
return (converted != null && (converted > Integer.MAX_VALUE || converted < Integer.MIN_VALUE));
}

@Override
protected void convertAttribute(PathAddress address, String attributeName, ModelNode attributeValue, TransformationContext context) {
Long converted = convert(attributeValue);
if (converted != null && converted <= Integer.MAX_VALUE && converted >= Integer.MIN_VALUE) {
attributeValue.set((int) converted.longValue());
}
}

@Override
protected boolean isValueDiscardable(PathAddress address, String attributeName, ModelNode attributeValue, TransformationContext context) {
// Not used for discard
return false;
}

private Long convert(ModelNode attributeValue) {
if (attributeValue.isDefined() && !checkForExpression(attributeValue)) {
double raw = attributeValue.asDouble();
if (isIntegerValue(raw)) {
return Math.round(raw);
}
}
return null;
}

private boolean isIntegerValue(double raw) {
return raw == (double) Math.round(raw);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@
package org.wildfly.extension.mod_cluster;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ReloadRequiredRemoveStepHandler;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
import org.jboss.as.controller.SimpleAttributeDefinition;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.transform.description.RejectAttributeChecker;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.dmr.ModelType;

import static org.wildfly.extension.mod_cluster.LoadMetricDefinition.CAPACITY;
import static org.wildfly.extension.mod_cluster.LoadMetricDefinition.WEIGHT;

/**
* @author <a href="mailto:tomaz.cerar@redhat.com">Tomaz Cerar</a>
*/
public class CustomLoadMetricDefinition extends SimpleResourceDefinition {

static final PathElement PATH = PathElement.pathElement(CommonAttributes.CUSTOM_LOAD_METRIC);

protected static final CustomLoadMetricDefinition INSTANCE = new CustomLoadMetricDefinition();

static final SimpleAttributeDefinition CLASS = SimpleAttributeDefinitionBuilder.create(CommonAttributes.CLASS, ModelType.STRING, false)
Expand All @@ -48,8 +57,19 @@ public class CustomLoadMetricDefinition extends SimpleResourceDefinition {
CLASS, LoadMetricDefinition.WEIGHT, LoadMetricDefinition.CAPACITY, LoadMetricDefinition.PROPERTY
};

static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder builder) {
if (ModClusterModel.VERSION_1_2_0.requiresTransformation(version)) {
builder.addChildResource(PATH)
.getAttributeBuilder()
.addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, CLASS, WEIGHT)
.addRejectCheck(CapacityCheckerAndConverter.INSTANCE, CAPACITY)
.setValueConverter(CapacityCheckerAndConverter.INSTANCE, CAPACITY)
.end();
}
}

private CustomLoadMetricDefinition() {
super(ModClusterExtension.CUSTOM_LOAD_METRIC_PATH,
super(PATH,
ModClusterExtension.getResourceDescriptionResolver(CommonAttributes.CONFIGURATION, CommonAttributes.DYNAMIC_LOAD_PROVIDER, CommonAttributes.LOAD_METRIC),
CustomLoadMetricAdd.INSTANCE,
new ReloadRequiredRemoveStepHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
package org.wildfly.extension.mod_cluster;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ReloadRequiredRemoveStepHandler;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
import org.jboss.as.controller.SimpleAttributeDefinition;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.transform.description.RejectAttributeChecker;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.modcluster.load.impl.DynamicLoadBalanceFactorProvider;
Expand All @@ -37,6 +41,9 @@
* @author <a href="mailto:tomaz.cerar@redhat.com">Tomaz Cerar</a>
*/
public class DynamicLoadProviderDefinition extends SimpleResourceDefinition {

static final PathElement PATH = PathElement.pathElement(CommonAttributes.DYNAMIC_LOAD_PROVIDER, CommonAttributes.CONFIGURATION);

protected static final DynamicLoadProviderDefinition INSTANCE = new DynamicLoadProviderDefinition();

static final SimpleAttributeDefinition DECAY = SimpleAttributeDefinitionBuilder.create(CommonAttributes.DECAY, ModelType.INT, true)
Expand All @@ -52,7 +59,7 @@ public class DynamicLoadProviderDefinition extends SimpleResourceDefinition {
.build();

private DynamicLoadProviderDefinition() {
super(ModClusterExtension.DYNAMIC_LOAD_PROVIDER_PATH,
super(PATH,
ModClusterExtension.getResourceDescriptionResolver(CommonAttributes.CONFIGURATION, CommonAttributes.DYNAMIC_LOAD_PROVIDER),
DynamicLoadProviderAdd.INSTANCE,
new ReloadRequiredRemoveStepHandler()
Expand All @@ -63,6 +70,19 @@ private DynamicLoadProviderDefinition() {
HISTORY, DECAY
};


static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder builder) {
if (ModClusterModel.VERSION_1_2_0.requiresTransformation(version)) {
builder.addChildResource(PATH)
.getAttributeBuilder()
.addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, DECAY, HISTORY)
.end();
}

LoadMetricDefinition.buildTransformation(version, builder);
CustomLoadMetricDefinition.buildTransformation(version, builder);
}

@Override
public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
for (AttributeDefinition def : ATTRIBUTES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
package org.wildfly.extension.mod_cluster;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.PropertiesAttributeDefinition;
import org.jboss.as.controller.ReloadRequiredRemoveStepHandler;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
Expand All @@ -31,6 +33,8 @@
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.operations.validation.EnumValidator;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.transform.description.RejectAttributeChecker;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.modcluster.load.metric.LoadMetric;
Expand All @@ -40,6 +44,8 @@
*/
public class LoadMetricDefinition extends SimpleResourceDefinition {

static final PathElement PATH = PathElement.pathElement(CommonAttributes.LOAD_METRIC);

protected static final LoadMetricDefinition INSTANCE = new LoadMetricDefinition();

static final SimpleAttributeDefinition TYPE = SimpleAttributeDefinitionBuilder.create(CommonAttributes.TYPE, ModelType.STRING, false)
Expand Down Expand Up @@ -69,8 +75,21 @@ public class LoadMetricDefinition extends SimpleResourceDefinition {
TYPE, WEIGHT, CAPACITY, PROPERTY
};

static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder builder) {
if (ModClusterModel.VERSION_1_2_0.requiresTransformation(version)) {
builder.addChildResource(PATH)
.getAttributeBuilder()
.addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, TYPE, WEIGHT, CAPACITY, PROPERTY)
.addRejectCheck(CapacityCheckerAndConverter.INSTANCE, CAPACITY)
.setValueConverter(CapacityCheckerAndConverter.INSTANCE, CAPACITY)
.addRejectCheck(PropertyCheckerAndConverter.INSTANCE, PROPERTY)
.setValueConverter(PropertyCheckerAndConverter.INSTANCE, PROPERTY)
.end();
}
}

private LoadMetricDefinition() {
super(ModClusterExtension.LOAD_METRIC_PATH,
super(PATH,
ModClusterExtension.getResourceDescriptionResolver(CommonAttributes.CONFIGURATION, CommonAttributes.DYNAMIC_LOAD_PROVIDER, CommonAttributes.LOAD_METRIC),
LoadMetricAdd.INSTANCE,
new ReloadRequiredRemoveStepHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public class ModClusterAddCustomMetric implements OperationStepHandler {
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
PathAddress opAddress = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR));
PathAddress parent = opAddress.append(ModClusterExtension.DYNAMIC_LOAD_PROVIDER_PATH);
PathAddress parent = opAddress.append(DynamicLoadProviderDefinition.PATH);
String name = CustomLoadMetricDefinition.CLASS.resolveModelAttribute(context, operation).asString();

ModelNode targetOperation = Util.createAddOperation(parent.append(PathElement.pathElement(ModClusterExtension.CUSTOM_LOAD_METRIC_PATH.getKey(), name)));
ModelNode targetOperation = Util.createAddOperation(parent.append(PathElement.pathElement(CustomLoadMetricDefinition.PATH.getKey(), name)));

for (AttributeDefinition def : CustomLoadMetricDefinition.ATTRIBUTES) {
def.validateAndSet(operation, targetOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class ModClusterAddMetric implements OperationStepHandler {
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

PathAddress opAddress = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR));
PathAddress parent = opAddress.append(ModClusterExtension.DYNAMIC_LOAD_PROVIDER_PATH);
PathAddress parent = opAddress.append(DynamicLoadProviderDefinition.PATH);
String name = LoadMetricDefinition.TYPE.resolveModelAttribute(context, operation).asString();
ModelNode targetOperation = Util.createAddOperation(parent.append(PathElement.pathElement(ModClusterExtension.LOAD_METRIC_PATH.getKey(), name)));
ModelNode targetOperation = Util.createAddOperation(parent.append(PathElement.pathElement(LoadMetricDefinition.PATH.getKey(), name)));

for (AttributeDefinition def : LoadMetricDefinition.ATTRIBUTES) {
def.validateAndSet(operation, targetOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class ModClusterAddProxy implements OperationStepHandler {

static OperationDefinition getDefinition(ResourceDescriptionResolver descriptionResolver) {
return new SimpleOperationDefinitionBuilder(CommonAttributes.ADD_PROXY, descriptionResolver)
.addParameter(ModClusterDefinition.HOST)
.addParameter(ModClusterDefinition.PORT)
.addParameter(ModClusterSubsystemResourceDefinition.HOST)
.addParameter(ModClusterSubsystemResourceDefinition.PORT)
.setRuntimeOnly()
.addAccessConstraint(ModClusterExtension.MOD_CLUSTER_PROXIES_DEF)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package org.wildfly.extension.mod_cluster;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.OperationDefinition;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ReloadRequiredRemoveStepHandler;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
import org.jboss.as.controller.SimpleAttributeDefinition;
Expand All @@ -37,6 +39,8 @@
import org.jboss.as.controller.operations.validation.IntRangeValidator;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.OperationEntry;
import org.jboss.as.controller.transform.description.RejectAttributeChecker;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;

Expand All @@ -56,6 +60,8 @@
*/
class ModClusterConfigResourceDefinition extends SimpleResourceDefinition {

static final PathElement PATH = PathElement.pathElement(CommonAttributes.MOD_CLUSTER_CONFIG, CommonAttributes.CONFIGURATION);

static final SimpleAttributeDefinition ADVERTISE_SOCKET = SimpleAttributeDefinitionBuilder.create(CommonAttributes.ADVERTISE_SOCKET, ModelType.STRING, true)
.setRestartAllServices()
.addAccessConstraint(SensitiveTargetAccessConstraintDefinition.SOCKET_BINDING_REF)
Expand Down Expand Up @@ -272,8 +278,28 @@ class ModClusterConfigResourceDefinition extends SimpleResourceDefinition {
ATTRIBUTES_BY_NAME = Collections.unmodifiableMap(attrs);
}

public static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder parent) {
ResourceTransformationDescriptionBuilder builder = parent.addChildResource(PATH);

if (ModClusterModel.VERSION_1_3_0.requiresTransformation(version) || ModClusterModel.VERSION_1_4_0.requiresTransformation(version)) {
builder.getAttributeBuilder()
.addRejectCheck(SessionDrainingStrategyChecker.INSTANCE, SESSION_DRAINING_STRATEGY)
.setDiscard(SessionDrainingStrategyChecker.INSTANCE, SESSION_DRAINING_STRATEGY)
.end();
}

if (ModClusterModel.VERSION_1_2_0.requiresTransformation(version)) {
builder.getAttributeBuilder()
.addRejectCheck(RejectAttributeChecker.SIMPLE_EXPRESSIONS, ADVERTISE, AUTO_ENABLE_CONTEXTS, FLUSH_PACKETS, STICKY_SESSION, STICKY_SESSION_REMOVE, STICKY_SESSION_FORCE, PING)
.end();
}

DynamicLoadProviderDefinition.buildTransformation(version, builder);
ModClusterSSLResourceDefinition.buildTransformation(version, builder);
}

public ModClusterConfigResourceDefinition() {
super(ModClusterExtension.CONFIGURATION_PATH,
super(PATH,
ModClusterExtension.getResourceDescriptionResolver(CommonAttributes.CONFIGURATION),
ModClusterConfigAdd.INSTANCE,
new ReloadRequiredRemoveStepHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class ModClusterDisableContext implements OperationStepHandler {

static OperationDefinition getDefinition(ResourceDescriptionResolver descriptionResolver) {
return new SimpleOperationDefinitionBuilder(CommonAttributes.DISABLE_CONTEXT, descriptionResolver)
.addParameter(ModClusterDefinition.VIRTUAL_HOST)
.addParameter(ModClusterDefinition.CONTEXT)
.addParameter(ModClusterSubsystemResourceDefinition.VIRTUAL_HOST)
.addParameter(ModClusterSubsystemResourceDefinition.CONTEXT)
.setRuntimeOnly()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class ModClusterEnableContext implements OperationStepHandler {

static OperationDefinition getDefinition(ResourceDescriptionResolver descriptionResolver) {
return new SimpleOperationDefinitionBuilder(CommonAttributes.ENABLE_CONTEXT, descriptionResolver)
.addParameter(ModClusterDefinition.VIRTUAL_HOST)
.addParameter(ModClusterDefinition.CONTEXT)
.addParameter(ModClusterSubsystemResourceDefinition.VIRTUAL_HOST)
.addParameter(ModClusterSubsystemResourceDefinition.CONTEXT)
.setRuntimeOnly()
.build();
}
Expand Down
Loading

0 comments on commit ad9ae24

Please sign in to comment.