summaryrefslogtreecommitdiffstats
path: root/model/policy-model/src/main
diff options
context:
space:
mode:
authorJohnKeeney <John.Keeney@est.tech>2019-07-01 14:46:49 +0000
committerJohnKeeney <John.Keeney@est.tech>2019-07-01 14:46:49 +0000
commitd83a82743070369878ffde4b9b4f9001a0884c5b (patch)
tree2840f803e197cbf00c742f9522a051d7286beca8 /model/policy-model/src/main
parentc4baac8a79b0ca6340693ae47f9c424f3612e3bd (diff)
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 <John.Keeney@est.tech>
Diffstat (limited to 'model/policy-model/src/main')
-rw-r--r--model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/concepts/AxPolicyModel.java112
-rw-r--r--model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java91
2 files changed, 166 insertions, 37 deletions
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<AxField> 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,14 +567,49 @@ 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}.
*/
@Override
@@ -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<AxContextAlbums, AxContextAlbums> {
+
+
+ /**
+ * 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);
+ }
+ }
+
+}