diff options
8 files changed, 446 insertions, 12 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java index 582c10d2..28587077 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java @@ -94,7 +94,9 @@ public class PapRestServer implements Startable { String.join(",", HealthCheckRestControllerV1.class.getCanonicalName(), StatisticsRestControllerV1.class.getCanonicalName(), PdpGroupDeployControllerV1.class.getCanonicalName(), - PdpGroupDeleteControllerV1.class.getCanonicalName())); + PdpGroupDeleteControllerV1.class.getCanonicalName(), + PdpGroupStateChangeControllerV1.class.getCanonicalName(), + PdpGroupQueryControllerV1.class.getCanonicalName())); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java new file mode 100644 index 00000000..ae4ef1d9 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.ResponseHeader; + +import java.util.UUID; + +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.pap.concepts.PdpGroups; + +/** + * Class to provide REST end points for PAP component to query details of all PDP groups. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PdpGroupQueryControllerV1 extends PapRestControllerV1 { + + private final PdpGroupQueryProvider provider = new PdpGroupQueryProvider(); + + /** + * Queries details of all PDP groups. + * + * @param requestId request ID used in ONAP logging + * @return a response + */ + // @formatter:off + @GET + @Path("pdps") + @ApiOperation(value = "Query details of all PDP groups", + notes = "Queries details of all PDP groups, returning all group details", + response = PdpGroups.class, + tags = {"Policy Administration (PAP) API"}, + authorizations = @Authorization(value = AUTHORIZATION_TYPE), + responseHeaders = { + @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION, + response = String.class), + @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION, + response = String.class), + @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION, + response = String.class), + @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION, + response = UUID.class)}, + extensions = {@Extension(name = EXTENSION_NAME, + properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION), + @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})}) + @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE), + @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE), + @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)}) + // @formatter:on + + public Response queryGroupDetails( + @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) { + + final Pair<Status, PdpGroups> pair = provider.fetchPdpGroupDetails(); + + return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId) + .entity(pair.getRight()).build(); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java new file mode 100644 index 00000000..d37409bf --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import javax.ws.rs.core.Response; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.pap.concepts.PdpGroups; + +/** + * Provider for PAP component to query details of all PDP groups. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PdpGroupQueryProvider { + + /** + * Queries details of all PDP groups. + * + * @return a pair containing the status and the response + */ + public Pair<Response.Status, PdpGroups> fetchPdpGroupDetails() { + + /* + * TODO Fetch all the details from DB and create the PdpGroups object. + */ + + return Pair.of(Response.Status.OK, new PdpGroups()); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java new file mode 100644 index 00000000..f3eb1dea --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.ResponseHeader; + +import java.util.UUID; + +import javax.ws.rs.HeaderParam; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse; +import org.onap.policy.pdp.common.enums.PdpState; + +/** + * Class to provide REST end points for PAP component to change state of a PDP group. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PdpGroupStateChangeControllerV1 extends PapRestControllerV1 { + + private final PdpGroupStateChangeProvider provider = new PdpGroupStateChangeProvider(); + + /** + * Changes state of a PDP group. + * + * @param requestId request ID used in ONAP logging + * @param groupName name of the PDP group to be deleted + * @param version version of the PDP group + * @param state state of the PDP group + * @return a response + */ + // @formatter:off + @PUT + @Path("pdps/groups/{name}/versions/{version}") + @ApiOperation(value = "Change state of a PDP Group", + notes = "Changes state of PDP Group, returning optional error details", + response = PdpGroupStateChangeResponse.class, + tags = {"Policy Administration (PAP) API"}, + authorizations = @Authorization(value = AUTHORIZATION_TYPE), + responseHeaders = { + @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION, + response = String.class), + @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION, + response = String.class), + @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION, + response = String.class), + @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION, + response = UUID.class)}, + extensions = {@Extension(name = EXTENSION_NAME, + properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION), + @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})}) + @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE), + @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE), + @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)}) + // @formatter:on + + public Response changeGroupState( + @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId, + @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") final String groupName, + @ApiParam(value = "PDP Group Version", required = true) @PathParam("version") final String version, + @ApiParam(value = "PDP Group State", required = true) @QueryParam("state") final PdpState state) { + + final Pair<Status, PdpGroupStateChangeResponse> pair = provider.changeGroupState(groupName, version, state); + + return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId) + .entity(pair.getRight()).build(); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java new file mode 100644 index 00000000..51441e46 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import javax.ws.rs.core.Response; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse; +import org.onap.policy.pdp.common.enums.PdpState; + +/** + * Provider for PAP component to change state of PDP group. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PdpGroupStateChangeProvider { + + /** + * Changes state of a PDP group. + * + * @param groupName name of the PDP group + * @param version version of the PDP group + * @param pdpGroupState state of the PDP group + * @return a pair containing the status and the response + */ + public Pair<Response.Status, PdpGroupStateChangeResponse> changeGroupState(final String groupName, + final String version, final PdpState pdpGroupState) { + + /* + * TODO Check preconditions - return error if any. + */ + + /* + * TODO Change state - sending state change messages to PDPs and arranging for listeners to complete the state + * change actions (in the background). + */ + + /* + * TODO Return error if unable to send state change to all PDPs. + */ + + return Pair.of(Response.Status.OK, new PdpGroupStateChangeResponse()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java index 0fc3577e..b6d0f1d7 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. @@ -25,16 +25,18 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import java.util.Arrays; + import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; + import org.junit.Test; import org.onap.policy.models.pap.concepts.PdpGroup; import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; import org.onap.policy.models.pap.concepts.PdpPolicies; import org.onap.policy.models.pap.concepts.PdpSubGroup; -import org.onap.policy.pdp.common.models.Policy; +import org.onap.policy.models.pap.concepts.Policy; public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { @@ -49,9 +51,9 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { @Test public void testDeployGroup() throws Exception { - Entity<PdpGroup> entgrp = makePdpGroupEntity(); + final Entity<PdpGroup> entgrp = makePdpGroupEntity(); - Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); + final Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); Response rawresp = invocationBuilder.post(entgrp); PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); @@ -68,9 +70,9 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { @Test public void testDeployPolicies() throws Exception { - Entity<PdpPolicies> entgrp = makePdpPoliciesEntity(); + final Entity<PdpPolicies> entgrp = makePdpPoliciesEntity(); - Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT); + final Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT); Response rawresp = invocationBuilder.post(entgrp); PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); @@ -86,10 +88,10 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { } private Entity<PdpGroup> makePdpGroupEntity() { - PdpSubGroup subgrp = new PdpSubGroup(); + final PdpSubGroup subgrp = new PdpSubGroup(); subgrp.setPdpType("drools"); - PdpGroup group = new PdpGroup(); + final PdpGroup group = new PdpGroup(); group.setName("drools-group"); group.setDescription("my description"); group.setVersion("my-version"); @@ -99,14 +101,14 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { } private Entity<PdpPolicies> makePdpPoliciesEntity() { - Policy pol1 = new Policy(); + final Policy pol1 = new Policy(); pol1.setName("policy-a"); pol1.setPolicyVersion("1"); - Policy pol2 = new Policy(); + final Policy pol2 = new Policy(); pol2.setName("policy-b"); - PdpPolicies policies = new PdpPolicies(); + final PdpPolicies policies = new PdpPolicies(); policies.setPolicies(Arrays.asList(pol1, pol2)); return Entity.entity(policies, MediaType.APPLICATION_JSON); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupQueryControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupQueryControllerV1.java new file mode 100644 index 00000000..f0bc12bd --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupQueryControllerV1.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; + +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpGroups; + +/** + * Class to perform unit test of {@link PdpGroupQueryControllerV1}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestPdpGroupQueryControllerV1 extends CommonPapRestServer { + + private static final String GROUP_ENDPOINT = "pdps"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(GROUP_ENDPOINT); + } + + @Test + public void testchangeGroupState() throws Exception { + final String uri = GROUP_ENDPOINT; + + final Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.get(); + PdpGroups resp = rawresp.readEntity(PdpGroups.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp); + + rawresp = invocationBuilder.get(); + resp = rawresp.readEntity(PdpGroups.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.get()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupStateChangeControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupStateChangeControllerV1.java new file mode 100644 index 00000000..d824b8b9 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupStateChangeControllerV1.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; + +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse; + +/** + * Class to perform unit test of {@link PdpGroupStateChangeControllerV1}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestPdpGroupStateChangeControllerV1 extends CommonPapRestServer { + + private static final String GROUP_ENDPOINT = "pdps/groups"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(GROUP_ENDPOINT + "/{name}/versions/{version}"); + } + + @Test + public void testchangeGroupState() throws Exception { + final String uri = GROUP_ENDPOINT + "/my-name/versions/1.2.3?state=ACTIVE"; + + final Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.put(Entity.json("")); + PdpGroupStateChangeResponse resp = rawresp.readEntity(PdpGroupStateChangeResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.put(Entity.json("")); + resp = rawresp.readEntity(PdpGroupStateChangeResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.put(Entity.json(""))); + } +} |