From d83a82743070369878ffde4b9b4f9001a0884c5b Mon Sep 17 00:00:00 2001 From: JohnKeeney Date: Mon, 1 Jul 2019 14:46:49 +0000 Subject: ApexPDP: Adding support to omit the (context) albums entry in an apex policy model if it is empty. Implemented as a pair of JAXB XMLAdaters Updated copyright, fixed somecheckstyle issues, refactored affected tests & Merge Issue-ID: POLICY-1802 Change-Id: Id68d9a524700f44c164939b7533f2d511b591e36 Signed-off-by: JohnKeeney --- .../model/basicmodel/concepts/AxArtifactKey.java | 14 ++- .../apex/model/basicmodel/concepts/AxModel.java | 3 + .../basicmodel/handling/KeyInfoMarshalFilter.java | 104 +++++++++++++++++++ .../apex/model/basicmodel/concepts/AxKeyTest.java | 22 ++-- .../model/policymodel/concepts/AxPolicyModel.java | 112 ++++++++++++++------- .../policymodel/handling/EmptyAlbumsAdapter.java | 91 +++++++++++++++++ 6 files changed, 298 insertions(+), 48 deletions(-) create mode 100644 model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java create mode 100644 model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java (limited to 'model') diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java index ad0e3fe8f..86c89c35e 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java @@ -23,7 +23,6 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.util.ArrayList; import java.util.List; - import javax.persistence.Column; import javax.persistence.Embeddable; import javax.xml.bind.annotation.XmlAccessType; @@ -31,7 +30,6 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; - import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; @@ -184,6 +182,16 @@ public class AxArtifactKey extends AxKey { this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); } + /** + * Check if the key is IDENTICAL to a null key. + * + * @return true, if the key is IDENTICAL to a null key + */ + public boolean isNullKey() { + return this.getCompatibility(AxArtifactKey.getNullKey()).equals(AxKey.Compatibility.IDENTICAL); + } + + /** * {@inheritDoc}. */ @@ -352,4 +360,4 @@ public class AxArtifactKey extends AxKey { } return version.compareTo(other.version); } -} +} \ No newline at end of file diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java index 03c578e21..e54b88078 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java @@ -39,8 +39,10 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.basicmodel.handling.KeyInfoMarshalFilter; import org.onap.policy.apex.model.basicmodel.service.ModelService; import org.onap.policy.common.utils.validation.Assertions; @@ -79,6 +81,7 @@ public class AxModel extends AxConcept { @JoinColumns({ @JoinColumn(name = "keyInformationName", referencedColumnName = "name"), @JoinColumn(name = "keyInformationVersion", referencedColumnName = "version") }) @XmlElement(name = "keyInformation", required = true) + @XmlJavaTypeAdapter(KeyInfoMarshalFilter.class) private AxKeyInformation keyInformation; // @formatter:on diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java new file mode 100644 index 000000000..41cf4df89 --- /dev/null +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.model.basicmodel.handling; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; + +/** + * This class implements a filter to prevent some keyinfo information being marshalled when a model is serialised. + */ +public class KeyInfoMarshalFilter extends XmlAdapter { + + private List filterList = new LinkedList<>(); + + /** + * Adds a key to the list to be filtered. + * + * @param key the key to add to the filter list + */ + public void addFilterKey(AxKey key) { + filterList.add(key); + } + + /** + * Remove a key from the list to be filtered. + * + * @param key the key to remove from the filter list + * @return true if the passed key was in the filter list and has been removed. + */ + public boolean removeFilterKey(AxKey key) { + return filterList.remove(key); + } + + /** + * Adds some keys to the list to be filtered. + * + * @param keys the keys to add to the filter list + */ + public void addFilterKeys(Collection keys) { + filterList.addAll(keys); + } + + /** + * Decide whether to unmarshall some keyinfos - Always. + * + * @param val the keyinfo + * @return the keyinfo + * @throws Exception if there is some problem unmarshalling + * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object) + */ + @Override + public AxKeyInformation unmarshal(AxKeyInformation val) throws Exception { + return val; + } + + /** + * Select which keyinfo entries will be marshalled - i.e. those not in the filter list. + * + * @param val the keyinfo + * @return the keyinfo + * @throws Exception if there is some problem with the marshalling + * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object) + */ + @Override + public AxKeyInformation marshal(AxKeyInformation val) throws Exception { + if (val == null || val.getKeyInfoMap() == null || val.getKeyInfoMap().isEmpty() || filterList.isEmpty() ) { + return val; + } + //create a new keyinfo clone to avoid removing keyinfo entries from the original model + AxKeyInformation ret = new AxKeyInformation(val); + Map retmap = new TreeMap<>(ret.getKeyInfoMap()); + for (AxKey key : filterList) { + retmap.remove(key); + } + ret.setKeyInfoMap(retmap); + return ret; + } +} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java index c3c87b965..592933772 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java @@ -1,19 +1,20 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -27,13 +28,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.lang.reflect.Field; - import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; import org.onap.policy.apex.model.basicmodel.concepts.AxKey.Compatibility; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; public class AxKeyTest { @@ -124,6 +120,16 @@ public class AxKeyTest { assertFalse(someKey0.equals(null)); assertTrue(someKey0.equals(someKey0)); assertFalse(((AxKey) someKey0).equals(new AxReferenceKey())); + + AxArtifactKey nullKey0 = AxArtifactKey.getNullKey(); + assertTrue(nullKey0.isNullKey()); + AxArtifactKey nullKey1 = new AxArtifactKey(); + assertTrue(nullKey1.isNullKey()); + AxArtifactKey nullKey2 = new AxArtifactKey(AxKey.NULL_KEY_NAME,AxKey.NULL_KEY_VERSION); + assertTrue(nullKey2.isNullKey()); + AxArtifactKey notnullKey = new AxArtifactKey("Blah",AxKey.NULL_KEY_VERSION); + assertFalse(notnullKey.isNullKey()); + } diff --git a/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/concepts/AxPolicyModel.java b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/concepts/AxPolicyModel.java index ca7e7b335..3ad08a132 100644 --- a/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/concepts/AxPolicyModel.java +++ b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/concepts/AxPolicyModel.java @@ -25,19 +25,20 @@ import java.util.List; import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; - import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; - +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -47,6 +48,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.basicmodel.handling.KeyInfoMarshalFilter; import org.onap.policy.apex.model.basicmodel.service.ModelService; import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; @@ -55,6 +57,7 @@ import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.policymodel.handling.EmptyAlbumsAdapter; import org.onap.policy.common.utils.validation.Assertions; /** @@ -108,7 +111,7 @@ import org.onap.policy.common.utils.validation.Assertions; @XmlRootElement(name = "apexPolicyModel", namespace = "http://www.onap.org/policy/apex-pdp") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "AxPolicyModel", namespace = "http://www.onap.org/policy/apex-pdp", - propOrder = {"policies", "tasks", "events", "albums", "schemas"}) + propOrder = {"policies", "tasks", "events", "albums", "schemas"}) public class AxPolicyModel extends AxModel { private static final String DOES_NOT_EXIST = " does not exist"; @@ -137,7 +140,8 @@ public class AxPolicyModel extends AxModel { @OneToOne(cascade = CascadeType.ALL) @JoinColumns({@JoinColumn(name = "albumsName", referencedColumnName = "name"), @JoinColumn(name = "albumsVersion", referencedColumnName = "version")}) - @XmlElement(name = "albums", required = true) + @XmlElement(name = "albums", required = false ) + @XmlJavaTypeAdapter(EmptyAlbumsAdapter.class) private AxContextAlbums albums; @OneToOne(cascade = CascadeType.ALL) @@ -172,11 +176,11 @@ public class AxPolicyModel extends AxModel { */ public AxPolicyModel(final AxArtifactKey key) { this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())), - new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())), - new AxEvents(new AxArtifactKey(key.getName() + "_Events", key.getVersion())), - new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())), - new AxTasks(new AxArtifactKey(key.getName() + "_Tasks", key.getVersion())), - new AxPolicies(new AxArtifactKey(key.getName() + "_Policies", key.getVersion()))); + new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())), + new AxEvents(new AxArtifactKey(key.getName() + "_Events", key.getVersion())), + new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())), + new AxTasks(new AxArtifactKey(key.getName() + "_Tasks", key.getVersion())), + new AxPolicies(new AxArtifactKey(key.getName() + "_Policies", key.getVersion()))); } /** @@ -243,7 +247,7 @@ public class AxPolicyModel extends AxModel { */ public AxContextModel getContextModel() { return new AxContextModel(new AxArtifactKey(albums.getKey().getName() + "_Model", albums.getKey().getVersion()), - getSchemas(), getAlbums(), getKeyInformation()); + getSchemas(), getAlbums(), getKeyInformation()); } /** @@ -374,8 +378,8 @@ public class AxPolicyModel extends AxModel { for (final AxField field : event.getFields()) { if (getSchemas().get(field.getSchema()) == null) { result.addValidationMessage( - new AxValidationMessage(event.getKey(), this.getClass(), ValidationResult.INVALID, - "event field data type " + field.getSchema().getId() + DOES_NOT_EXIST)); + new AxValidationMessage(event.getKey(), this.getClass(), ValidationResult.INVALID, + "event field data type " + field.getSchema().getId() + DOES_NOT_EXIST)); } } } @@ -392,8 +396,8 @@ public class AxPolicyModel extends AxModel { for (final AxContextAlbum contextAlbum : albums.getAll(null)) { if (getSchemas().get(contextAlbum.getItemSchema()) == null) { result.addValidationMessage( - new AxValidationMessage(contextAlbum.getKey(), this.getClass(), ValidationResult.INVALID, - "context album schema " + contextAlbum.getItemSchema().getId() + DOES_NOT_EXIST)); + new AxValidationMessage(contextAlbum.getKey(), this.getClass(), ValidationResult.INVALID, + "context album schema " + contextAlbum.getItemSchema().getId() + DOES_NOT_EXIST)); } } return result; @@ -422,22 +426,21 @@ public class AxPolicyModel extends AxModel { private AxValidationResult validateTaskKeys(final AxTask task, AxValidationResult result) { for (final AxField field : task.getInputFieldSet()) { if (getSchemas().get(field.getSchema()) == null) { - result.addValidationMessage( - new AxValidationMessage(task.getKey(), this.getClass(), ValidationResult.INVALID, - "task input field schema " + field.getSchema().getId() + DOES_NOT_EXIST)); + result.addValidationMessage(new AxValidationMessage(task.getKey(), this.getClass(), + ValidationResult.INVALID, "task input field schema " + field.getSchema().getId() + DOES_NOT_EXIST)); } } for (final AxField field : task.getOutputFieldSet()) { if (getSchemas().get(field.getSchema()) == null) { result.addValidationMessage( - new AxValidationMessage(task.getKey(), this.getClass(), ValidationResult.INVALID, - "task output field schema " + field.getSchema().getId() + DOES_NOT_EXIST)); + new AxValidationMessage(task.getKey(), this.getClass(), ValidationResult.INVALID, + "task output field schema " + field.getSchema().getId() + DOES_NOT_EXIST)); } } for (final AxArtifactKey contextAlbumKey : task.getContextAlbumReferences()) { if (albums.get(contextAlbumKey) == null) { result.addValidationMessage(new AxValidationMessage(task.getKey(), this.getClass(), - ValidationResult.INVALID, "task context album " + contextAlbumKey.getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "task context album " + contextAlbumKey.getId() + DOES_NOT_EXIST)); } } return result; @@ -469,40 +472,40 @@ public class AxPolicyModel extends AxModel { for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) { if (albums.get(contextAlbumKey) == null) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "state context album " + contextAlbumKey.getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "state context album " + contextAlbumKey.getId() + DOES_NOT_EXIST)); } } final AxEvent triggerEvent = events.getEventMap().get(state.getTrigger()); if (triggerEvent == null) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "state trigger event " + state.getTrigger().getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "state trigger event " + state.getTrigger().getId() + DOES_NOT_EXIST)); } final AxTask defaultTask = tasks.getTaskMap().get(state.getDefaultTask()); if (defaultTask == null) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "state default task " + state.getDefaultTask().getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "state default task " + state.getDefaultTask().getId() + DOES_NOT_EXIST)); } // Check task input fields and event fields are compatible for default tasks with no task // selection logic - if (state.getTaskSelectionLogic().getKey().equals(AxReferenceKey.getNullKey()) && triggerEvent != null - && defaultTask != null) { + if (state.getTaskSelectionLogic().getKey().equals(AxReferenceKey.getNullKey()) + && triggerEvent != null && defaultTask != null) { final Set unhandledTaskInputFields = new TreeSet<>(defaultTask.getInputFieldSet()); unhandledTaskInputFields.removeAll(triggerEvent.getFields()); for (final AxField unhandledTaskInputField : unhandledTaskInputFields) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "task input field " + unhandledTaskInputField + " for task " - + defaultTask.getId() + " not in trigger event " + triggerEvent.getId())); + ValidationResult.INVALID, "task input field " + unhandledTaskInputField + " for task " + + defaultTask.getId() + " not in trigger event " + triggerEvent.getId())); } } for (final AxStateOutput stateOutput : state.getStateOutputs().values()) { if (events.getEventMap().get(stateOutput.getOutgingEvent()) == null) { result.addValidationMessage(new AxValidationMessage(stateOutput.getKey(), this.getClass(), - ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId() - + " for state output " + stateOutput.getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId() + + " for state output " + stateOutput.getId() + DOES_NOT_EXIST)); } } @@ -527,10 +530,10 @@ public class AxPolicyModel extends AxModel { final AxTask usedTask = tasks.getTaskMap().get(taskRefEntry.getKey()); if (usedTask == null) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "state task " + taskRefEntry.getKey().getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "state task " + taskRefEntry.getKey().getId() + DOES_NOT_EXIST)); } else { AxStateOutput stateOutput = - state.getStateOutputs().get(taskRefEntry.getValue().getOutput().getKey().getLocalName()); + state.getStateOutputs().get(taskRefEntry.getValue().getOutput().getKey().getLocalName()); validateEventTaskFieldCompatibilityOnStateOutput(state, usedTask, stateOutput, result); } } @@ -549,14 +552,14 @@ public class AxPolicyModel extends AxModel { final AxStateOutput stateOutput, AxValidationResult result) { if (stateOutput == null) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "state output on task reference for task " + task.getId() + " is null")); + ValidationResult.INVALID, "state output on task reference for task " + task.getId() + " is null")); } else { final AxEvent usedEvent = events.getEventMap().get(stateOutput.getOutgingEvent()); if (usedEvent == null) { result.addValidationMessage(new AxValidationMessage(stateOutput.getKey(), this.getClass(), - ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId() - + " for state output " + stateOutput.getId() + DOES_NOT_EXIST)); + ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId() + + " for state output " + stateOutput.getId() + DOES_NOT_EXIST)); } if (task != null && usedEvent != null) { @@ -564,13 +567,48 @@ public class AxPolicyModel extends AxModel { unhandledTaskOutputFields.removeAll(usedEvent.getFields()); for (final AxField unhandledTaskOutputField : unhandledTaskOutputFields) { result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(), - ValidationResult.INVALID, "task output field " + unhandledTaskOutputField + " for task " - + task.getId() + " not in output event " + usedEvent.getId())); + ValidationResult.INVALID, "task output field " + unhandledTaskOutputField + " for task " + + task.getId() + " not in output event " + usedEvent.getId())); } } } } + /** + * When a model is unmarshalled from disk or from the database, if the albums field was missing a blank + * with a null key was added. This method is called by JAXB after unmarshalling and is + * used to insert an appropriate key + * + * @param unmarshaller the unmarshaller that is unmarshalling the model + * @param parent the parent object of this object in the unmarshaller + */ + public void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) { + new EmptyAlbumsAdapter().doAfterUnmarshal(this); + } + + /** + * When a model is marshalled from disk or database, if the albums field is empty/null, then the albums field + * is not emitted. If the (empty) albums field is not emitted then it's keyinfo should also be suppressed + * This method is called by JAXB before marshaling and is used to insert the appropriate filters + * + * @param marshaller the marshaller that is marshaller the model + * @throws Exception if there is a problem with the marshalling + */ + public void beforeMarshal(final Marshaller marshaller) throws Exception { + EmptyAlbumsAdapter albumsfilter = new EmptyAlbumsAdapter(); + marshaller.setAdapter(EmptyAlbumsAdapter.class, albumsfilter); + //get/create the keyinfofilter + KeyInfoMarshalFilter keyinfoFilter = marshaller.getAdapter(KeyInfoMarshalFilter.class); + if (keyinfoFilter == null) { + keyinfoFilter = new KeyInfoMarshalFilter(); + } + //if the albumsfilter would filter out this model's albums add the album's key to the keyinfofilter + if (albumsfilter.marshal(this.albums) == null && this.albums != null) { + keyinfoFilter.addFilterKey(this.albums.getKey()); + } + marshaller.setAdapter(keyinfoFilter); + } + /** * {@inheritDoc}. */ @@ -711,4 +749,4 @@ public class AxPolicyModel extends AxModel { } return schemas.compareTo(other.schemas); } -} +} \ No newline at end of file diff --git a/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java new file mode 100644 index 000000000..aa19edf0b --- /dev/null +++ b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.model.policymodel.handling; + +import javax.xml.bind.annotation.adapters.XmlAdapter; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * This class makes the albums field optional in marshaled Policy Models. + * Empty albums are not marshaled to JSON/XML. + * When unmarshaled, if no albums value is present then a new empty albums entry is created. + * + * @author John Keeney (john.keeney@ericsson.com) + */ +public class EmptyAlbumsAdapter extends XmlAdapter { + + + /** + * Decide whether to marshall a context albums entry. Non-empty context albums are always marshalled. + * Empty albums are filtered. + * + * @param albums the albums entry + * @return the albums entry, or null if empty + * @throws Exception if there is a problem with the marshalling + * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object) + */ + @Override + public AxContextAlbums marshal(AxContextAlbums albums) throws Exception { + if ((albums == null) || (albums.getAlbumsMap() == null) || (albums.getAlbumsMap().isEmpty())) { + return null; + } else { + return albums; + } + } + + /** + * Decide whether to unmarshall a context albums entry - Always. + * + * @param val the albums entry + * @return the albums entry + * @throws Exception if there is a problem with the unmarshalling + * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object) + */ + @Override + public AxContextAlbums unmarshal(AxContextAlbums val) throws Exception { + return val; + } + + /** + * After unmarshalling has completed the model's context albums entry may be null/empty or default. + * If so the key for the albums entry should updated to a sensible value and additional keyinfo + * information should then be added for that key + * + * @param policyModel the policy model containing the possibly empty context albums entry + * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object) + */ + public void doAfterUnmarshal(AxPolicyModel policyModel) { + AxArtifactKey nullkey = new AxArtifactKey(); + AxArtifactKey blanknullalbumskey = + new AxArtifactKey(nullkey.getKey().getName() + "_Albums", nullkey.getKey().getVersion()); + AxArtifactKey thisalbumskey = policyModel.getAlbums().getKey(); + AxArtifactKey thismodelkey = policyModel.getKey(); + AxContextAlbums thismodelalbums = policyModel.getAlbums(); + + if (nullkey.equals(thisalbumskey) || blanknullalbumskey.equals(thisalbumskey)) { + thismodelalbums.setKey(new AxArtifactKey(thismodelkey.getName() + "_Albums", thismodelkey.getVersion())); + policyModel.getKeyInformation().generateKeyInfo(thismodelalbums); + } + } + +} -- cgit 1.2.3-korg