summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChenfei Gao <cgao@research.att.com>2019-10-09 16:36:05 -0400
committerChenfei Gao <cgao@research.att.com>2019-10-09 16:36:20 -0400
commitc0feaca7b641f6c0e3214f5333a1493c9893e0cb (patch)
treec5b2d448388fb38aa74129cbc2bfb2024f3e7381
parent20390c6ed94e2082cb688d76aacabe5366d908fe (diff)
Created a new simplified API for creating policies
Added a new endpoint to create multiple policies in one call Added corresponding provider method Added corresponding junit tests Issue-ID: POLICY-1991 Change-Id: I09202ededfd88cf9f1ac62ea036e98e3b5d78f98 Signed-off-by: Chenfei Gao <cgao@research.att.com>
-rw-r--r--main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java63
-rw-r--r--main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java14
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java82
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java39
4 files changed, 198 insertions, 0 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java
index 8c3d55ff..67baa8a4 100644
--- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java
+++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java
@@ -939,6 +939,69 @@ public class ApiRestController extends CommonRestController {
}
/**
+ * Creates one or more new policies in one call.
+ *
+ * @param body the body of policy following TOSCA definition
+ *
+ * @return the Response object containing the results of the API operation
+ */
+ @POST
+ @Path("/policies")
+ @ApiOperation(value = "Create one or more new policies",
+ notes = "Client should provide TOSCA body of the new polic(ies)",
+ authorizations = @Authorization(value = "basicAuth"),
+ tags = { "Policy", },
+ response = ToscaServiceTemplate.class,
+ responseHeaders = {
+ @ResponseHeader(name = "X-MinorVersion",
+ description = "Used to request or communicate a MINOR version back from the client"
+ + " to the server, and from the server back to the client",
+ response = String.class),
+ @ResponseHeader(name = "X-PatchVersion",
+ description = "Used only to communicate a PATCH version in a response for"
+ + " troubleshooting purposes only, and will not be provided by"
+ + " the client on request",
+ response = String.class),
+ @ResponseHeader(name = "X-LatestVersion",
+ description = "Used only to communicate an API's latest version",
+ response = String.class),
+ @ResponseHeader(name = "X-ONAP-RequestID",
+ description = "Used to track REST transactions for logging purpose",
+ response = UUID.class)
+ },
+ extensions = {
+ @Extension(name = "interface info", properties = {
+ @ExtensionProperty(name = "api-version", value = "1.0.0"),
+ @ExtensionProperty(name = "last-mod-release", value = "El Alto")
+ })
+ })
+ @ApiResponses(value = {
+ @ApiResponse(code = 400, message = "Invalid Body"),
+ @ApiResponse(code = 401, message = "Authentication Error"),
+ @ApiResponse(code = 403, message = "Authorization Error"),
+ @ApiResponse(code = 404, message = "Resource Not Found"),
+ @ApiResponse(code = 500, message = "Internal Server Error")
+ })
+ public Response createPolicies(
+ @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
+ @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) {
+
+ if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
+ NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body));
+ }
+
+ try (PolicyProvider policyProvider = new PolicyProvider()) {
+ ToscaServiceTemplate serviceTemplate = policyProvider.createPolicies(body);
+ updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST);
+ return makeOkResponse(requestId, serviceTemplate);
+ } catch (PfModelException | PfModelRuntimeException pfme) {
+ LOGGER.error("POST /policies", pfme);
+ updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST);
+ return makeErrorResponse(requestId, pfme);
+ }
+ }
+
+ /**
* Deletes the specified version of a particular policy.
*
* @param policyTypeId the ID of specified policy type
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java
index 66fd7f0e..99dcd715 100644
--- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java
+++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java
@@ -145,6 +145,20 @@ public class PolicyProvider extends CommonModelProvider {
}
/**
+ * Creates one or more new policies.
+ *
+ * @param body the entity body of policy
+ *
+ * @return the ToscaServiceTemplate object
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
+
+ return modelsProvider.createPolicies(body);
+ }
+
+ /**
* Deletes the policy matching specified ID and version of both policy type and policy.
*
* @param policyTypeId the ID of policy type
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java
index b065277e..6e51e369 100644
--- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java
+++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java
@@ -151,6 +151,7 @@ public class TestApiRestServer {
+ "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.scaleout/versions/1";
private static final String OPS_POLICIES_VFIREWALL_VERSION = "policytypes/"
+ "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.modifyconfig/versions/1";
+ private static final String POLICIES = "policies";
private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
@@ -170,6 +171,11 @@ public class TestApiRestServer {
"policies/vFirewall.policy.monitoring.input.tosca.yaml"
};
+ private String[] toscaPoliciesResourceNames = {
+ "policies/vCPE.policies.optimization.input.tosca.json",
+ "policies/vCPE.policies.optimization.input.tosca.yaml"
+ };
+
private String[] toscaPolicyTypeResourceNames = {
"policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
"policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
@@ -292,6 +298,44 @@ public class TestApiRestServer {
}
@Test
+ public void testCreatePoliciesPersistent() throws Exception {
+ setupParameters(); // setup DB
+ main = startApiService(true);
+ for (String resrcName : toscaPolicyResourceNames) {
+ Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true);
+ assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
+ ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
+ assertNotNull(response);
+ assertFalse(response.getToscaTopologyTemplate().getPolicies().isEmpty());
+ }
+ }
+
+ @Test
+ public void testSimpleCreatePolicies() throws Exception {
+ main = startApiService(true);
+ for (String resrcName : toscaPoliciesResourceNames) {
+ Response rawResponse = createResource(POLICIES, resrcName, true);
+ assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
+ ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
+ assertEquals("policy type onap.policies.optimization.AffinityPolicy:0.0.0 for "
+ + "policy OSDF_CASABLANCA.Affinity_vCPE_1:1.0.0 does not exist", error.getErrorMessage());
+ }
+ }
+
+ @Test
+ public void testSimpleCreatePoliciesPersistent() throws Exception {
+ setupParameters(); // setup DB
+ main = startApiService(true);
+ for (String resrcName : toscaPoliciesResourceNames) {
+ Response rawResponse = createResource(POLICIES, resrcName, true);
+ assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
+ ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
+ assertNotNull(response);
+ assertFalse(response.getToscaTopologyTemplate().getPolicies().isEmpty());
+ }
+ }
+
+ @Test
public void testCreateGuardPolicies() {
try {
main = startApiService(true);
@@ -378,6 +422,44 @@ public class TestApiRestServer {
}
@Test
+ public void testHttpsCreatePoliciesPersistent() throws Exception {
+ setupParameters(); // setup DB
+ main = startApiService(false);
+ for (String resrcName : toscaPolicyResourceNames) {
+ Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, false);
+ assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
+ ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
+ assertNotNull(response);
+ assertFalse(response.getToscaTopologyTemplate().getPolicies().isEmpty());
+ }
+ }
+
+ @Test
+ public void testHttpsSimpleCreatePolicies() throws Exception {
+ main = startApiService(false);
+ for (String resrcName : toscaPoliciesResourceNames) {
+ Response rawResponse = createResource(POLICIES, resrcName, false);
+ assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
+ ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
+ assertEquals("policy type onap.policies.optimization.AffinityPolicy:0.0.0 for "
+ + "policy OSDF_CASABLANCA.Affinity_vCPE_1:1.0.0 does not exist", error.getErrorMessage());
+ }
+ }
+
+ @Test
+ public void testHttpsSimpleCreatePoliciesPersistent() throws Exception {
+ setupParameters(); // setup DB
+ main = startApiService(false);
+ for (String resrcName : toscaPoliciesResourceNames) {
+ Response rawResponse = createResource(POLICIES, resrcName, false);
+ assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
+ ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
+ assertNotNull(response);
+ assertFalse(response.getToscaTopologyTemplate().getPolicies().isEmpty());
+ }
+ }
+
+ @Test
public void testHttpsCreateGuardPolicies() {
try {
main = startApiService(false);
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
index e81cbf45..2e431704 100644
--- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
+++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
@@ -75,6 +75,19 @@ public class TestPolicyProvider {
private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json";
private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION =
"policies/vCPE.policy.bad.policytypeversion.json";
+ private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json";
+
+ // @formatter:off
+ private String[] toscaPolicyTypeResourceNames = {
+ "policytypes/onap.policies.optimization.AffinityPolicy.yaml",
+ "policytypes/onap.policies.optimization.DistancePolicy.yaml",
+ "policytypes/onap.policies.optimization.HpaPolicy.yaml",
+ "policytypes/onap.policies.optimization.QueryPolicy.yaml",
+ "policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
+ "policytypes/onap.policies.optimization.Vim_fit.yaml",
+ "policytypes/onap.policies.optimization.VnfPolicy.yaml"
+ };
+ // @formatter:on
/**
* Initializes parameters.
@@ -269,6 +282,32 @@ public class TestPolicyProvider {
}
@Test
+ public void testSimpleCreatePolicy() throws Exception {
+
+ String errorMessage = "policy type onap.policies.optimization.AffinityPolicy:0.0.0 for "
+ + "policy OSDF_CASABLANCA.Affinity_vCPE_1:1.0.0 does not exist";
+ assertThatThrownBy(() -> {
+ String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
+ ToscaServiceTemplate multiPoliciesServiceTemplate =
+ standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
+ policyProvider.createPolicies(multiPoliciesServiceTemplate);
+ }).hasMessage(errorMessage);
+
+ // Create required policy types
+ for (String policyTypeName : toscaPolicyTypeResourceNames) {
+ ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString(policyTypeName), ToscaServiceTemplate.class);
+ policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+ }
+
+ // Create multiple policies in one call
+ String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
+ ToscaServiceTemplate multiPoliciesServiceTemplate =
+ standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
+ policyProvider.createPolicies(multiPoliciesServiceTemplate);
+ }
+
+ @Test
public void testDeletePolicy() {
assertThatThrownBy(() -> {