Skip to content

Commit

Permalink
[WFCORE-6503]: Ignoring removal of unexisting resource
Browse files Browse the repository at this point in the history
Signed-off-by: Emmanuel Hugonnet <[email protected]>
  • Loading branch information
ehsavoie committed Mar 19, 2024
1 parent 4b3c9d3 commit ea55b5c
Showing 1 changed file with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void load() {
for (Path file : files) {
if (file != null && Files.exists(file) && Files.isRegularFile(file)) {
Map<String, Object> yamlConfig = Collections.emptyMap();
Yaml yaml = new Yaml(new OperationConstructor(new LoaderOptions()));
Yaml yaml = new Yaml(new OperationConstructor(new LoaderOptions()));
try (InputStream inputStream = Files.newInputStream(file)) {
yamlConfig = yaml.load(inputStream);
} catch (IOException ioex) {
Expand All @@ -115,11 +115,11 @@ private void load() {
for (String excluded : EXCLUDED_ELEMENTS) {
boolean isPresent = config.containsKey(excluded);
Object value = config.remove(excluded);
if(value != null && value instanceof Map && DEPLOYMENT.equals(excluded)) {
deployments.putAll((Map<String, Object>) value);
if (value != null && value instanceof Map && DEPLOYMENT.equals(excluded)) {
deployments.putAll((Map<String, Object>) value);
} else if (isPresent) {
String message = MGMT_OP_LOGGER.ignoreYamlElement(excluded);
if(value != null) {
if (value != null) {
message = message + MGMT_OP_LOGGER.ignoreYamlSubElement(yaml.dump(value).trim());
}
MGMT_OP_LOGGER.warn(message);
Expand Down Expand Up @@ -162,7 +162,7 @@ public void processOperations(ImmutableManagementResourceRegistration rootRegist
for (Map<String, Object> config : configs) {
processResource(PathAddress.EMPTY_ADDRESS, new HashMap<>(config), rootRegistration, rootRegistration, xmlOperations, postExtensionOps, false);
}
for(Map.Entry<String, Object> deployment : deployments.entrySet()) {
for (Map.Entry<String, Object> deployment : deployments.entrySet()) {
processUnmanagedDeployments(rootRegistration, deployment, xmlOperations, postExtensionOps);
}
this.configs.clear();
Expand Down Expand Up @@ -197,7 +197,7 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
} else {
if (value == null && !isExistingResource(xmlOperations, address)) { //empty resource
OperationEntry operationEntry = rootRegistration.getOperationEntry(address, ADD);
if(operationEntry != null) {
if (operationEntry != null) {
processAttributes(address, rootRegistration, operationEntry, Collections.emptyMap(), postExtensionOps, xmlOperations);
} else {
throw MGMT_OP_LOGGER.missingOperationForResource("ADD", address.toCLIStyleString());
Expand All @@ -209,10 +209,10 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
} else if (yamlOperation instanceof RemoveOperation) {
//ignore
} else {
if(yamlOperation instanceof UndefineOperation) {
throw MGMT_OP_LOGGER.noResourceForUndefiningAttribute(name, address.toCLIStyleString());
if (yamlOperation instanceof UndefineOperation) {
throw MGMT_OP_LOGGER.noResourceForUndefiningAttribute(name, address.toCLIStyleString());
}
throw MGMT_OP_LOGGER.illegalOperationForAttribute(yamlOperation.getOperationName() ,name, address.toCLIStyleString());
throw MGMT_OP_LOGGER.illegalOperationForAttribute(yamlOperation.getOperationName(), name, address.toCLIStyleString());
}
} else {
if (!isExistingResource(xmlOperations, address)) {
Expand All @@ -233,13 +233,13 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
Object value = yaml.get(name);
if (value instanceof Map) {
Map<String, Object> map = (Map<String, Object>) value;
if (resourceRegistration.getAttributeNames(PathAddress.EMPTY_ADDRESS).contains(name) &&
resourceRegistration.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name).getAttributeDefinition().getType() == OBJECT) {
if (resourceRegistration.getAttributeNames(PathAddress.EMPTY_ADDRESS).contains(name)
&& resourceRegistration.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name).getAttributeDefinition().getType() == OBJECT) {
processAttribute(address, rootRegistration, name, value, postExtensionOps, xmlOperations);
} else if( !address.equals(parentAddress)) {
} else if (!address.equals(parentAddress)) {
processResource(address, map, rootRegistration, rootRegistration.getSubModel(address), xmlOperations, postExtensionOps, false);
} else {
throw MGMT_OP_LOGGER.noChildResource(name , address.toCLIStyleString());
throw MGMT_OP_LOGGER.noChildResource(name, address.toCLIStyleString());
}
} else if (value instanceof Operation) {
Operation yamlOperation = Operation.class.cast(value);
Expand Down Expand Up @@ -271,8 +271,8 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
} else {
if (!postExtensionOps.isEmpty()) {
ParsedBootOp op = postExtensionOps.get(postExtensionOps.size() - 1);
if (! address.equals(op.getAddress())) { // else already processed
Map<String, Object> map = value instanceof Map ? new HashMap<>((Map)value) : new HashMap<>(yaml);
if (!address.equals(op.getAddress())) { // else already processed
Map<String, Object> map = value instanceof Map ? new HashMap<>((Map) value) : new HashMap<>(yaml);
//need to process attributes for adding
processAttributes(address, rootRegistration, operationEntry, map, postExtensionOps, xmlOperations);
processResource(address, map, rootRegistration, resourceRegistration, xmlOperations, postExtensionOps, false);
Expand All @@ -292,7 +292,15 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
processResource(address, map, rootRegistration, childResourceRegistration, xmlOperations, postExtensionOps, false);
} else {
if (value != null) {
MGMT_OP_LOGGER.unexpectedValueForResource(value, address.toCLIStyleString(), name);
if (value instanceof Operation) {
if (value instanceof RemoveOperation) {
//ignore
} else {
MGMT_OP_LOGGER.illegalOperationForAttribute(((Operation) value).getOperationName(), name, address.toPathStyleString());
}
} else {
MGMT_OP_LOGGER.unexpectedValueForResource(value, address.toCLIStyleString(), name);
}
}
}
} else if (name.equals(address.getLastElement().getValue())) {
Expand All @@ -304,7 +312,15 @@ private void processResource(PathAddress parentAddress, Map<String, Object> yaml
processResource(address, map, rootRegistration, childResourceRegistration, xmlOperations, postExtensionOps, false);
} else {
if (value != null) {
MGMT_OP_LOGGER.unexpectedValueForResource(value, address.toCLIStyleString(), name);
if (value instanceof Operation) {
if (value instanceof RemoveOperation) {
//ignore
} else {
MGMT_OP_LOGGER.illegalOperationForAttribute(((Operation) value).getOperationName(), name, address.toPathStyleString());
}
} else {
MGMT_OP_LOGGER.unexpectedValueForResource(value, address.toCLIStyleString(), name);
}
} else {// ADD operation without parameters
processAttributes(address, rootRegistration, operationEntry, null, postExtensionOps, xmlOperations);
}
Expand Down Expand Up @@ -414,7 +430,7 @@ private void processAttributes(PathAddress address, ImmutableManagementResourceR
processListAttribute((ListAttributeDefinition) att, list, value);
break;
default:
if(value != null) {
if (value != null) {
op.get(att.getName()).set(value.toString());
} else {
op.get(att.getName());
Expand All @@ -427,7 +443,7 @@ private void processAttributes(PathAddress address, ImmutableManagementResourceR
ParsedBootOp operation = new ParsedBootOp(op, operationEntry.getOperationHandler());
MGMT_OP_LOGGER.debugf("Adding resource with operation %s", op);
postExtensionOps.add(operation);
if(ADD.equals(operationEntry.getOperationDefinition().getName())) {
if (ADD.equals(operationEntry.getOperationDefinition().getName())) {
xmlOperations.put(address, operation);
}
}
Expand All @@ -447,8 +463,8 @@ private ModelNode processObjectAttribute(ObjectTypeAttributeDefinition att, Map<
Object value = map.get(child.getName());
switch (child.getType()) {
case OBJECT:
if(child instanceof MapAttributeDefinition) {
processMapAttribute((MapAttributeDefinition)child, objectNode, (Map<String, Object>)value);
if (child instanceof MapAttributeDefinition) {
processMapAttribute((MapAttributeDefinition) child, objectNode, (Map<String, Object>) value);
} else {
objectNode.get(child.getName()).set(processObjectAttribute((ObjectTypeAttributeDefinition) child, (Map<String, Object>) value));
}
Expand Down Expand Up @@ -484,11 +500,11 @@ private void processListAttribute(ListAttributeDefinition att, ModelNode list, O

@SuppressWarnings("unchecked")
private void processMapAttribute(MapAttributeDefinition att, ModelNode map, Map<String, Object> yaml) {
if(att instanceof ObjectMapAttributeDefinition) {
if (att instanceof ObjectMapAttributeDefinition) {
ObjectMapAttributeDefinition objectMapAtt = (ObjectMapAttributeDefinition) att;
ModelNode objectMapNode = map.get(att.getName()).setEmptyObject();
for (Map.Entry<String, Object> entry : yaml.entrySet()) {
ModelNode objectValue= processObjectAttribute(objectMapAtt.getValueType(), ( Map<String, Object>) entry.getValue());
ModelNode objectValue = processObjectAttribute(objectMapAtt.getValueType(), (Map<String, Object>) entry.getValue());
objectMapNode.get(entry.getKey()).set(objectValue);
}
} else {
Expand Down Expand Up @@ -519,18 +535,20 @@ private void processUnmanagedDeployments(ImmutableManagementResourceRegistration
OperationEntry operationEntry = rootRegistration.getOperationEntry(PathAddress.pathAddress("deployment", name), ADD);
if (deployment.getValue() != null && deployment.getValue() instanceof Map) {
Map<String, Object> attributes = (Map<String, Object>) deployment.getValue();
Map<String, Object> content = (Map<String, Object>) (((Iterable<? extends Object>)attributes.get("content")).iterator().next());
Map<String, Object> content = (Map<String, Object>) (((Iterable<? extends Object>) attributes.get("content")).iterator().next());
Set<String> result = content.keySet().stream().distinct().filter(MANAGED_CONTENT_ATTRIBUTES::contains).collect(Collectors.toSet());
if (!result.isEmpty()) {
throw MGMT_OP_LOGGER.unsupportedDeployment(name, result);
}
PathAddress address = PathAddress.pathAddress(DEPLOYMENT, name);
processAttributes(address, rootRegistration, operationEntry, attributes, postExtensionOps,xmlOperations);
processAttributes(address, rootRegistration, operationEntry, attributes, postExtensionOps, xmlOperations);
}
}

private interface Operation {

String getOperationName();

void processOperation(ImmutableManagementResourceRegistration rootRegistration, Map<PathAddress, ParsedBootOp> xmlOperations, List<ParsedBootOp> postExtensionOps, PathAddress address, String name);
}

Expand Down Expand Up @@ -615,7 +633,7 @@ public void processOperation(ImmutableManagementResourceRegistration rootRegistr
OperationEntry operationEntry = rootRegistration.getOperationEntry(address, getOperationName());
if (operationEntry != null) {
AttributeAccess access = rootRegistration.getAttributeAccess(address, name);
if(! (access.getAttributeDefinition() instanceof ListAttributeDefinition)) {
if (!(access.getAttributeDefinition() instanceof ListAttributeDefinition)) {
throw MGMT_OP_LOGGER.illegalOperationForAttribute(getOperationName(), name, address.toPathStyleString());
}
ListAttributeDefinition att = (ListAttributeDefinition) access.getAttributeDefinition();
Expand Down

0 comments on commit ea55b5c

Please sign in to comment.