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 +++-- 4 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java (limited to 'model/basic-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()); + } -- cgit 1.2.3-korg