Fix return codes and policy get
Change the return codes for validation to return more helpful and
correct codes.
Fix the policy "get" request to get policy types and data types for a
policy get.
Issue-ID: POLICY-1402
Change-Id: I89d67f830e9a75bdeef39c4142523c718879f19c
Signed-off-by: liamfallon <liam.fallon@est.tech>
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
index 0cdc2ad..3116868 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
@@ -36,6 +36,7 @@
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProvider;
@@ -44,7 +45,8 @@
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.yaml.snakeyaml.Yaml;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Test persistence of monitoring policies to and from the database.
@@ -52,7 +54,10 @@
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class PolicyToscaPersistenceTest {
- private StandardCoder standardCoder;
+ private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class);
+
+ private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
+ private StandardCoder standardCoder = new StandardCoder();
private PolicyModelsProvider databaseProvider;
@@ -78,14 +83,6 @@
createPolicyTypes();
}
- /**
- * Set up standard coder.
- */
- @Before
- public void setupStandardCoder() {
- standardCoder = new StandardCoder();
- }
-
@After
public void teardown() throws Exception {
databaseProvider.close();
@@ -103,29 +100,44 @@
String policyString = ResourceUtils.getResourceAsString(policyResource);
if (policyResource.endsWith("yaml")) {
- testYamlStringPolicyPersistence(policyString);
+ testPolicyPersistence(yamlJsonTranslator.fromYaml(policyString, ToscaServiceTemplate.class));
} else {
- testJsonStringPolicyPersistence(policyString);
+ testPolicyPersistence(standardCoder.decode(policyString, ToscaServiceTemplate.class));
}
}
}
- private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
- Object yamlObject = new Yaml().load(policyString);
- String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+ @Test
+ public void testNamingPolicyGet() throws PfModelException {
+ String policyYamlString = ResourceUtils.getResourceAsString("policies/sdnc.policy.naming.input.tosca.yaml");
+ ToscaServiceTemplate serviceTemplate =
+ yamlJsonTranslator.fromYaml(policyYamlString, ToscaServiceTemplate.class);
- testJsonStringPolicyPersistence(yamlAsJsonString);
+ long createStartTime = System.currentTimeMillis();
+ databaseProvider.createPolicies(serviceTemplate);
+ LOGGER.trace("Naming policy create time (ms): {}", System.currentTimeMillis() - createStartTime);
+
+ long getStartTime = System.currentTimeMillis();
+ ToscaServiceTemplate namingServiceTemplate =
+ databaseProvider.getPolicies("SDNC_Policy.ONAP_VNF_NAMING_TIMESTAMP", "1.0.0");
+ LOGGER.trace("Naming policy get time (ms): {}", System.currentTimeMillis() - getStartTime);
+
+ assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
+ assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
+ assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
+
+ long deleteStartTime = System.currentTimeMillis();
+ databaseProvider.deletePolicy("SDNC_Policy.ONAP_VNF_NAMING_TIMESTAMP", "1.0.0");
+ LOGGER.trace("Naming policy delete time (ms): {}", System.currentTimeMillis() - deleteStartTime);
}
/**
* Check persistence of a policy.
*
- * @param policyString the policy as a string
+ * @param serviceTemplate the service template containing the policy
* @throws Exception any exception thrown
*/
- public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
- ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
-
+ public void testPolicyPersistence(@NonNull final ToscaServiceTemplate serviceTemplate) throws Exception {
assertNotNull(serviceTemplate);
databaseProvider.createPolicies(serviceTemplate);
@@ -170,11 +182,9 @@
Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
for (String policyTypeResource : policyTypeResources) {
- Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
- String yamlAsJsonString = new StandardCoder().encode(yamlObject);
-
+ String policyTypeYamlString = ResourceUtils.getResourceAsString(policyTypeResource);
ToscaServiceTemplate toscaServiceTemplatePolicyType =
- standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+ yamlJsonTranslator.fromYaml(policyTypeYamlString, ToscaServiceTemplate.class);
assertNotNull(toscaServiceTemplatePolicyType);
databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
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 17eae00..d19dbf9 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
@@ -106,7 +106,7 @@
PfValidationResult result = serviceTemplateToWrite.validate(new PfValidationResult());
if (!result.isValid()) {
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.toString());
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.toString());
}
new SimpleToscaServiceTemplateProvider().write(dao, serviceTemplateToWrite);
@@ -378,6 +378,8 @@
LOGGER.debug("->getPolicies: name={}, version={}", name, version);
JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+ serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+ serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
@@ -392,13 +394,10 @@
}
for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) {
- if (policy.getDerivedFrom() != null) {
- JpaToscaServiceTemplate referencedEntitiesServiceTemplate =
- getPolicyTypes(dao, policy.getDerivedFrom().getName(), policy.getDerivedFrom().getVersion());
+ JpaToscaServiceTemplate referencedEntitiesServiceTemplate =
+ getPolicyTypes(dao, policy.getType().getName(), policy.getType().getVersion());
- serviceTemplate =
- ToscaServiceTemplateUtils.addFragment(serviceTemplate, referencedEntitiesServiceTemplate);
- }
+ serviceTemplate = ToscaServiceTemplateUtils.addFragment(serviceTemplate, referencedEntitiesServiceTemplate);
}
LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
@@ -502,7 +501,7 @@
if (policyType == null) {
String errorMessage =
"policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage);
}
}
@@ -531,7 +530,7 @@
// We should have one and only one returned entry
if (filterdPolicyTypeList.size() != 1) {
String errorMessage = "search for latest policy type " + policyTypeName + " returned more than one entry";
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ throw new PfModelRuntimeException(Response.Status.CONFLICT, errorMessage);
}
return (JpaToscaPolicyType) filterdPolicyTypeList.get(0);
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
index aae45e5..6f21c19 100644
--- 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
@@ -95,7 +95,7 @@
if (!result.isValid()) {
String message = result.toString();
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, message);
}
return compositeTemplate;
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index cc04319..b75273e 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -147,7 +147,7 @@
final Function<JpaToscaServiceTemplate, String> checkerFunction) {
String message = checkerFunction.apply(serviceTemplate);
if (message != null) {
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message);
}
}
@@ -263,7 +263,7 @@
}
if (!result.isValid()) {
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.toString());
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.toString());
}
entityTypes.getConceptMap().entrySet()