From 632899642b6a1d1489a0b96932c9c38863a53a26 Mon Sep 17 00:00:00 2001 From: Rashmi Pujar Date: Sat, 22 Jan 2022 00:37:28 -0500 Subject: Add TOSCA policy status header field to APEX event create CLI In order to expose the processing status of a TOSCA policy to APEX-PDP, a new header field for APEX concept "event" has been introduced to track this within an APEX policy implementation. This field will be leveraged to extract the TOSCA policy execution metrics. Note, that the field is marked as optional for backward compatibility. Unit tests have been augmented to test for the field wherever applicable and the default empty value is retained for the others. "example-grpc" module has been updated to include this field. Exposing the TOSCA policy execution metrics is outside the scope of this patch since the current changes are already very large owing to the atomic nature of the change introduced. Issue-ID: POLICY-3845 Signed-off-by: Rashmi Pujar Change-Id: Ief6d70f9abcfc8414e10aa51a27815ee9028e4c8 --- .../protocol/yaml/Apex2YamlEventConverter.java | 25 +++++++++++++++++++--- .../protocol/yaml/YamlEventProtocolParameters.java | 16 ++++++++------ .../protocol/yaml/YamlPluginStabilityTest.java | 8 +++++-- 3 files changed, 37 insertions(+), 12 deletions(-) (limited to 'plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml') diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java index 9359e4ef9..731d3519a 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. - * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -257,7 +257,9 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { String target = getHeaderTarget(yamlMap, eventDefinition); - return new ApexEvent(name, version, namespace, source, target); + String toscaPolicyState = getHeaderToscaPolicyState(yamlMap, eventDefinition); + + return new ApexEvent(name, version, namespace, source, target, toscaPolicyState); } /** @@ -317,6 +319,23 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { return target; } + /** + * Get the event header toscaPolicyState field. + * + * @param yamlMap the YAML map to read from + * @param eventDefinition the event definition + * @return the event header toscaPolicyState field + */ + private String getHeaderToscaPolicyState(final Map yamlMap, final AxEvent eventDefinition) { + // For toscaPolicyState, use the defined value only if not found on the incoming event + var toscaPolicyState = getYamlStringField(yamlMap, ApexEvent.TOSCA_POLICY_STATE_HEADER_FIELD, + yamlPars.getToscaPolicyStateAlias(), null, false); + if (toscaPolicyState == null) { + toscaPolicyState = eventDefinition.getTarget(); + } + return toscaPolicyState; + } + /** * This method gets an event string field from a JSON object. * @@ -391,4 +410,4 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { return eventElement; } -} +} \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java index e5539cd04..120104460 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,12 +62,13 @@ public class YamlEventProtocolParameters extends EventProtocolTextTokenDelimited // Aliases for Apex event header fields // @formatter:off - private String nameAlias = null; - private String versionAlias = null; - private String nameSpaceAlias = null; - private String sourceAlias = null; - private String targetAlias = null; - private String yamlFieldName = DEFAULT_YAML_FIELD_NAME; + private String nameAlias = null; + private String versionAlias = null; + private String nameSpaceAlias = null; + private String sourceAlias = null; + private String targetAlias = null; + private String toscaPolicyStateAlias = null; + private String yamlFieldName = DEFAULT_YAML_FIELD_NAME; // @formatter:on /** @@ -96,4 +98,4 @@ public class YamlEventProtocolParameters extends EventProtocolTextTokenDelimited // Set the event protocol plugin class this.setEventProtocolPluginClass(Apex2YamlEventConverter.class.getName()); } -} +} \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java index ab911499a..0d3d62382 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java @@ -3,6 +3,7 @@ * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +36,7 @@ import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxToscaPolicyProcessingStatus; import org.onap.policy.apex.model.basicmodel.service.ModelService; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; @@ -80,6 +82,7 @@ public class YamlPluginStabilityTest { testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1")); testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + testEvent.setToscaPolicyState(AxToscaPolicyProcessingStatus.ENTRY.name()); AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey()); testEvent.getParameterMap().put("intValue", teField0); AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"), @@ -130,7 +133,8 @@ public class YamlPluginStabilityTest { assertThatThrownBy(() -> converter.fromApexEvent(null)) .hasMessage("event processing failed, Apex event is null"); ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), - testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); + testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget(), + testEvent.getToscaPolicyState()); apexEvent.put("doubleValue", 123.45); apexEvent.put("intValue", 123); apexEvent.put("stringValue", "123.45"); @@ -216,4 +220,4 @@ public class YamlPluginStabilityTest { assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString")).getCause().hasMessageStartingWith("mandatory field \"intValue\" is missing"); } -} +} \ No newline at end of file -- cgit 1.2.3-korg