diff options
author | liamfallon <liam.fallon@est.tech> | 2019-03-25 08:09:14 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-03-25 08:09:14 +0000 |
commit | bde47d7867610f36042d3ce1440c1598d1248fcc (patch) | |
tree | 41824c940daa78c00a6c4b8f75b19faccd4638af /models-tosca/src/main | |
parent | 2a245ef80e39a101015efb164de53f1753fa5d47 (diff) |
Add provider for Tosca Policies
Provider working from JAVA API call through to database and
back for TOSCA policies with full unit test.
Issue-ID: POLICY-1195
Change-Id: I82cf3b513b4921dcb2e6726856aa4fbeb7d0d816
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main')
7 files changed, 193 insertions, 22 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java index cbf0e3844..eae98a1bc 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java @@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey; public class ToscaDataTypes extends PfConceptContainer<ToscaDataType> { private static final long serialVersionUID = 2941102271022190348L; + public static final String DEFAULT_NAME = "ToscaDataTypesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + /** * The Default Constructor creates a {@link ToscaDataTypes} object with a null artifact key * and creates an empty concept map. */ public ToscaDataTypes() { - super(new PfConceptKey()); + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java index 9dd12ec88..ee9c2eae5 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java @@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey; public class ToscaPolicies extends PfConceptContainer<ToscaPolicy> { private static final long serialVersionUID = -7526648702327776101L; + public static final String DEFAULT_NAME = "ToscaPoliciessSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + /** * The Default Constructor creates a {@link ToscaPolicies} object with a null artifact key and * creates an empty concept map. */ public ToscaPolicies() { - super(new PfConceptKey()); + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java index 23159ccfa..277291364 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java @@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey; public class ToscaPolicyTypes extends PfConceptContainer<ToscaPolicyType> { private static final long serialVersionUID = -4157979965271220098L; + public static final String DEFAULT_NAME = "ToscaPolicyTypesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + /** * The Default Constructor creates a {@link ToscaPolicyTypes} object with a null artifact key * and creates an empty concept map. */ public ToscaPolicyTypes() { - super(new PfConceptKey()); + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java index fd8b134ce..c3bb9165e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java @@ -60,6 +60,9 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; public class ToscaServiceTemplate extends ToscaEntityType { private static final long serialVersionUID = 8084846046148349401L; + public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + @Column @SerializedName("tosca_definitions_version") private String toscaDefinitionsVersion; @@ -80,7 +83,7 @@ public class ToscaServiceTemplate extends ToscaEntityType { * The Default Constructor creates a {@link ToscaServiceTemplate} object with a null key. */ public ToscaServiceTemplate() { - this(new PfConceptKey()); + this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java index 9d156c3df..35381ec49 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java @@ -45,8 +45,8 @@ import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; /** - * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA - * topology template are implemented. + * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are + * implemented. * * @author Liam Fallon (liam.fallon@est.tech) */ @@ -58,6 +58,8 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; public class ToscaTopologyTemplate extends PfConcept { private static final long serialVersionUID = 8969698734673232603L; + public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple"; + @EmbeddedId private PfReferenceKey key; @@ -71,12 +73,12 @@ public class ToscaTopologyTemplate extends PfConcept { * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key. */ public ToscaTopologyTemplate() { - this(new PfReferenceKey()); + this(new PfReferenceKey(ToscaServiceTemplate.DEFAULT_NAME, ToscaServiceTemplate.DEFAULT_VERSION, + DEFAULT_LOCAL_NAME)); } /** - * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept - * key. + * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept key. * * @param key the key */ diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index 3d563a1cc..2240ef099 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -20,12 +20,22 @@ package org.onap.policy.models.tosca.simple.provider; +import javax.ws.rs.core.Response; + import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class provides the provision of information on TOSCA concepts in the database to callers. @@ -33,6 +43,8 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; * @author Liam Fallon (liam.fallon@est.tech) */ public class SimpleToscaProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); + /** * Get policy types. * @@ -44,7 +56,21 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) throws PfModelException { - return null; + + // Create the structure of the TOSCA service template to contain the policy type + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setPolicyTypes(new ToscaPolicyTypes()); + + // Add the policy type to the TOSCA service template + ToscaPolicyType policyType = dao.get(ToscaPolicyType.class, policyTypeKey); + if (policyType != null) { + serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType); + return serviceTemplate; + } else { + String errorMessage = "policy type not found: " + policyTypeKey.getId(); + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } } /** @@ -57,7 +83,24 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + + assertPolicyTypesExist(serviceTemplate); + + for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + dao.create(policyType); + } + + // Return the created policy types + ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes(); + + for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) { + returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey)); + } + + ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate(); + returnServiceTemplate.setPolicyTypes(returnPolicyTypes); + + return returnServiceTemplate; } /** @@ -70,7 +113,24 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + + assertPolicyTypesExist(serviceTemplate); + + for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + dao.update(policyType); + } + + // Return the created policy types + ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes(); + + for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) { + returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey)); + } + + ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate(); + returnServiceTemplate.setPolicyTypes(returnPolicyTypes); + + return returnServiceTemplate; } /** @@ -84,7 +144,12 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) throws PfModelException { - return null; + + ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey); + + dao.delete(ToscaPolicyType.class, policyTypeKey); + + return serviceTemplate; } /** @@ -98,7 +163,22 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) throws PfModelException { - return null; + + // Create the structure of the TOSCA service template to contain the policy type + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + + // Add the policy to the TOSCA service template + ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey); + if (policy != null) { + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy); + return serviceTemplate; + } else { + String errorMessage = "policy not found: " + policyKey.getId(); + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } } /** @@ -111,7 +191,24 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + + assertPoliciesExist(serviceTemplate); + + for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { + dao.create(policy); + } + + // Return the created policy types + ToscaPolicies returnPolicies = new ToscaPolicies(); + returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey()); + + for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) { + returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey)); + } + + serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies); + + return serviceTemplate; } /** @@ -124,7 +221,24 @@ public class SimpleToscaProvider { */ public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + + assertPoliciesExist(serviceTemplate); + + for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { + dao.update(policy); + } + + // Return the created policy types + ToscaPolicies returnPolicies = new ToscaPolicies(); + returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey()); + + for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) { + returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey)); + } + + serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies); + + return serviceTemplate; } /** @@ -132,11 +246,56 @@ public class SimpleToscaProvider { * * @param dao the DAO to use to access the database * @param policyKey the policy key - * @return the TOSCA service template containing the policy types that were deleted + * @return the TOSCA service template containing the policies that were deleted * @throws PfModelException on errors deleting policies */ public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) throws PfModelException { - return null; + + ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey); + + dao.delete(ToscaPolicy.class, policyKey); + + return serviceTemplate; + } + + /** + * Check if policy types have been specified is initialized. + */ + private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getPolicyTypes() == null) { + String errorMessage = "no policy types specified on service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { + String errorMessage = "list of policy types specified on service template is empty"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + } + + /** + * Check if policy types have been specified is initialized. + */ + private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getTopologyTemplate() == null) { + String errorMessage = "topology template not specified on service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { + String errorMessage = "no policies specified on topology template of service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { + String errorMessage = "list of policies specified on topology template of service template is empty"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java index 613b0e292..40fe63c74 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java @@ -32,7 +32,6 @@ import java.lang.reflect.Type; import lombok.NonNull; -import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; @@ -56,8 +55,7 @@ public class ToscaServiceTemplateJsonAdapter final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject(); // The outgoing object - final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1"); - final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey); + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); serviceTemplate .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString()); @@ -67,7 +65,7 @@ public class ToscaServiceTemplateJsonAdapter } // Set the parent key of the topology template to be this service template - serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey); + serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplate.getKey()); return serviceTemplate; } |