forked from wildfly/wildfly-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterfaceDefinition.java
380 lines (332 loc) · 19.8 KB
/
InterfaceDefinition.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, 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.controller.resource;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALTERNATIVES;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INTERFACE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE_TYPE;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.AttributeMarshaller;
import org.jboss.as.controller.ListAttributeDefinition;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.ParameterCorrector;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ReadResourceNameOperationStepHandler;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.SimpleListAttributeDefinition;
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.access.management.AccessConstraintDefinition;
import org.jboss.as.controller.access.management.SensitiveTargetAccessConstraintDefinition;
import org.jboss.as.controller.client.helpers.MeasurementUnit;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
import org.jboss.as.controller.descriptions.common.ControllerResolver;
import org.jboss.as.controller.logging.ControllerLogger;
import org.jboss.as.controller.operations.common.InterfaceAddHandler;
import org.jboss.as.controller.operations.common.InterfaceCriteriaWriteHandler;
import org.jboss.as.controller.operations.common.InterfaceRemoveHandler;
import org.jboss.as.controller.operations.validation.ModelTypeValidator;
import org.jboss.as.controller.operations.validation.ParameterValidator;
import org.jboss.as.controller.parsing.Element;
import org.jboss.as.controller.registry.AttributeAccess;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
/**
* @author <a href="mailto:tomaz.cerar@redhat.com">Tomaz Cerar</a> (c) 2012 Red Hat Inc.
*/
public class InterfaceDefinition extends SimpleResourceDefinition {
public static final String INTERFACE_CAPABILITY_NAME = "org.wildfly.network.interface";
public static final String[] OTHERS = new String[]{localName(Element.INET_ADDRESS), localName(Element.LINK_LOCAL_ADDRESS),
localName(Element.LOOPBACK), localName(Element.LOOPBACK_ADDRESS), localName(Element.MULTICAST), localName(Element.NIC),
localName(Element.NIC_MATCH), localName(Element.POINT_TO_POINT), localName(Element.PUBLIC_ADDRESS), localName(Element.SITE_LOCAL_ADDRESS),
localName(Element.SUBNET_MATCH), localName(Element.UP), localName(Element.VIRTUAL),
localName(Element.ANY), localName(Element.NOT)
};
public static final AttributeDefinition NAME = SimpleAttributeDefinitionBuilder.create(ModelDescriptionConstants.NAME, ModelType.STRING)
.setResourceOnly()
.build();
public static final AttributeDefinition ANY_ADDRESS = SimpleAttributeDefinitionBuilder.create(ModelDescriptionConstants.ANY_ADDRESS, ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).setRestartAllServices()
.setValidator(new ModelTypeValidator(ModelType.BOOLEAN, true, false))
.addAlternatives(OTHERS)
.build();
/**
* All other attribute names.
*/
public static final AttributeDefinition INET_ADDRESS = SimpleAttributeDefinitionBuilder.create(ModelDescriptionConstants.INET_ADDRESS, ModelType.STRING)
.setAllowExpression(true).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition LINK_LOCAL_ADDRESS = SimpleAttributeDefinitionBuilder.create(localName(Element.LINK_LOCAL_ADDRESS), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition LOOPBACK = SimpleAttributeDefinitionBuilder.create(localName(Element.LOOPBACK), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition LOOPBACK_ADDRESS = SimpleAttributeDefinitionBuilder.create(localName(Element.LOOPBACK_ADDRESS), ModelType.STRING)
.setAllowExpression(true).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition NIC = SimpleAttributeDefinitionBuilder.create(localName(Element.NIC), ModelType.STRING)
.setAllowExpression(true).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition NIC_MATCH = SimpleAttributeDefinitionBuilder.create(localName(Element.NIC_MATCH), ModelType.STRING)
.setAllowExpression(true).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition MULTICAST = SimpleAttributeDefinitionBuilder.create(localName(Element.MULTICAST), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition POINT_TO_POINT = SimpleAttributeDefinitionBuilder.create(localName(Element.POINT_TO_POINT), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition PUBLIC_ADDRESS = SimpleAttributeDefinitionBuilder.create(localName(Element.PUBLIC_ADDRESS), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition SITE_LOCAL_ADDRESS = SimpleAttributeDefinitionBuilder.create(localName(Element.SITE_LOCAL_ADDRESS), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition SUBNET_MATCH = SimpleAttributeDefinitionBuilder.create(localName(Element.SUBNET_MATCH), ModelType.STRING)
.setAllowExpression(true).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition UP = SimpleAttributeDefinitionBuilder.create(localName(Element.UP), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
/**
* The any-* alternatives.
*/
public static final AttributeDefinition VIRTUAL = SimpleAttributeDefinitionBuilder.create(localName(Element.VIRTUAL), ModelType.BOOLEAN)
.setAllowExpression(false).setAllowNull(true).addAlternatives(ModelDescriptionConstants.ANY_ADDRESS).setRestartAllServices()
.build();
public static final AttributeDefinition[] NESTED_ATTRIBUTES = new AttributeDefinition[]{
INET_ADDRESS, LINK_LOCAL_ADDRESS, LOOPBACK, LOOPBACK_ADDRESS, MULTICAST, NIC,
NIC_MATCH, POINT_TO_POINT, PUBLIC_ADDRESS, SITE_LOCAL_ADDRESS, SUBNET_MATCH, UP, VIRTUAL
};
public static final AttributeDefinition NOT = createNestedComplexType("not");
public static final AttributeDefinition ANY = createNestedComplexType("any");
/*public static final ObjectTypeAttributeDefinition NOT = new ObjectTypeAttributeDefinition.Builder("not", NESTED_ATTRIBUTES)
.setAllowNull(true)
.build();
public static final ObjectTypeAttributeDefinition ANY = new ObjectTypeAttributeDefinition.Builder("any", NESTED_ATTRIBUTES)
.setAllowNull(true)
.build();*/
/**
* The root attributes.
*/
public static final AttributeDefinition[] ROOT_ATTRIBUTES = new AttributeDefinition[]{
ANY_ADDRESS, INET_ADDRESS, LINK_LOCAL_ADDRESS,
LOOPBACK, LOOPBACK_ADDRESS, MULTICAST, NIC, NIC_MATCH, POINT_TO_POINT, PUBLIC_ADDRESS,
SITE_LOCAL_ADDRESS, SUBNET_MATCH, UP, VIRTUAL, ANY, NOT
};
/**
* The nested attributes for any, not.
*/
public static final Set<AttributeDefinition> NESTED_LIST_ATTRIBUTES = new HashSet<AttributeDefinition>(
Arrays.asList(INET_ADDRESS, NIC, NIC_MATCH, SUBNET_MATCH)
);
/**
* The wildcard criteria attributes
*/
/*public static final AttributeDefinition[] WILDCARD_ATTRIBUTES = new AttributeDefinition[]{ANY_ADDRESS, ANY_IPV4_ADDRESS, ANY_IPV6_ADDRESS};
public static final AttributeDefinition[] SIMPLE_ATTRIBUTES = new AttributeDefinition[]{LINK_LOCAL_ADDRESS, LOOPBACK,
MULTICAST, POINT_TO_POINT, PUBLIC_ADDRESS, SITE_LOCAL_ADDRESS, UP, VIRTUAL};*/
private final boolean updateRuntime;
private final List<AccessConstraintDefinition> sensitivity;
private final boolean resolvable;
public InterfaceDefinition(InterfaceAddHandler addHandler, InterfaceRemoveHandler removeHandler, boolean updateRuntime, boolean resolvable) {
super(PathElement.pathElement(INTERFACE),
ControllerResolver.getResolver(INTERFACE),
addHandler,
removeHandler);
this.updateRuntime = updateRuntime;
this.resolvable = resolvable;
this.sensitivity = SensitiveTargetAccessConstraintDefinition.SOCKET_CONFIG.wrapAsList();
}
public static String localName(final Element element) {
return element.getLocalName();
}
/**
* Test whether the operation has a defined criteria attribute.
*
* @param operation the operation
* @return
*/
public static boolean isOperationDefined(final ModelNode operation) {
for (final AttributeDefinition def : ROOT_ATTRIBUTES) {
if (operation.hasDefined(def.getName())) {
return true;
}
}
return false;
}
@Override
public void registerOperations(ManagementResourceRegistration interfaces) {
super.registerOperations(interfaces);
if(resolvable) {
interfaces.registerOperationHandler(org.jboss.as.controller.operations.global.ReadResourceHandler.RESOLVE_DEFINITION,
org.jboss.as.controller.operations.global.ReadResourceHandler.RESOLVE_INSTANCE, true);
interfaces.registerOperationHandler(org.jboss.as.controller.operations.global.ReadAttributeHandler.RESOLVE_DEFINITION,
org.jboss.as.controller.operations.global.ReadAttributeHandler.RESOLVE_INSTANCE, true);
interfaces.registerOperationHandler(org.jboss.as.controller.operations.global.ReadAttributeGroupHandler.RESOLVE_DEFINITION,
org.jboss.as.controller.operations.global.ReadAttributeGroupHandler.RESOLVE_INSTANCE, true);
}
}
@Override
public void registerAttributes(ManagementResourceRegistration registration) {
super.registerAttributes(registration);
InterfaceCriteriaWriteHandler handler = updateRuntime ? InterfaceCriteriaWriteHandler.UPDATE_RUNTIME : InterfaceCriteriaWriteHandler.CONFIG_ONLY;
for (final AttributeDefinition def : InterfaceDefinition.ROOT_ATTRIBUTES) {
registration.registerReadWriteAttribute(def, null, handler);
}
registration.registerReadOnlyAttribute(InterfaceDefinition.NAME, ReadResourceNameOperationStepHandler.INSTANCE);
}
@Override
public List<AccessConstraintDefinition> getAccessConstraints() {
return sensitivity;
}
@Deprecated
private static AttributeDefinition createNestedComplexType(final String name) {
return new AttributeDefinition(name, name, null, ModelType.OBJECT,
true, false, MeasurementUnit.NONE,
(ParameterCorrector) null, createNestedParamValidator(), true,
new String[]{ModelDescriptionConstants.ANY_ADDRESS}, (String[]) null, (AttributeMarshaller) null,
false, null, null, (Boolean) null, null,
AttributeAccess.Flag.RESTART_ALL_SERVICES) {
@Override
public ModelNode addResourceAttributeDescription(final ResourceBundle bundle, final String prefix, final ModelNode resourceDescription) {
final ModelNode result = super.addResourceAttributeDescription(bundle, prefix, resourceDescription);
addNestedDescriptions(result, prefix, bundle);
return result;
}
@Override
public ModelNode addOperationParameterDescription(ResourceBundle bundle, String prefix, ModelNode operationDescription) {
final ModelNode result = super.addOperationParameterDescription(bundle, prefix, operationDescription);
addNestedDescriptions(result, prefix, bundle);
return result;
}
@Override
public ModelNode addResourceAttributeDescription(ModelNode resourceDescription, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
ModelNode result = super.addResourceAttributeDescription(resourceDescription, resolver, locale, bundle);
for (final AttributeDefinition def : NESTED_ATTRIBUTES) {
result.get(VALUE_TYPE, def.getName(), ModelDescriptionConstants.DESCRIPTION).set(resolver.getResourceAttributeDescription(def.getName(), locale, bundle));
}
return result;
}
@Override
public ModelNode addOperationParameterDescription(ModelNode resourceDescription, String operationName, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
ModelNode result = super.addOperationParameterDescription(resourceDescription, operationName, resolver, locale, bundle);
for (final AttributeDefinition def : NESTED_ATTRIBUTES) {
result.get(VALUE_TYPE, def.getName(), ModelDescriptionConstants.DESCRIPTION).set(resolver.getOperationParameterDescription(operationName, def.getName(), locale, bundle));
}
return result;
}
void addNestedDescriptions(final ModelNode result, final String prefix, final ResourceBundle bundle) {
for (final AttributeDefinition def : NESTED_ATTRIBUTES) {
final String bundleKey = prefix == null ? def.getName() : (prefix + "." + def.getName());
result.get(VALUE_TYPE, def.getName(), ModelDescriptionConstants.DESCRIPTION).set(bundle.getString(bundleKey));
}
}
@Override
public ModelNode getNoTextDescription(boolean forOperation) {
final ModelNode model = super.getNoTextDescription(forOperation);
final ModelNode valueType = model.get(VALUE_TYPE);
for (final AttributeDefinition def : NESTED_ATTRIBUTES) {
final AttributeDefinition current;
if (NESTED_LIST_ATTRIBUTES.contains(def)) {
current = wrapAsList(def);
} else {
current = def;
}
final ModelNode m = current.getNoTextDescription(forOperation);
m.remove(ALTERNATIVES);
valueType.get(current.getName()).set(m);
}
return model;
}
};
}
/**
* Wrap a simple attribute def as list.
*
* @param def the attribute definition
* @return the list attribute def
*/
@Deprecated
private static ListAttributeDefinition wrapAsList(final AttributeDefinition def) {
final ListAttributeDefinition list = new ListAttributeDefinition(new SimpleListAttributeDefinition.Builder(def.getName(), def)
.setElementValidator(def.getValidator())) {
@Override
public ModelNode getNoTextDescription(boolean forOperation) {
final ModelNode model = super.getNoTextDescription(forOperation);
setValueType(model);
return model;
}
@Override
protected void addValueTypeDescription(final ModelNode node, final ResourceBundle bundle) {
setValueType(node);
}
@Override
public void marshallAsElement(final ModelNode resourceModel, final boolean marshalDefault, final XMLStreamWriter writer) throws XMLStreamException {
throw new RuntimeException();
}
@Override
protected void addAttributeValueTypeDescription(ModelNode node, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
setValueType(node);
}
@Override
protected void addOperationParameterValueTypeDescription(ModelNode node, String operationName, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
setValueType(node);
}
private void setValueType(ModelNode node) {
node.get(ModelDescriptionConstants.VALUE_TYPE).set(ModelType.STRING);
}
};
return list;
}
@Deprecated
static ParameterValidator createNestedParamValidator() {
return new ModelTypeValidator(ModelType.OBJECT, true, false, true) {
@Override
public void validateParameter(final String parameterName, final ModelNode value) throws OperationFailedException {
super.validateParameter(parameterName, value);
for (final AttributeDefinition def : NESTED_ATTRIBUTES) {
final String name = def.getName();
if (value.hasDefined(name)) {
final ModelNode v = value.get(name);
if (NESTED_LIST_ATTRIBUTES.contains(def)) {
if (ModelType.LIST != v.getType()) {
throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.invalidType(v.getType()));
}
} else {
def.getValidator().validateParameter(name, v);
}
}
}
}
};
}
}