aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-02-03 14:37:33 +0000
committerliamfallon <liam.fallon@est.tech>2020-02-04 12:47:09 +0000
commit70dbf7a387b0e5d00f50a954751c4d4b4e54d7e2 (patch)
tree129e5118d695802d378cd5924b7aa1e5ee043891 /models-tosca/src/main/java
parent172a67f92943d64d5a1eda2b1dabf6c1c1c77f7e (diff)
Add merge utility for service templates
This utility function allows a TOSCA service template fragment to be merged with an exsiting service template and will be tbe backbone of "create" methods on the API from now on. Issue-ID: POLICY-1402 Change-Id: I3381cb2a1bd30621a639dedc213c546eeb2bf9aa Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java44
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java30
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java152
3 files changed, 201 insertions, 25 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
index 31c7df047..f6139ab2d 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
@@ -277,26 +277,13 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
@Override
public int compareTo(final PfConcept otherConcept) {
- if (otherConcept == null) {
- return -1;
- }
- if (this == otherConcept) {
- return 0;
- }
- if (getClass() != otherConcept.getClass()) {
- return getClass().getName().compareTo(otherConcept.getClass().getName());
- }
-
- final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
- if (!super.equals(other)) {
- return super.compareTo(other);
- }
-
- int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
+ int result = compareToWithoutEntities(otherConcept);
if (result != 0) {
return result;
}
+ final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
+
result = ObjectUtils.compare(dataTypes, other.dataTypes);
if (result != 0) {
return result;
@@ -309,4 +296,29 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
}
+
+ /**
+ * Compare this service template to another service template, ignoring contained entitites.
+ *
+ * @param otherConcept the other topology template
+ * @return the result of the comparison
+ */
+ public int compareToWithoutEntities(final PfConcept otherConcept) {
+ if (otherConcept == null) {
+ return -1;
+ }
+ if (this == otherConcept) {
+ return 0;
+ }
+ if (getClass() != otherConcept.getClass()) {
+ return getClass().getName().compareTo(otherConcept.getClass().getName());
+ }
+
+ final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
+ if (!super.equals(other)) {
+ return super.compareTo(other);
+ }
+
+ return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java
index 5fd1a498c..c8dfa5a36 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
package org.onap.policy.models.tosca.simple.concepts;
import java.util.List;
+
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
@@ -31,9 +32,11 @@ import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
import javax.persistence.Table;
+
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+
import org.apache.commons.lang3.ObjectUtils;
import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
@@ -78,8 +81,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
}
/**
- * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept
- * key.
+ * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
*
* @param key the key
*/
@@ -178,6 +180,21 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
@Override
public int compareTo(final PfConcept otherConcept) {
+ int result = compareToWithoutEntities(otherConcept);
+ if (result != 0) {
+ return result;
+ }
+
+ return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
+ }
+
+ /**
+ * Compare this topology template to another topology template, ignoring contained entitites.
+ *
+ * @param otherConcept the other topology template
+ * @return the result of the comparison
+ */
+ public int compareToWithoutEntities(final PfConcept otherConcept) {
if (otherConcept == null) {
return -1;
}
@@ -193,11 +210,6 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
return key.compareTo(other.key);
}
- int result = ObjectUtils.compare(description, other.description);
- if (result != 0) {
- return result;
- }
-
- return ObjectUtils.compare(policies, other.policies);
+ return ObjectUtils.compare(description, other.description);
}
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java
new file mode 100644
index 000000000..343e02300
--- /dev/null
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java
@@ -0,0 +1,152 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 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.models.tosca.utils;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.ws.rs.core.Response;
+
+import lombok.NonNull;
+
+import org.onap.policy.models.base.PfConceptContainer;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.base.PfValidationMessage;
+import org.onap.policy.models.base.PfValidationResult;
+import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This utility class provides methods to manage service templates.
+ */
+public class ToscaServiceTemplateUtils {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateUtils.class);
+
+ /**
+ * Private constructor to prevent subclassing.
+ */
+ private ToscaServiceTemplateUtils() {
+ // Private constructor to prevent subclassing
+ }
+
+ /**
+ * Add a service template fragment to a service template. All entities in the service template fragment must either
+ * a) not exist on the original service template or b) be identical to entities on the original service template.
+ *
+ * @param originalTemplate the original service template
+ * @param fragmentTemplate the fragment being added to the original service template
+ * @return
+ */
+ public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
+ @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
+ PfValidationResult result = new PfValidationResult();
+
+ if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
+ result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(),
+ ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
+ "service template in incoming fragment does not equal existing service template"));
+ }
+
+ JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
+
+ compositeTemplate.setDataTypes(
+ addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
+ compositeTemplate.setPolicyTypes(
+ addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
+
+ if (originalTemplate.getTopologyTemplate() != null) {
+ if (originalTemplate.getTopologyTemplate()
+ .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) {
+ compositeTemplate.getTopologyTemplate()
+ .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(),
+ fragmentTemplate.getTopologyTemplate().getPolicies(), result));
+ } else {
+ result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
+ ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
+ "topology template in incoming fragment does not equal existing topology template"));
+ }
+ } else if (fragmentTemplate.getTopologyTemplate() != null) {
+ compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
+ }
+
+ if (result.isValid()) {
+ result = compositeTemplate.validate(result);
+ }
+
+ if (!result.isValid()) {
+ String message = result.toString();
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
+ }
+
+ return compositeTemplate;
+ }
+
+ /**
+ * Check entities from a fragment container can be added to an original container.
+ *
+ * @param <S> The type of container
+ *
+ * @param compositeContainer the original container
+ * @param fragmentContainer the fragment being added to the original container
+ * @return the composite container with the result
+ */
+ @SuppressWarnings("unchecked")
+ // @formatter:off
+ private static
+ <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
+ S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
+ final PfValidationResult result) {
+
+ if (compositeContainer == null) {
+ return fragmentContainer;
+ }
+
+ if (fragmentContainer == null) {
+ return compositeContainer;
+ }
+
+ for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
+ .getConceptMap().entrySet()) {
+ JpaToscaEntityType<? extends ToscaEntity> containerEntry =
+ compositeContainer.getConceptMap().get(fragmentEntry.getKey());
+ if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) {
+ result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(),
+ ToscaServiceTemplateUtils.class,
+ ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity"));
+ }
+ }
+
+ // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
+ // original map
+ @SuppressWarnings("rawtypes")
+ Map originalContainerMap = compositeContainer.getConceptMap();
+ originalContainerMap.putAll(fragmentContainer.getConceptMap());
+
+ return compositeContainer;
+ }
+ // @formatter:on
+}