/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.
* 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=========================================================
*/
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;
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;
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.utilities.Assertions;
+import org.onap.policy.apex.model.policymodel.handling.EmptyAlbumsAdapter;
+import org.onap.policy.common.utils.validation.Assertions;
/**
* A container class for an Apex policy model. This class is a container class that allows an Apex
* required to run policies in Apex. The model contains schema definitions, definitions of events
* and context albums that use those schemas, definitions of tasks for policies and definitions of
* the policies themselves.
- *
+ *
* <p>An Apex policy model is an important artifact in Apex. At editing time, an Apex editor creates
* and edits a policy model and a policy model is loaded into and is executed by an Apex engine.
* Therefore, an Apex model and the set of policies that it holds is the way that the policy domain
* that an Apex engine or a group of Apex engines executes across is expressed, both at design time
* and run time. The Apex deployment system is responsible for deploying Apex models to and the
* context they need the appropriate engines for execution.
- *
+ *
* <p>Model registration is carried out by calling the {@code register()} method, which registers the
* policy model and all its constituent containers with the model service. The containers for
* context schemas, events, context albums, tasks, policies, and key information are all registered.
- *
+ *
* <p>Once a policy model is composed, the overall structure of the policy model and all its references
* can be validated. During validation of a policy model, the validation checks listed below are
* executed:
@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";
// @formatter:off
@OneToOne(cascade = CascadeType.ALL)
- @JoinColumns({@JoinColumn(name = "policiesName", referencedColumnName = "name"),
- @JoinColumn(name = "policiesVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "policiesName", referencedColumnName = "name")
+ @JoinColumn(name = "policiesVersion", referencedColumnName = "version")
@XmlElement(name = "policies", required = true)
private AxPolicies policies;
@OneToOne(cascade = CascadeType.ALL)
- @JoinColumns({@JoinColumn(name = "tasksName", referencedColumnName = "name"),
- @JoinColumn(name = "tasksVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "tasksName", referencedColumnName = "name")
+ @JoinColumn(name = "tasksVersion", referencedColumnName = "version")
@XmlElement(name = "tasks", required = true)
private AxTasks tasks;
@OneToOne(cascade = CascadeType.ALL)
- @JoinColumns({@JoinColumn(name = "eventsName", referencedColumnName = "name"),
- @JoinColumn(name = "eventsVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "eventsName", referencedColumnName = "name")
+ @JoinColumn(name = "eventsVersion", referencedColumnName = "version")
@XmlElement(name = "events", required = true)
private AxEvents events;
@OneToOne(cascade = CascadeType.ALL)
- @JoinColumns({@JoinColumn(name = "albumsName", referencedColumnName = "name"),
- @JoinColumn(name = "albumsVersion", referencedColumnName = "version")})
- @XmlElement(name = "albums", required = true)
+ @JoinColumn(name = "albumsName", referencedColumnName = "name")
+ @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
+ @XmlElement(name = "albums", required = false)
+ @XmlJavaTypeAdapter(EmptyAlbumsAdapter.class)
private AxContextAlbums albums;
@OneToOne(cascade = CascadeType.ALL)
- @JoinColumns({@JoinColumn(name = "schemasName", referencedColumnName = "name"),
- @JoinColumn(name = "schemasVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "schemasName", referencedColumnName = "name")
+ @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
@XmlElement(name = "schemas", required = true)
private AxContextSchemas schemas;
// @formatter:on
/**
* Copy constructor.
- *
+ *
* @param copyConcept the concept to copy from
*/
public AxPolicyModel(final AxPolicyModel copyConcept) {
*/
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())));
}
/**
this.policies = policies;
}
- /*
- * (non-Javadoc)
- *
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#register()
+ /**
+ * {@inheritDoc}.
*/
@Override
public void register() {
ModelService.registerModel(AxPolicyModel.class, this);
}
- /*
- * (non-Javadoc)
- *
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#getKeys()
+ /**
+ * {@inheritDoc}.
*/
@Override
public List<AxKey> getKeys() {
*/
public AxContextModel getContextModel() {
return new AxContextModel(new AxArtifactKey(albums.getKey().getName() + "_Model", albums.getKey().getVersion()),
- getSchemas(), getAlbums(), getKeyInformation());
+ getSchemas(), getAlbums(), getKeyInformation());
}
/**
this.schemas = schemas;
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.onap.policy.apex.model.basicmodel.concepts.AxModel#validate(org.onap.policy.apex.model.
- * basicmodel.concepts.AxValidationResult)
+ /**
+ * {@inheritDoc}.
*/
@Override
public AxValidationResult validate(final AxValidationResult resultIn) {
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));
}
}
}
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;
/**
* Validate all fundamental concepts keyed in tasks exist.
- *
+ *
* @param task The task to validate the keys of
* @param result the validation result to return
* @return the result
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;
/**
* Validate that the references used on a state are valid.
- *
+ *
* @param state The state to check
* @param result the validation result to append to
*/
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));
}
}
/**
* Validate that the fields on tasks and events that trigger them and are output by them are
* compatible for all tasks used on a state.
- *
+ *
* @param state The state to check
* @param result the validation result to append to
*/
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);
}
}
/**
* Validate that the fields on a task of a state output and the events that trigger it are
* compatible.
- *
+ *
* @param state The state to check
* @param task The task to check
* @param stateOutput The state output to check
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) {
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()));
}
}
}
}
- /*
- * (non-Javadoc)
+ /**
+ * 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
*
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#clean()
+ * @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
public void clean() {
schemas.clean();
}
- /*
- * (non-Javadoc)
- *
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#toString()
+ /**
+ * {@inheritDoc}.
*/
@Override
public String toString() {
return builder.toString();
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
- * basicmodel.concepts.AxConcept)
+ /**
+ * {@inheritDoc}.
*/
@Override
public AxConcept copyTo(final AxConcept targetObject) {
return copy;
}
- /*
- * (non-Javadoc)
- *
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#hashCode()
+ /**
+ * {@inheritDoc}.
*/
@Override
public int hashCode() {
return result;
}
- /*
- * (non-Javadoc)
- *
- * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#equals(java.lang.Object)
+ /**
+ * {@inheritDoc}.
*/
@Override
public boolean equals(final Object obj) {
return schemas.equals(other.schemas);
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.onap.policy.apex.model.basicmodel.concepts.AxModel#compareTo(org.onap.policy.apex.model.
- * basicmodel.concepts.AxConcept)
+ /**
+ * {@inheritDoc}.
*/
@Override
public int compareTo(final AxConcept otherObj) {