From ea55b5ca9d71836292239226c7a01132d05f9d2c Mon Sep 17 00:00:00 2001 From: Emmanuel Hugonnet Date: Tue, 19 Mar 2024 09:33:54 +0100 Subject: [PATCH] [WFCORE-6503]: Ignoring removal of unexisting resource Signed-off-by: Emmanuel Hugonnet --- .../yaml/YamlConfigurationExtension.java | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/controller/src/main/java/org/jboss/as/controller/persistence/yaml/YamlConfigurationExtension.java b/controller/src/main/java/org/jboss/as/controller/persistence/yaml/YamlConfigurationExtension.java index 4cbf8e69eb3..db1de2a2a63 100644 --- a/controller/src/main/java/org/jboss/as/controller/persistence/yaml/YamlConfigurationExtension.java +++ b/controller/src/main/java/org/jboss/as/controller/persistence/yaml/YamlConfigurationExtension.java @@ -104,7 +104,7 @@ private void load() { for (Path file : files) { if (file != null && Files.exists(file) && Files.isRegularFile(file)) { Map 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) { @@ -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) value); + if (value != null && value instanceof Map && DEPLOYMENT.equals(excluded)) { + deployments.putAll((Map) 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); @@ -162,7 +162,7 @@ public void processOperations(ImmutableManagementResourceRegistration rootRegist for (Map config : configs) { processResource(PathAddress.EMPTY_ADDRESS, new HashMap<>(config), rootRegistration, rootRegistration, xmlOperations, postExtensionOps, false); } - for(Map.Entry deployment : deployments.entrySet()) { + for (Map.Entry deployment : deployments.entrySet()) { processUnmanagedDeployments(rootRegistration, deployment, xmlOperations, postExtensionOps); } this.configs.clear(); @@ -197,7 +197,7 @@ private void processResource(PathAddress parentAddress, Map 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()); @@ -209,10 +209,10 @@ private void processResource(PathAddress parentAddress, Map 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)) { @@ -233,13 +233,13 @@ private void processResource(PathAddress parentAddress, Map yaml Object value = yaml.get(name); if (value instanceof Map) { Map map = (Map) 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); @@ -271,8 +271,8 @@ private void processResource(PathAddress parentAddress, Map yaml } else { if (!postExtensionOps.isEmpty()) { ParsedBootOp op = postExtensionOps.get(postExtensionOps.size() - 1); - if (! address.equals(op.getAddress())) { // else already processed - Map map = value instanceof Map ? new HashMap<>((Map)value) : new HashMap<>(yaml); + if (!address.equals(op.getAddress())) { // else already processed + Map 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); @@ -292,7 +292,15 @@ private void processResource(PathAddress parentAddress, Map 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())) { @@ -304,7 +312,15 @@ private void processResource(PathAddress parentAddress, Map 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); } @@ -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()); @@ -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); } } @@ -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)value); + if (child instanceof MapAttributeDefinition) { + processMapAttribute((MapAttributeDefinition) child, objectNode, (Map) value); } else { objectNode.get(child.getName()).set(processObjectAttribute((ObjectTypeAttributeDefinition) child, (Map) value)); } @@ -484,11 +500,11 @@ private void processListAttribute(ListAttributeDefinition att, ModelNode list, O @SuppressWarnings("unchecked") private void processMapAttribute(MapAttributeDefinition att, ModelNode map, Map yaml) { - if(att instanceof ObjectMapAttributeDefinition) { + if (att instanceof ObjectMapAttributeDefinition) { ObjectMapAttributeDefinition objectMapAtt = (ObjectMapAttributeDefinition) att; ModelNode objectMapNode = map.get(att.getName()).setEmptyObject(); for (Map.Entry entry : yaml.entrySet()) { - ModelNode objectValue= processObjectAttribute(objectMapAtt.getValueType(), ( Map) entry.getValue()); + ModelNode objectValue = processObjectAttribute(objectMapAtt.getValueType(), (Map) entry.getValue()); objectMapNode.get(entry.getKey()).set(objectValue); } } else { @@ -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 attributes = (Map) deployment.getValue(); - Map content = (Map) (((Iterable)attributes.get("content")).iterator().next()); + Map content = (Map) (((Iterable) attributes.get("content")).iterator().next()); Set 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 xmlOperations, List postExtensionOps, PathAddress address, String name); } @@ -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();