-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ChannelResourceDefinition.java
251 lines (220 loc) · 13 KB
/
ChannelResourceDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* 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.jboss.as.clustering.jgroups.subsystem;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.function.UnaryOperator;
import org.jboss.as.clustering.controller.CapabilityProvider;
import org.jboss.as.clustering.controller.CapabilityReference;
import org.jboss.as.clustering.controller.ChildResourceDefinition;
import org.jboss.as.clustering.controller.ManagementResourceRegistration;
import org.jboss.as.clustering.controller.MetricHandler;
import org.jboss.as.clustering.controller.ResourceDescriptor;
import org.jboss.as.clustering.controller.SimpleResourceRegistration;
import org.jboss.as.clustering.controller.ResourceServiceHandler;
import org.jboss.as.clustering.controller.UnaryRequirementCapability;
import org.jboss.as.clustering.controller.validation.ModuleIdentifierValidatorBuilder;
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.registry.AttributeAccess;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.controller.transform.TransformationContext;
import org.jboss.as.controller.transform.description.DiscardAttributeChecker;
import org.jboss.as.controller.transform.description.DiscardAttributeChecker.DefaultDiscardAttributeChecker;
import org.jboss.as.controller.transform.description.DiscardPolicy;
import org.jboss.as.controller.transform.description.DynamicDiscardPolicy;
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.wildfly.clustering.jgroups.spi.JGroupsRequirement;
import org.wildfly.clustering.service.UnaryRequirement;
import org.wildfly.clustering.spi.ClusteringRequirement;
/**
* Definition for /subsystem=jgroups/channel=* resources
*
* @author Paul Ferraro
*/
public class ChannelResourceDefinition extends ChildResourceDefinition<ManagementResourceRegistration> {
public static final PathElement WILDCARD_PATH = pathElement(PathElement.WILDCARD_VALUE);
public static PathElement pathElement(String name) {
return PathElement.pathElement("channel", name);
}
enum Capability implements CapabilityProvider {
JCHANNEL(JGroupsRequirement.CHANNEL),
FORK_CHANNEL_FACTORY(JGroupsRequirement.CHANNEL_FACTORY),
JCHANNEL_FACTORY(JGroupsRequirement.CHANNEL_SOURCE),
JCHANNEL_MODULE(JGroupsRequirement.CHANNEL_MODULE),
JCHANNEL_CLUSTER(JGroupsRequirement.CHANNEL_CLUSTER),
;
private org.jboss.as.clustering.controller.Capability capability;
Capability(UnaryRequirement requirement) {
this.capability = new UnaryRequirementCapability(requirement);
}
@Override
public org.jboss.as.clustering.controller.Capability getCapability() {
return this.capability;
}
}
static final Map<ClusteringRequirement, org.jboss.as.clustering.controller.Capability> CLUSTERING_CAPABILITIES = new EnumMap<>(ClusteringRequirement.class);
static {
EnumSet.allOf(ClusteringRequirement.class).forEach(requirement -> CLUSTERING_CAPABILITIES.put(requirement, new UnaryRequirementCapability(requirement)));
}
public enum Attribute implements org.jboss.as.clustering.controller.Attribute {
STACK("stack", ModelType.STRING, builder -> builder
.setRequired(true)
.setAllowExpression(false)
.setCapabilityReference(new CapabilityReference(Capability.JCHANNEL_FACTORY, JGroupsRequirement.CHANNEL_FACTORY))),
MODULE("module", ModelType.STRING, builder -> builder
.setDefaultValue(new ModelNode("org.wildfly.clustering.server"))
.setValidator(new ModuleIdentifierValidatorBuilder().configure(builder).build())),
CLUSTER("cluster", ModelType.STRING),
STATISTICS_ENABLED("statistics-enabled", ModelType.BOOLEAN, builder -> builder.setDefaultValue(new ModelNode(false))),
;
private final AttributeDefinition definition;
Attribute(String name, ModelType type) {
this(name, type, UnaryOperator.identity());
}
Attribute(String name, ModelType type, UnaryOperator<SimpleAttributeDefinitionBuilder> configurator) {
this.definition = configurator.apply(new SimpleAttributeDefinitionBuilder(name, type)
.setAllowExpression(true)
.setRequired(false)
.setFlags(AttributeAccess.Flag.RESTART_RESOURCE_SERVICES)
).build();
}
@Override
public AttributeDefinition getDefinition() {
return this.definition;
}
}
public enum DeprecatedAttribute implements org.jboss.as.clustering.controller.Attribute {
STATS_ENABLED("stats-enabled", ModelType.BOOLEAN, JGroupsModel.VERSION_4_1_0),
;
private final AttributeDefinition definition;
DeprecatedAttribute(String name, ModelType type, JGroupsModel deprecation) {
this.definition = new SimpleAttributeDefinitionBuilder(name, type, true).setDeprecated(deprecation.getVersion()).setStorageRuntime().build();
}
@Override
public AttributeDefinition getDefinition() {
return this.definition;
}
}
static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder parent) {
if (JGroupsModel.VERSION_3_0_0.requiresTransformation(version)) {
DynamicDiscardPolicy channelDiscardRejectPolicy = new DynamicDiscardPolicy() {
@Override
public DiscardPolicy checkResource(TransformationContext context, PathAddress address) {
// Check whether all channel resources are used by the infinispan subsystem, and transformed
// by its corresponding transformers; reject otherwise
// n.b. we need to hard-code the values because otherwise we would end up with cyclical dependency
String channelName = address.getLastElement().getValue();
PathAddress rootAddress = address.subAddress(0, address.size() - 2);
PathAddress subsystemAddress = rootAddress.append(PathElement.pathElement(ModelDescriptionConstants.SUBSYSTEM, "infinispan"));
Resource infinispanResource;
try {
infinispanResource = context.readResourceFromRoot(subsystemAddress);
} catch (Resource.NoSuchResourceException ex) {
return DiscardPolicy.REJECT_AND_WARN;
}
ModelNode infinispanModel = Resource.Tools.readModel(infinispanResource);
if (infinispanModel.hasDefined("cache-container")) {
for (ModelNode container : infinispanModel.get("cache-container").asList()) {
ModelNode cacheContainer = container.get(0);
if (cacheContainer.hasDefined("transport")) {
ModelNode transport = cacheContainer.get("transport").get("jgroups");
if (transport.hasDefined("channel")) {
String channel = transport.get("channel").asString();
if (channel.equals(channelName)) {
return DiscardPolicy.SILENT;
}
} else {
// In that case, if this were the default channel, it can be discarded too
ModelNode subsystem = context.readResourceFromRoot(address.subAddress(0, address.size() - 1)).getModel();
if (subsystem.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName())) {
if (subsystem.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName()).asString().equals(channelName)) {
return DiscardPolicy.SILENT;
}
}
}
}
}
}
// No references to this channel, we need to reject it.
return DiscardPolicy.REJECT_AND_WARN;
}
};
parent.addChildResource(WILDCARD_PATH, channelDiscardRejectPolicy);
} else {
ResourceTransformationDescriptionBuilder builder = parent.addChildResource(WILDCARD_PATH);
if (JGroupsModel.VERSION_4_0_0.requiresTransformation(version)) {
DiscardAttributeChecker discarder = new DefaultDiscardAttributeChecker(false, true) {
@Override
protected boolean isValueDiscardable(PathAddress address, String attributeName, ModelNode attributeValue, TransformationContext context) {
return !attributeValue.isDefined() || attributeValue.equals(new ModelNode(address.getLastElement().getValue()));
}
};
builder.getAttributeBuilder()
.setDiscard(discarder, Attribute.CLUSTER.getDefinition())
.addRejectCheck(RejectAttributeChecker.DEFINED, Attribute.CLUSTER.getDefinition())
;
}
ProtocolRegistration.buildTransformation(version, builder);
}
}
ChannelResourceDefinition() {
super(WILDCARD_PATH, new JGroupsResourceDescriptionResolver(WILDCARD_PATH));
}
@Override
public void register(ManagementResourceRegistration parentRegistration) {
ManagementResourceRegistration registration = parentRegistration.registerSubModel(this);
@SuppressWarnings("deprecation")
ResourceDescriptor descriptor = new ResourceDescriptor(this.getResourceDescriptionResolver())
.addAttributes(Attribute.class)
.addCapabilities(Capability.class)
.addCapabilities(CLUSTERING_CAPABILITIES.values())
.addAlias(DeprecatedAttribute.STATS_ENABLED, Attribute.STATISTICS_ENABLED)
.setAddOperationTransformation(handler -> (context, operation) -> {
// Handle recipe for version < 4.0 where stack was not required and the stack attribute would use default-stack for a default value
if (!operation.hasDefined(Attribute.STACK.getName())) {
ModelNode parentModel = context.readResourceFromRoot(context.getCurrentAddress().getParent()).getModel();
// If default-stack is not defined either, then recipe must be for version >= 4.0 and so this really is an invalid operation
if (parentModel.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName())) {
operation.get(Attribute.STACK.getName()).set(parentModel.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName()));
}
}
handler.execute(context, operation);
})
.addRuntimeResourceRegistration(new ChannelRuntimeResourceRegistration())
;
ResourceServiceHandler handler = new ChannelServiceHandler();
new SimpleResourceRegistration(descriptor, handler).register(registration);
if (registration.isRuntimeOnlyRegistrationValid()) {
new MetricHandler<>(new ChannelMetricExecutor(), ChannelMetric.class).register(registration);
}
new ForkResourceDefinition().register(registration);
}
}