diff options
Diffstat (limited to 'adapters')
25 files changed, 787 insertions, 117 deletions
diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V8.4__AddBBNameSelectionReference.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V8.4__AddBBNameSelectionReference.sql new file mode 100644 index 0000000000..2b8d4ea69f --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V8.4__AddBBNameSelectionReference.sql @@ -0,0 +1,16 @@ +use catalogdb; + +CREATE TABLE IF NOT EXISTS `bbname_selection_reference` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `CONTROLLER_ACTOR` varchar(200) NOT NULL , + `SCOPE` varchar(200) NOT NULL, + `ACTION` varchar(200) NOT NULL, + `BB_NAME` varchar(200) NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +INSERT INTO bbname_selection_reference (CONTROLLER_ACTOR,SCOPE,ACTION,BB_NAME) +VALUES +('APPC', 'vfModule', 'healthCheck','GenericVnfHealthCheckBB'), +('APPC', 'vfModule', 'configScaleOut','ConfigurationScaleOutBB'), +('APPC', 'vnf', 'healthCheck','GenericVnfHealthCheckBB');
\ No newline at end of file diff --git a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java index 707a2a45af..03ef24ded0 100644 --- a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java +++ b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java @@ -22,8 +22,8 @@ package org.onap.so.db.catalog.client; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -33,6 +33,7 @@ import org.junit.Before; import org.junit.Test; import org.onap.so.adapters.catalogdb.CatalogDbAdapterBaseTest; import org.onap.so.db.catalog.beans.AuthenticationType; +import org.onap.so.db.catalog.beans.BBNameSelectionReference; import org.onap.so.db.catalog.beans.CloudIdentity; import org.onap.so.db.catalog.beans.CloudSite; import org.onap.so.db.catalog.beans.CloudifyManager; @@ -428,7 +429,7 @@ public class CatalogDbClientTest extends CatalogDbAdapterBaseTest { } @Test - public void testPostCloudSite() { + public void testCloudSiteClient() { CatalogDbClientPortChanger localClient = new CatalogDbClientPortChanger( "http://localhost:" + client.wiremockPort, msoAdaptersAuth, client.wiremockPort); CloudSite cloudSite = new CloudSite(); @@ -455,6 +456,19 @@ public class CatalogDbClientTest extends CatalogDbAdapterBaseTest { assertEquals("TESTCLLI", getCloudSite.getClli()); assertEquals("regionId", getCloudSite.getRegionId()); assertEquals("RANDOMID", getCloudSite.getIdentityServiceId()); + + getCloudSite.setClli("clli2"); + getCloudSite.setRegionId("region2"); + + CloudSite updatedCloudSite = this.client.updateCloudSite(getCloudSite); + assertNotNull(updatedCloudSite); + assertNotNull(updatedCloudSite.getIdentityService()); + assertEquals("clli2", updatedCloudSite.getClli()); + assertEquals("region2", updatedCloudSite.getRegionId()); + + this.client.deleteCloudSite(getCloudSite.getId()); + getCloudSite = this.client.getCloudSite("MTN6"); + assertNull(getCloudSite); } @Test @@ -705,8 +719,8 @@ public class CatalogDbClientTest extends CatalogDbAdapterBaseTest { } @Test - public void getWorkflowByModelUUID_validUuid_expectedOutput() { - List<Workflow> workflows = client.findWorkflowByModelUUID("ff2ae348-214a-11e7-93ae-92361f002671"); + public void getWorkflowByVnfModelUUID_validUuid_expectedOutput() { + List<Workflow> workflows = client.findWorkflowByVnfModelUUID("ff2ae348-214a-11e7-93ae-92361f002671"); assertTrue(workflows != null); assertTrue(workflows.size() != 0); @@ -741,5 +755,21 @@ public class CatalogDbClientTest extends CatalogDbAdapterBaseTest { assertNotEquals(0, cloudSites.size()); } + @Test + public void getBBNameSelectionReference_validData_expectedOutput() { + BBNameSelectionReference bbNameSelectionReference = + client.getBBNameSelectionReference("APPC", "vfModule", "healthCheck"); + assertNotNull(bbNameSelectionReference); + assertEquals("GenericVnfHealthCheckBB", bbNameSelectionReference.getBbName()); + } + + @Test + public void getBBNameSelectionReference_invalidData_nullOutput() { + BBNameSelectionReference bbNameSelectionReference = + client.getBBNameSelectionReference("ABC", "vfModule", "healthCheck"); + assertNull(bbNameSelectionReference); + + } + } diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudException.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudException.java new file mode 100644 index 0000000000..9cdf7e3b2a --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudException.java @@ -0,0 +1,14 @@ +package org.onap.so.adapters.cloudregion; + +public class CloudException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -2631715095942372451L; + + public CloudException(String error, Exception e) { + super(error, e); + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRegionRestV1.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRegionRestV1.java new file mode 100644 index 0000000000..780480507b --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRegionRestV1.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (C) 2018 IBM. + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.cloudregion; + + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.http.HttpStatus; +import org.onap.so.db.catalog.beans.CloudSite; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + + +@Path("/v1/cloud-region") +@Api(value = "/v1/cloud-region", description = "root of cloud region adapter") +@Component +public class CloudRegionRestV1 { + private static Logger logger = LoggerFactory.getLogger(CloudRegionRestV1.class); + + @Autowired + private CloudRestImpl cloudRestImpl; + + @POST + @Path("{cloud-region-id}/{cloud-owner}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + @ApiOperation(value = "CreateCloudRegion", response = Response.class, + notes = "Create a cloud site in MSO and Region In AAI") + @ApiResponses({@ApiResponse(code = 201, message = "Cloud Region has been created"), + @ApiResponse(code = 500, message = "Create Cloud Region has failed")}) + public Response createCloudRegion( + @ApiParam(value = "cloud-region-id", required = true) @PathParam("cloud-region-id") String cloudRegionId, + @ApiParam(value = "cloud-owner", required = true) @PathParam("cloud-owner") String cloudOwner, + @ApiParam(value = "CloudSite", required = true) final CloudSite cloudSite) { + cloudRestImpl.createCloudRegion(cloudSite, cloudOwner); + return Response.status(HttpStatus.SC_CREATED).build(); + } + + @DELETE + @Path("{cloud-region-id}/{cloud-owner}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + @ApiOperation(value = "CreateCloudRegion", response = Response.class, notes = "Delete an cloud Region in SO") + @ApiResponses({@ApiResponse(code = 204, message = "cloud Region has been deleted"), + @ApiResponse(code = 500, message = "Cloud Region delete has failed")}) + public Response deleteCloudRegion( + @ApiParam(value = "cloud-region-id", required = true) @PathParam("cloud-region-id") String cloudRegionId, + @ApiParam(value = "cloud-owner", required = true) @PathParam("cloud-owner") String cloudOwner) { + cloudRestImpl.deleteCloudRegion(cloudRegionId); + return Response.status(HttpStatus.SC_NO_CONTENT).build(); + } + + @PUT + @Path("{cloud-region-id}/{cloud-owner}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + @ApiOperation(value = "CreateCloudRegion", response = Response.class, notes = "Update an existing Cloud Region") + @ApiResponses({@ApiResponse(code = 200, message = "Cloud Region has been updated"), + @ApiResponse(code = 500, message = "Update Cloud Region has failed examine entity object for details")}) + public Response updateCloudRegion( + @ApiParam(value = "cloud-region-id", required = true) @PathParam("cloud-region-id") String cloudRegionId, + @ApiParam(value = "cloud-owner", required = true) @PathParam("cloud-owner") String cloudOwner, + @ApiParam(value = "CloudSite", required = true) final CloudSite cloudSite) { + cloudRestImpl.updateCloudRegion(cloudSite, cloudOwner); + return Response.status(HttpStatus.SC_OK).build(); + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRestImpl.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRestImpl.java new file mode 100644 index 0000000000..4cde8655ae --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/cloudregion/CloudRestImpl.java @@ -0,0 +1,97 @@ +package org.onap.so.adapters.cloudregion; + +import java.util.Optional; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.uri.AAIResourceUri; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.db.catalog.beans.CloudSite; +import org.onap.so.db.catalog.client.CatalogDbClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + + +@Component +public class CloudRestImpl { + private static final Logger logger = LoggerFactory.getLogger(CloudRestImpl.class); + + private AAIResourcesClient aaiClient; + + @Autowired + private CatalogDbClient catalogDBClient; + + public void createCloudRegion(CloudSite cloudSite, String cloudOwner) throws CloudException { + createRegionInCatalogDb(cloudSite); + createCloudRegionInAAI(cloudSite, cloudOwner); + } + + public void updateCloudRegion(CloudSite cloudSite, String cloudOwner) throws CloudException { + updateRegionInCatalogDb(cloudSite); + } + + protected void updateRegionInCatalogDb(CloudSite cloudSite) { + try { + catalogDBClient.updateCloudSite(cloudSite); + } catch (Exception e) { + logger.error("Error updating cloud region in catalogdb", e); + throw new CloudException("Error updating cloud region in Catalog: " + e.getMessage(), e); + } + } + + public void deleteCloudRegion(String cloudRegionId) throws CloudException { + try { + catalogDBClient.deleteCloudSite(cloudRegionId); + } catch (Exception e) { + logger.error("Error deleting cloud region in catalogdb", e); + throw new CloudException("Error deleting cloud region in Catalog: " + e.getMessage(), e); + } + } + + protected void createCloudRegionInAAI(CloudSite cloudSite, String cloudOwner) { + try { + CloudRegion cloudRegion = mapCloudRegion(cloudSite, cloudOwner); + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, + cloudRegion.getCloudOwner(), cloudRegion.getCloudRegionId()); + getAaiClient().createIfNotExists(uri, Optional.of(cloudRegion)); + } catch (Exception e) { + logger.error("Error creating cloud region in AAI", e); + throw new CloudException("Error creating cloud region in AAI: " + e.getMessage(), e); + } + } + + protected void createRegionInCatalogDb(CloudSite cloudSite) throws CloudException { + try { + CloudSite existingCloudSite = catalogDBClient.getCloudSite(cloudSite.getRegionId()); + if (existingCloudSite == null) { + catalogDBClient.postCloudSite(cloudSite); + } + } catch (Exception e) { + logger.error("Error creating cloud site in Catalog Adapter: " + e.getMessage(), e); + throw new CloudException("Error creating cloud site in Catalog Adapter", e); + } + } + + protected CloudRegion mapCloudRegion(CloudSite cloudSite, String cloudOwner) { + CloudRegion region = new CloudRegion(); + region.setCloudOwner(cloudOwner); + region.setCloudRegionId(cloudSite.getRegionId()); + region.setCloudRegionVersion(cloudSite.getCloudVersion()); + region.setOwnerDefinedType("cLCP"); + region.setOrchestrationDisabled(false); + region.setComplexName("NA"); + region.setInMaint(false); + region.setCloudType("openstack"); + return region; + } + + protected AAIResourcesClient getAaiClient() { + if (aaiClient == null) + return new AAIResourcesClient(); + else + return aaiClient; + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CXFConfiguration.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CXFConfiguration.java index 9fc5c979d8..9badd795eb 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CXFConfiguration.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CXFConfiguration.java @@ -32,6 +32,7 @@ import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.swagger.Swagger2Feature; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; +import org.onap.so.adapters.cloudregion.CloudRegionRestV1; import org.onap.so.adapters.network.MsoNetworkAdapterAsyncImpl; import org.onap.so.adapters.network.MsoNetworkAdapterImpl; import org.onap.so.adapters.network.NetworkAdapterRest; @@ -47,10 +48,12 @@ import org.onap.so.adapters.vnf.VolumeAdapterRestV2; import org.onap.so.client.policy.JettisonStyleMapperProvider; import org.onap.so.logging.cxf.interceptor.SOAPLoggingInInterceptor; import org.onap.so.logging.cxf.interceptor.SOAPLoggingOutInterceptor; +import org.onap.so.logging.jaxrs.filter.SOAuditLogContainerFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; @@ -81,7 +84,14 @@ public class CXFConfiguration { @Autowired private MsoVnfCloudifyAdapterImpl vnfCloudifyAdapterImpl; @Autowired + private CloudRegionRestV1 cloudRegionRestV1; + @Autowired private JettisonStyleMapperProvider jettisonStyleObjectMapper; + @Autowired + private ObjectMapper mapper; + @Autowired + private SOAuditLogContainerFilter soAuditLogContainerFilter; + @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { @@ -170,6 +180,7 @@ public class CXFConfiguration { return endpoint; } + // Uses Jettson Style marshalling semantics @Bean public Server rsServer() { JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean(); @@ -178,7 +189,20 @@ public class CXFConfiguration { vnfAdapterRestV2, volumeAdapterRest, volumeAdapterRestV2)); endpoint.setAddress("/rest"); endpoint.setFeatures(Arrays.asList(createSwaggerFeature(), new LoggingFeature())); - endpoint.setProvider(new JacksonJsonProvider(jettisonStyleObjectMapper.getMapper())); + endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(jettisonStyleObjectMapper.getMapper()), + soAuditLogContainerFilter)); + return endpoint.create(); + } + + // Uses normal Jackson marshalling semantics + @Bean + public Server rsServerApi() { + JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean(); + endpoint.setBus(springBus()); + endpoint.setServiceBeans(Arrays.<Object>asList(cloudRegionRestV1)); + endpoint.setAddress("/api"); + endpoint.setFeatures(Arrays.asList(new LoggingFeature())); + endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(mapper), soAuditLogContainerFilter)); return endpoint.create(); } diff --git a/adapters/mso-openstack-adapters/src/test/java/org/onap/so/adapters/cloudregion/CloudRegionRestImplTest.java b/adapters/mso-openstack-adapters/src/test/java/org/onap/so/adapters/cloudregion/CloudRegionRestImplTest.java new file mode 100644 index 0000000000..9c62c286ac --- /dev/null +++ b/adapters/mso-openstack-adapters/src/test/java/org/onap/so/adapters/cloudregion/CloudRegionRestImplTest.java @@ -0,0 +1,96 @@ +package org.onap.so.adapters.cloudregion; + +import static com.shazam.shazamcrest.MatcherAssert.assertThat; +import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.uri.AAIResourceUri; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.db.catalog.beans.CloudSite; +import org.onap.so.db.catalog.client.CatalogDbClient; + + +@RunWith(MockitoJUnitRunner.class) +public class CloudRegionRestImplTest { + + @Spy + @InjectMocks + private CloudRestImpl cloudRestImpl; + + @Mock + private CatalogDbClient catalogDbClientMock; + + @Mock + private AAIResourcesClient aaiResClientMock; + + private CloudSite cloudSite = new CloudSite(); + + private CloudRegion testCloudRegion = new CloudRegion(); + + @Before + public void setup() { + cloudSite.setCloudVersion("1.0"); + cloudSite.setRegionId("region1"); + Mockito.doReturn(aaiResClientMock).when(cloudRestImpl).getAaiClient(); + testCloudRegion.setCloudOwner("bob"); + testCloudRegion.setCloudRegionId("region1"); + testCloudRegion.setCloudRegionVersion("1.0"); + testCloudRegion.setInMaint(false); + testCloudRegion.setOrchestrationDisabled(false); + testCloudRegion.setComplexName("NA"); + testCloudRegion.setCloudRegionVersion("1.0"); + testCloudRegion.setOwnerDefinedType("cLCP"); + testCloudRegion.setCloudType("openstack"); + } + + @Test + public void mapCloudRegionTest() { + CloudRegion mappedRegion = cloudRestImpl.mapCloudRegion(cloudSite, "bob"); + assertThat(mappedRegion, sameBeanAs(testCloudRegion)); + } + + @Test + public void createCloudRegionTest() { + when(catalogDbClientMock.getCloudSite("region1")).thenReturn(null); + when(catalogDbClientMock.postCloudSite(cloudSite)).thenReturn(cloudSite); + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, "bob", "region1"); + cloudRestImpl.createCloudRegion(cloudSite, "bob"); + ArgumentCaptor<AAIResourceUri> actualURI = ArgumentCaptor.forClass(AAIResourceUri.class); + ArgumentCaptor<Optional<Object>> actualCloudRegion = ArgumentCaptor.forClass(Optional.class); + verify(catalogDbClientMock, times(1)).getCloudSite("region1"); + verify(catalogDbClientMock, times(1)).postCloudSite(cloudSite); + verify(aaiResClientMock, times(1)).createIfNotExists(Mockito.eq(uri), Mockito.any()); + verify(aaiResClientMock, times(1)).createIfNotExists(actualURI.capture(), actualCloudRegion.capture()); + assertThat((CloudRegion) actualCloudRegion.getValue().get(), sameBeanAs(testCloudRegion)); + } + + @Test + public void updateCloudRegionTest() { + when(catalogDbClientMock.updateCloudSite(cloudSite)).thenReturn(cloudSite); + cloudRestImpl.updateCloudRegion(cloudSite, "bob"); + verify(catalogDbClientMock, times(1)).updateCloudSite(cloudSite); + } + + @Test + public void deleteCloudRegionTest() { + doNothing().when(catalogDbClientMock).deleteCloudSite("region1"); + cloudRestImpl.deleteCloudRegion(cloudSite.getRegionId()); + verify(catalogDbClientMock, times(1)).deleteCloudSite("region1"); + } + +} diff --git a/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/OrchestrationTaskRepositoryCustomController.java b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/OrchestrationTaskRepositoryCustomController.java new file mode 100644 index 0000000000..e32d90b137 --- /dev/null +++ b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/OrchestrationTaskRepositoryCustomController.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.requestsdb; + +import org.onap.so.adapters.requestsdb.exceptions.MsoRequestsDbException; +import org.onap.so.db.request.beans.OrchestrationTask; +import org.onap.so.db.request.data.repository.OrchestrationTaskRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import java.util.List; + +@RestController +public class OrchestrationTaskRepositoryCustomController { + + @Autowired + private OrchestrationTaskRepository orchestrationTaskRepository; + + @RequestMapping(method = RequestMethod.GET, value = "/orchestrationTask") + public List<OrchestrationTask> getAllOrchestrationTask() { + return orchestrationTaskRepository.findAll(); + } + + @RequestMapping(method = RequestMethod.GET, value = "/orchestrationTask/{taskId}") + public OrchestrationTask getOrchestrationTask(@PathVariable("taskId") String taskId) throws MsoRequestsDbException { + return orchestrationTaskRepository.findById(taskId) + .orElseThrow(() -> new MsoRequestsDbException("orchestration task not found: " + taskId)); + } + + @RequestMapping(method = RequestMethod.POST, value = "/orchestrationTask/") + public OrchestrationTask createOrchestrationTask(@RequestBody OrchestrationTask orchestrationTask) { + return orchestrationTaskRepository.save(orchestrationTask); + } + + @RequestMapping(method = RequestMethod.PUT, value = "/orchestrationTask/{taskId}") + public OrchestrationTask updateOrchestrationTask(@PathVariable("taskId") String taskId, + @RequestBody OrchestrationTask orchestrationTask) throws MsoRequestsDbException { + return orchestrationTaskRepository.save(orchestrationTask); + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/orchestrationTask/{taskId}") + public void deleteOrchestrationTask(@PathVariable("taskId") String taskId) { + orchestrationTaskRepository.deleteById(taskId); + } +} diff --git a/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V8.1__Add_Orchestration_Task_Table.sql b/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V8.1__Add_Orchestration_Task_Table.sql new file mode 100644 index 0000000000..0d7cb7a62b --- /dev/null +++ b/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V8.1__Add_Orchestration_Task_Table.sql @@ -0,0 +1,12 @@ +USE `requestdb`; + +CREATE TABLE `orchestration_task` ( + `TASK_ID` varchar(200) NOT NULL, + `REQUEST_ID` varchar(200) NOT NULL, + `NAME` varchar(200) NOT NULL, + `CREATED_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `STATUS` varchar(200) NOT NULL, + `IS_MANUAL` varchar(20) NOT NULL, + `PARAMS` varchar(20000) DEFAULT NULL, + PRIMARY KEY (`TASK_ID`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; diff --git a/adapters/mso-ve-vnfm-adapter/pom.xml b/adapters/mso-ve-vnfm-adapter/pom.xml index 3518445b45..4472956eb7 100644 --- a/adapters/mso-ve-vnfm-adapter/pom.xml +++ b/adapters/mso-ve-vnfm-adapter/pom.xml @@ -59,10 +59,21 @@ </dependencies> <build> + <finalName>${project.artifactId}-${project.version}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> + <configuration> + <mainClass>org.onap.so.adapters.vevnfm.Application</mainClass> + </configuration> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> </plugin> </plugins> </build> diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java index eca5240cb5..251e0c426b 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java @@ -20,29 +20,15 @@ package org.onap.so.adapters.vevnfm.provider; -import java.util.List; -import org.apache.logging.log4j.util.Strings; import org.onap.so.configuration.rest.BasicHttpHeadersProvider; -import org.springframework.http.HttpHeaders; public class AuthorizationHeadersProvider extends BasicHttpHeadersProvider { - private List<String> previousAuthorization; - public void addAuthorization(final String authorization) { - final HttpHeaders headers = getHttpHeaders(); - previousAuthorization = headers.get(AUTHORIZATION_HEADER); - headers.set(AUTHORIZATION_HEADER, authorization); - } - - public void resetPrevious() { - if (!isPreviousAuthorizationBlank()) { - getHttpHeaders().addAll(AUTHORIZATION_HEADER, previousAuthorization); - } + getHttpHeaders().set(AUTHORIZATION_HEADER, authorization); } - private boolean isPreviousAuthorizationBlank() { - return previousAuthorization == null || previousAuthorization.isEmpty() - || Strings.isBlank(previousAuthorization.get(0)); + public void removeAuthorization() { + getHttpHeaders().remove(AUTHORIZATION_HEADER); } } diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java index 0e77ce4073..eefd9ba93b 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java @@ -68,7 +68,7 @@ public class SubscriberService { final LccnSubscriptionRequest request = createRequest(); return sender.send(info, request); } finally { - headersProvider.resetPrevious(); + headersProvider.removeAuthorization(); } } diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProviderTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProviderTest.java new file mode 100644 index 0000000000..64503ddfc2 --- /dev/null +++ b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProviderTest.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * SO + * ================================================================================ + * Copyright (C) 2020 Samsung. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.vevnfm.provider; + +import static org.junit.Assert.*; +import static org.onap.so.configuration.rest.BasicHttpHeadersProvider.AUTHORIZATION_HEADER; +import org.junit.Test; +import org.springframework.http.HttpHeaders; + +public class AuthorizationHeadersProviderTest { + + private static final String AUTHORIZATION_EXAMPLE = "authorization"; + + private final AuthorizationHeadersProvider provider = new AuthorizationHeadersProvider(); + + @Test + public void testSuccessValidAuthorizationAndRemoval() { + final HttpHeaders headers = provider.getHttpHeaders(); + final int size = headers.size(); + + provider.addAuthorization(AUTHORIZATION_EXAMPLE); + assertEquals(size + 1, headers.size()); + assertTrue(headers.containsKey(AUTHORIZATION_HEADER)); + + provider.removeAuthorization(); + assertEquals(size, headers.size()); + assertFalse(headers.containsKey(AUTHORIZATION_HEADER)); + } +} diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/Constants.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/Constants.java index fb32fb9605..d798267918 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/Constants.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/Constants.java @@ -32,8 +32,12 @@ public class Constants { public static final String SERVICE_VERSION = "v1"; public static final String BASE_URL = "/so/" + SERVICE_NAME + "/" + SERVICE_VERSION; public static final String PACKAGE_MANAGEMENT_BASE_URL = BASE_URL + "/vnfpkgm/v1"; - public static final String ETSI_SUBSCRIPTION_NOTIFICATION_BASE_URL = BASE_URL + "/etsicatalogmanager/notification"; - public static final String ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL = BASE_URL + "/etsicatalogmanager"; + public static final String ETSI_CATALOG_MANAGER_BASE_ENDPOINT = "/etsicatalogmanager"; + public static final String ETSI_SUBSCRIPTION_NOTIFICATION_ENDPOINT = "/notification"; + public static final String ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL = + BASE_URL + ETSI_CATALOG_MANAGER_BASE_ENDPOINT; + public static final String ETSI_SUBSCRIPTION_NOTIFICATION_BASE_URL = + ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL + ETSI_SUBSCRIPTION_NOTIFICATION_ENDPOINT; public static final String APPLICATION_ZIP = "application/zip"; public static final String OPERATION_NOTIFICATION_ENDPOINT = "/lcn/VnfLcmOperationOccurrenceNotification"; diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/VnfmBasicHttpSecurityConfigurer.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/VnfmBasicHttpSecurityConfigurer.java index 1e0a18a339..f1815054e3 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/VnfmBasicHttpSecurityConfigurer.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/VnfmBasicHttpSecurityConfigurer.java @@ -9,9 +9,9 @@ * 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. @@ -35,7 +35,6 @@ import org.springframework.util.StringUtils; /** * @author Waqas Ikram (waqas.ikram@est.tech) * @author Gareth Roper (gareth.roper@est.tech) - * */ @Primary @Component @@ -53,10 +52,10 @@ public class VnfmBasicHttpSecurityConfigurer implements HttpSecurityConfigurer { http.csrf().disable().authorizeRequests().anyRequest().permitAll(); } else { http.csrf().disable().authorizeRequests().antMatchers("/manage/health", "/manage/info").permitAll() - .antMatchers(HttpMethod.GET, "/etsi/subscription/notification").permitAll().antMatchers("/**") + .antMatchers(HttpMethod.GET, Constants.ETSI_SUBSCRIPTION_NOTIFICATION_BASE_URL).permitAll() + .antMatchers("/**") .hasAnyRole(StringUtils.collectionToDelimitedString(soUserCredentialConfiguration.getRoles(), ",")) .and().httpBasic(); - } } } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/converters/sol003/etsicatalog/PkgmSubscriptionRequestConverter.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/converters/sol003/etsicatalog/PkgmSubscriptionRequestConverter.java index c6d51c99aa..d0fd4c9e7c 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/converters/sol003/etsicatalog/PkgmSubscriptionRequestConverter.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/converters/sol003/etsicatalog/PkgmSubscriptionRequestConverter.java @@ -20,14 +20,10 @@ package org.onap.so.adapters.vnfmadapter.converters.sol003.etsicatalog; -import static org.slf4j.LoggerFactory.getLogger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; -import java.util.UUID; -import javax.swing.text.html.Option; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.Version; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfProducts; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfProductsProviders; @@ -38,7 +34,6 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model. import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVersions; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVnfProducts; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVnfProductsFromProviders; -import org.slf4j.Logger; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Service; @@ -48,14 +43,11 @@ import org.springframework.stereotype.Service; * * @author Ronan Kenny (ronan.kenny@est.tech) * @author Gareth Roper (gareth.roper@est.tech) - * */ @Service public class PkgmSubscriptionRequestConverter implements Converter<PkgmSubscriptionRequest, org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest> { - private static final Logger logger = getLogger(PkgmSubscriptionRequestConverter.class); - @Override public org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest convert( PkgmSubscriptionRequest pkgmSubscriptionRequest) { @@ -133,7 +125,8 @@ public class PkgmSubscriptionRequestConverter implements final List<VnfProductsProviders> etsiCatalogManagerVnfProductsProviders = new ArrayList<>(); filterProductsFromProvider.forEach(vnfProduct -> { etsiCatalogManagerVnfProductsProviders - .add(new VnfProductsProviders().vnfProducts(getVnfProducts(vnfProduct.getVnfProducts()))); + .add(new VnfProductsProviders().vnfProvider(vnfProduct.getVnfProvider()) + .vnfProducts(getVnfProducts(vnfProduct.getVnfProducts()))); }); return etsiCatalogManagerVnfProductsProviders; } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProvider.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProvider.java index 62b365745c..0dcc49eeac 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProvider.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProvider.java @@ -21,9 +21,9 @@ package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog; import java.util.Optional; +import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.NsdmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001; -import org.springframework.http.ResponseEntity; /** * Provides methods for invoking REST calls to the ETSI Catalog Manager. @@ -74,11 +74,26 @@ public interface EtsiCatalogServiceProvider { Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath); /** - * Post the SubscriptionRequest Object. + * POST the SubscriptionRequest Object. * - * @return The ResponseEntity containing the ETSI Catalog Manager's PkgmSubscription object. + * @return The ETSI Catalog Manager's PkgmSubscription object. */ Optional<PkgmSubscription> postSubscription( final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest); + /** + * Get the Subscription from ETSI Catalog. + * + * @param subscriptionId Subscription ID + * @return The Subscription {@link NsdmSubscription} from ETSI Catalog + */ + Optional<NsdmSubscription> getSubscription(final String subscriptionId); + + /** + * DELETE the SubscriptionRequest Object. + * + * @return A Boolean representing if the delete was successful or not. + */ + boolean deleteSubscription(final String subscriptionId); + } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProviderImpl.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProviderImpl.java index 1a48494e1a..30d084629c 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProviderImpl.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProviderImpl.java @@ -21,11 +21,16 @@ package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog; import java.util.Optional; -import javax.swing.text.html.Option; +import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.NsdmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001; -import org.onap.so.adapters.vnfmadapter.rest.exceptions.*; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerBadRequestException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerRequestFailureException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionNotFoundException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgBadRequestException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException; import org.onap.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.so.rest.exceptions.InvalidRestRequestException; import org.onap.so.rest.exceptions.RestProcessingException; @@ -76,7 +81,7 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class); logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}", response.getStatusCodeValue()); - if (response.getStatusCode() == HttpStatus.OK) { + if (response.getStatusCode().is2xxSuccessful()) { return Optional.ofNullable(response.getBody()); } } catch (final HttpResouceNotFoundException httpResouceNotFoundException) { @@ -101,7 +106,7 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class); logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}", response.getStatusCodeValue()); - if (response.getStatusCode() == HttpStatus.OK) { + if (response.getStatusCode().is2xxSuccessful()) { if (response.hasBody()) { final VnfPkgInfo[] vnfPackages = response.getBody(); assert (vnfPackages != null); @@ -142,7 +147,7 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class); logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId, response.getStatusCodeValue()); - if (response.getStatusCode() == HttpStatus.OK) { + if (response.getStatusCode().is2xxSuccessful()) { if (response.hasBody()) { final VnfPkgInfo vnfPkgInfo = response.getBody(); if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) { @@ -180,7 +185,7 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide final ResponseEntity<PkgmSubscription> responseEntity = httpServiceProvider.postHttpRequest(etsiCatalogManagerSubscriptionRequest, etsiCatalogUrlProvider.getSubscriptionUrl(), PkgmSubscription.class); - if (responseEntity.getStatusCode() == HttpStatus.OK) { + if (responseEntity.getStatusCode().is2xxSuccessful()) { if (responseEntity.hasBody()) { return Optional.of(responseEntity.getBody()); } @@ -190,7 +195,8 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide return Optional.empty(); } catch (final InvalidRestRequestException invalidRestRequestException) { logger.error("Caught InvalidRestRequestException", invalidRestRequestException); - throw new EtsiCatalogManagerBadRequestException("Bad Request Received on postSubscription call."); + throw new EtsiCatalogManagerBadRequestException( + "Bad Request Received on postSubscription call to ETSI Catalog Manager."); } catch (final RestProcessingException restProcessingException) { logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(), restProcessingException); @@ -200,6 +206,53 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide } } + @Override + public boolean deleteSubscription(final String subscriptionId) { + try { + final ResponseEntity<Void> responseEntity = httpServiceProvider + .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class); + + if (responseEntity.getStatusCode().is2xxSuccessful()) { + logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager", + subscriptionId); + return true; + } + logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode()); + return false; + } catch (final HttpResouceNotFoundException resouceNotFoundException) { + final String message = "Unable to find subscription in ETSI Catalog Manager using id: " + subscriptionId; + logger.error(message); + throw new SubscriptionNotFoundException(message); + } catch (final InvalidRestRequestException invalidRestRequestException) { + logger.error("Caught InvalidRestRequestException on deleteSubscription call to ETSI Catalog Manager.", + invalidRestRequestException); + throw new EtsiCatalogManagerBadRequestException( + "Bad Request Received on deleteSubscription call to ETSI Catalog Manager."); + } + } + + @Override + public Optional<NsdmSubscription> getSubscription(final String subscriptionId) { + try { + final ResponseEntity<NsdmSubscription> responseEntity = httpServiceProvider.getHttpResponse( + etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, NsdmSubscription.class); + + if (responseEntity.getStatusCode().is2xxSuccessful()) { + logger.debug("Found subscription with ID: {} in ETSI Catalog Manager", subscriptionId); + return Optional.ofNullable(responseEntity.getBody()); + } + logger.error("Unexpected Status Code Received on getting subscription from ETSI Catalog Manager: {}", + responseEntity.getStatusCode()); + } catch (final HttpResouceNotFoundException resouceNotFoundException) { + logger.error("Unable to find subscription in ETSI Catalog Manager using id: {}", subscriptionId); + return Optional.empty(); + } catch (final RestProcessingException | InvalidRestRequestException exception) { + logger.error("Unable to query ETSI Catalog Manager for subscription using id: {}", subscriptionId, + exception); + } + throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred."); + } + private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl, final String vnfRequestName) { try { diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/SubscriptionManager.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/SubscriptionManager.java index 30a16f70a8..d02bd9a345 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/SubscriptionManager.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/SubscriptionManager.java @@ -31,8 +31,8 @@ import java.util.Objects; import java.util.Optional; import org.onap.so.adapters.vnfmadapter.Constants; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProvider; -import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogUrlProvider; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.BasicAuth; +import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.NsdmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest; @@ -40,8 +40,8 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model. import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinksSelf; import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.cache.PackageManagementCacheServiceProvider; import org.onap.so.adapters.vnfmadapter.rest.exceptions.InternalServerErrorException; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionNotFoundException; import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionRequestConversionException; -import org.onap.so.rest.service.HttpRestServiceProvider; import org.onap.so.utils.CryptoUtils; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -55,15 +55,12 @@ import org.springframework.stereotype.Service; * * @author Ronan Kenny (ronan.kenny@est.tech) * @author Gareth Roper (gareth.roper@est.tech) - * */ @Service public class SubscriptionManager { private static final Logger logger = getLogger(SubscriptionManager.class); private final PackageManagementCacheServiceProvider packageManagementCacheServiceProvider; - private final EtsiCatalogUrlProvider etsiCatalogUrlProvider; - private final HttpRestServiceProvider httpServiceProvider; private final ConversionService conversionService; private final EtsiCatalogServiceProvider etsiCatalogServiceProvider; private final String vnfmAdapterEndpoint; @@ -73,16 +70,12 @@ public class SubscriptionManager { @Autowired public SubscriptionManager(final PackageManagementCacheServiceProvider packageManagementCacheServiceProvider, - final ConversionService conversionService, final HttpRestServiceProvider httpServiceProvider, - final EtsiCatalogUrlProvider etsiCatalogUrlProvider, - final EtsiCatalogServiceProvider etsiCatalogServiceProvider, + final ConversionService conversionService, final EtsiCatalogServiceProvider etsiCatalogServiceProvider, @Value("${vnfmadapter.endpoint}") final String vnfmAdapterEndpoint, @Value("${mso.key}") final String msoKeyString, - @Value("${vnfmadapter.auth:D6CFE56451508B75536C5E8A1E7AE06D0346006A693BF29293A6E1C762EFD59C671911DB6E9294E4FE15E4C1C5524B}") final String vnfmAdapterAuth) { + @Value("${vnfmadapter.auth:BF29BA36F0CFE1C05507781F6B97EFBCA7EFAC9F595954D465FC43F646883EF585C20A58CBB02528A6FAAC}") final String vnfmAdapterAuth) { this.packageManagementCacheServiceProvider = packageManagementCacheServiceProvider; - this.etsiCatalogUrlProvider = etsiCatalogUrlProvider; this.conversionService = conversionService; - this.httpServiceProvider = httpServiceProvider; this.etsiCatalogServiceProvider = etsiCatalogServiceProvider; this.vnfmAdapterEndpoint = vnfmAdapterEndpoint; this.vnfmAdapterAuth = vnfmAdapterAuth; @@ -99,10 +92,10 @@ public class SubscriptionManager { etsiCatalogServiceProvider.postSubscription(etsiCatalogManagerSubscriptionRequest); if (optionalEtsiCatalogManagerSubscription.isPresent()) { - PkgmSubscription etsiCatalogManagerSubscription = optionalEtsiCatalogManagerSubscription.get(); + final PkgmSubscription etsiCatalogManagerSubscription = optionalEtsiCatalogManagerSubscription.get(); logger.debug("postPkgmSubscriptionRequest Response SubscriptionId: {}", - Objects.requireNonNull(etsiCatalogManagerSubscription.getId().toString())); - final String subscriptionId = etsiCatalogManagerSubscription.getId().toString(); + Objects.requireNonNull(etsiCatalogManagerSubscription.getId())); + final String subscriptionId = etsiCatalogManagerSubscription.getId(); packageManagementCacheServiceProvider.addSubscription(subscriptionId, pkgmSubscriptionRequest); @@ -120,12 +113,24 @@ public class SubscriptionManager { } - public Optional<String> getSubscriptionId(final PkgmSubscriptionRequest pkgmSubscriptionRequest) { return packageManagementCacheServiceProvider.getSubscriptionId(pkgmSubscriptionRequest); } public Optional<InlineResponse2002> getSubscription(final String subscriptionId) { + + logger.debug("Checking if subscrition with id: {} exists in ETSI Catalog Manager", subscriptionId); + final Optional<NsdmSubscription> etsiCatalogSubscriptionOption = + etsiCatalogServiceProvider.getSubscription(subscriptionId); + + if (!etsiCatalogSubscriptionOption.isPresent()) { + logger.debug("Unable to find subscription in ETSI Catalog Manager using id: {}", subscriptionId); + if (packageManagementCacheServiceProvider.getSubscription(subscriptionId).isPresent()) { + logger.debug("will remove subcription with id: {} from local cache", subscriptionId); + packageManagementCacheServiceProvider.deleteSubscription(subscriptionId); + } + } + final Optional<PkgmSubscriptionRequest> optional = packageManagementCacheServiceProvider.getSubscription(subscriptionId); if (optional.isPresent()) { @@ -139,10 +144,31 @@ public class SubscriptionManager { final Map<String, PkgmSubscriptionRequest> subscriptions = packageManagementCacheServiceProvider.getSubscriptions(); final List<InlineResponse2002> response = new ArrayList<>(); - subscriptions.forEach((key, value) -> response.add(getInlineResponse2002(key, value))); + subscriptions.forEach((key, value) -> { + final Optional<InlineResponse2002> optional = getSubscription(key); + if (optional.isPresent()) { + response.add(optional.get()); + } + }); return response; } + public boolean deleteSubscription(final String subscriptionId) { + if (packageManagementCacheServiceProvider.getSubscription(subscriptionId).isPresent()) { + try { + if (etsiCatalogServiceProvider.deleteSubscription(subscriptionId)) { + return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId); + } + } catch (final SubscriptionNotFoundException subscriptionNotFoundException) { + logger.error( + "Unable to find subscription in ETSI Catalog Manager using id: {} will delete it from local cache", + subscriptionId); + return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId); + } + } + return false; + } + public URI getSubscriptionUri(final String subscriptionId) { return URI.create( vnfmAdapterEndpoint + Constants.PACKAGE_MANAGEMENT_BASE_URL + "/subscriptions/" + subscriptionId); @@ -154,17 +180,17 @@ public class SubscriptionManager { } private org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest buildEtsiCatalogManagerPkgmSubscriptionRequest( - PkgmSubscriptionRequest pkgmSubscriptionRequest) throws GeneralSecurityException { + final PkgmSubscriptionRequest pkgmSubscriptionRequest) throws GeneralSecurityException { final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest; try { etsiCatalogManagerSubscriptionRequest = conversionService.convert(pkgmSubscriptionRequest, org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest.class); - } catch (ConversionException conversionException) { + } catch (final ConversionException conversionException) { logger.error(conversionException.getMessage()); throw new SubscriptionRequestConversionException( "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest"); - } catch (Exception exception) { + } catch (final Exception exception) { logger.error(exception.getMessage()); throw new InternalServerErrorException( "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest"); diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/cache/PackageManagementCacheServiceProvider.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/cache/PackageManagementCacheServiceProvider.java index 6042513a50..437d20e593 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/cache/PackageManagementCacheServiceProvider.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/cache/PackageManagementCacheServiceProvider.java @@ -60,7 +60,7 @@ public interface PackageManagementCacheServiceProvider { * Delete subscription from cache * * @param subscriptionId - * @return true if subscription exists and able to be removed, otherwise returns false + * @return Boolean */ boolean deleteSubscription(final String subscriptionId); diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/EtsiSubscriptionNotificationController.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/EtsiSubscriptionNotificationController.java index c5bd5bc14e..a2f44f9deb 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/EtsiSubscriptionNotificationController.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/EtsiSubscriptionNotificationController.java @@ -22,7 +22,6 @@ package org.onap.so.adapters.vnfmadapter.rest; import static org.onap.so.adapters.vnfmadapter.Constants.ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL; import static org.slf4j.LoggerFactory.getLogger; -import javax.ws.rs.core.MediaType; import org.slf4j.Logger; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; @@ -36,9 +35,7 @@ import org.springframework.web.bind.annotation.RequestMapping; * @author Gareth Roper (gareth.roper@est.tech) */ @Controller -@RequestMapping(value = ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL, - produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, - consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) +@RequestMapping(value = ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL) public class EtsiSubscriptionNotificationController { private static final Logger logger = getLogger(EtsiSubscriptionNotificationController.class); diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java index 3ead98fce2..21b20b0134 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java @@ -50,8 +50,6 @@ import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping(value = BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON) public class Sol003GrantController { - private static final String SEPARATOR = "_"; - private static final String VIM_TYPE = "OPENSTACK"; private static final Logger logger = LoggerFactory.getLogger(Sol003GrantController.class); public final AaiServiceProvider aaiServiceProvider; public final AaiHelper aaiHelper; diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementController.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementController.java index cce7241757..f1d20c65ef 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementController.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementController.java @@ -109,19 +109,19 @@ public class Sol003PackageManagementController { */ @GetMapping(value = "/vnf_packages/{vnfPkgId}/vnfd", produces = {MediaType.TEXT_PLAIN, APPLICATION_ZIP, MediaType.APPLICATION_JSON}) - public ResponseEntity<byte[]> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) { + public ResponseEntity<?> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) { logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Endpoint Invoked with VNF Package ID: ", vnfPkgId); final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageVnfd(vnfPkgId); if (response.isPresent()) { logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Response: ", HttpStatus.OK); - return new ResponseEntity(response.get(), HttpStatus.OK); + return new ResponseEntity<>(response.get(), HttpStatus.OK); } final String errorMessage = "An error occurred, a null response was received by the\n" + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnfd\" \n" + "endpoint."; logger.error(errorMessage); - return new ResponseEntity(new ProblemDetails().detail(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(new ProblemDetails().detail(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR); } /** diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionController.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionController.java index cbad564210..6db3797f9b 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionController.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionController.java @@ -23,14 +23,13 @@ package org.onap.so.adapters.vnfmadapter.rest; import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL; import static org.slf4j.LoggerFactory.getLogger; import java.net.URI; -import java.net.URISyntaxException; import java.security.GeneralSecurityException; import java.util.List; import java.util.Optional; import javax.ws.rs.core.MediaType; +import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest; -import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails; import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.SubscriptionManager; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -38,6 +37,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -79,7 +79,7 @@ public class Sol003PackageManagementSubscriptionController { @PostMapping(value = "/subscriptions") public ResponseEntity<?> postSubscriptionRequest(@RequestBody final PkgmSubscriptionRequest pkgmSubscriptionRequest) throws GeneralSecurityException { - logger.info(LOG_REQUEST_RECEIVED, " postSubscriptionRequest Endpoint Called"); + logger.info(LOG_REQUEST_RECEIVED, " postSubscriptionRequest Endpoint Called", pkgmSubscriptionRequest); // Check if subscription exists already. final Optional<String> exists = subscriptionManager.getSubscriptionId(pkgmSubscriptionRequest); @@ -140,6 +140,24 @@ public class Sol003PackageManagementSubscriptionController { } /** + * DELETE a specific subscription, by subscriptionId. Section Number: 10.4.8.3.5 + * + * @param subscriptionId The ID of the subscription that you wish to delete. + * @return Empty response if successful. Object: Void Response Code: 204 No Content + */ + @DeleteMapping(value = "/subscriptions/{subscriptionId}") + public ResponseEntity<?> deleteSubscription(@PathVariable("subscriptionId") final String subscriptionId) { + if (subscriptionManager.deleteSubscription(subscriptionId)) { + logger.debug("Successfully deleted subscription with id {}", subscriptionId); + return ResponseEntity.noContent().build(); + } + final String errorMessage = + "The requested subscription: " + subscriptionId + " was not found on call deleteSubscription"; + logger.error(errorMessage); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ProblemDetails().detail(errorMessage)); + } + + /** * Method to set the Location in the header with the URI parameter * * @param subscriptionUri diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionControllerTest.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionControllerTest.java index f90978e0d5..ba1bf71936 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionControllerTest.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionControllerTest.java @@ -20,27 +20,32 @@ package org.onap.so.adapters.vnfmadapter.rest; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL; import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; -import java.security.GeneralSecurityException; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; import java.net.URI; import java.net.URISyntaxException; +import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.UUID; -import com.google.gson.Gson; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.so.adapters.vnfmadapter.Constants; import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.LinkSelf; +import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.NsdmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmNotificationsFilter; import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002; @@ -51,13 +56,13 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model. import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVnfProductsFromProviders; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsLinks; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinksSelf; -import org.onap.so.utils.CryptoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -65,41 +70,33 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; -import org.springframework.http.HttpMethod; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; -import static org.hamcrest.Matchers.is; -import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; +import com.google.gson.Gson; /** * @author Ronan Kenny (ronan.kenny@est.tech) * @author Gareth Roper (gareth.roper@est.tech) - * */ @RunWith(SpringRunner.class) @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -@SuppressWarnings("unchecked") public class Sol003PackageManagementSubscriptionControllerTest { - private static String subscriptionId; private final Gson gson = new Gson(); - + private final URI msbEndpoint = URI.create("http://msb-iag.onap:80/api/vnfpkgm/v1/subscriptions"); @Autowired @Qualifier(CONFIGURABLE_REST_TEMPLATE) - private RestTemplate testRestTemplate; - - private MockRestServiceServer mockRestServer; - + private RestTemplate restTemplate; + private MockRestServiceServer mockRestServiceServer; @Autowired private CacheManager cacheServiceProvider; - private final URI msbEndpoint = URI.create("http://msb-iag.onap:80/api/vnfpkgm/v1/subscriptions"); - @Autowired private Sol003PackageManagementSubscriptionController sol003PackageManagementSubscriptionController; + private static final String ID = UUID.randomUUID().toString(); + @Before public void setUp() { - mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build(); + mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build(); final Cache cache = cacheServiceProvider.getCache(Constants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE); cache.clear(); } @@ -113,8 +110,8 @@ public class Sol003PackageManagementSubscriptionControllerTest { final HttpHeaders headers = buildHttpHeaders(Objects.requireNonNull(response.getBody()).getCallbackUri()); - SubscriptionsLinks subscriptionsLinks = new SubscriptionsLinks(); - VnfPackagesLinksSelf vnfPackagesLinksSelf = new VnfPackagesLinksSelf(); + final SubscriptionsLinks subscriptionsLinks = new SubscriptionsLinks(); + final VnfPackagesLinksSelf vnfPackagesLinksSelf = new VnfPackagesLinksSelf(); vnfPackagesLinksSelf.setHref("https://so-vnfm-adapter.onap:30406" + PACKAGE_MANAGEMENT_BASE_URL + "/subscriptions/" + response.getBody().getId()); subscriptionsLinks.setSelf(vnfPackagesLinksSelf); @@ -134,7 +131,6 @@ public class Sol003PackageManagementSubscriptionControllerTest { final ResponseEntity<InlineResponse2002> response = (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController .postSubscriptionRequest(pkgmSubscriptionRequest); - subscriptionId = Objects.requireNonNull(response.getBody()).getId(); // Create duplicate entry final PkgmSubscriptionRequest pkgmSubscriptionRequest2 = buildPkgmSubscriptionRequest(); @@ -148,12 +144,18 @@ public class Sol003PackageManagementSubscriptionControllerTest { @Test public void testSuccessGetSubscriptionWithSubscriptionId() throws GeneralSecurityException, URISyntaxException { + final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest(); + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + ID)).andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess(gson.toJson(new NsdmSubscription().id(ID)), MediaType.APPLICATION_JSON)); + final ResponseEntity<InlineResponse2002> response = (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController .postSubscriptionRequest(pkgmSubscriptionRequest); - subscriptionId = Objects.requireNonNull(response.getBody()).getId(); + final String subscriptionId = response.getBody().getId(); + + final ResponseEntity<InlineResponse2002> response2002 = (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController @@ -161,9 +163,8 @@ public class Sol003PackageManagementSubscriptionControllerTest { final HttpHeaders headers = buildHttpHeaders(response.getBody().getCallbackUri()); - assertEquals(response.getBody().getFilter(), pkgmSubscriptionRequest.getFilter()); - assert (response.getHeaders().equals(headers)); + assertEquals(response.getHeaders(), headers); assertEquals(HttpStatus.OK, response2002.getStatusCode()); assertEquals(pkgmSubscriptionRequest.getFilter(), response.getBody().getFilter()); // Ensure CallBackUri is set to new URI @@ -172,9 +173,12 @@ public class Sol003PackageManagementSubscriptionControllerTest { @Test public void testFailGetSubscriptionWithInvalidSubscriptionId() { + final String invalidId = "invalidSubscriptionId"; + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + invalidId)).andExpect(method(HttpMethod.GET)) + .andRespond(withStatus(HttpStatus.NOT_FOUND)); final ResponseEntity<InlineResponse2002> response = (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController - .getSubscription("invalidSubscriptionId"); + .getSubscription(invalidId); assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } @@ -183,14 +187,16 @@ public class Sol003PackageManagementSubscriptionControllerTest { final PkgmSubscription pkgmSubscription = buildPkgmSubscription(); final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest(); - mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) + mockRestServiceServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON)); + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + ID)).andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess(gson.toJson(new NsdmSubscription().id(ID)), MediaType.APPLICATION_JSON)); sol003PackageManagementSubscriptionController.postSubscriptionRequest(pkgmSubscriptionRequest); - ResponseEntity<List<InlineResponse2002>> response = + final ResponseEntity<List<InlineResponse2002>> response = sol003PackageManagementSubscriptionController.getSubscriptions(); - List<InlineResponse2002> subscriptionsList = response.getBody(); + final List<InlineResponse2002> subscriptionsList = response.getBody(); assertEquals(Objects.requireNonNull(response.getBody()).get(0).getFilter(), pkgmSubscriptionRequest.getFilter()); @@ -199,6 +205,70 @@ public class Sol003PackageManagementSubscriptionControllerTest { assertEquals(HttpStatus.OK, response.getStatusCode()); } + @Test + public void testSuccessDeleteSubscriptionWithSubscriptionId() throws GeneralSecurityException { + final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest(); + final PkgmSubscription pkgmSubscription = buildPkgmSubscription(); + final String subscriptionId = pkgmSubscription.getId(); + + mockRestServiceServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) + .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON)); + + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + subscriptionId)).andExpect(method(HttpMethod.DELETE)) + .andRespond(withStatus(HttpStatus.NO_CONTENT)); + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + subscriptionId)).andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess(gson.toJson(new NsdmSubscription().id(subscriptionId)), + MediaType.APPLICATION_JSON)); + + final ResponseEntity<InlineResponse2002> responsePost = + (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController + .postSubscriptionRequest(pkgmSubscriptionRequest); + + final ResponseEntity responseDelete = + sol003PackageManagementSubscriptionController.deleteSubscription(subscriptionId); + + // Attempt to retrieve the subscription after delete + final ResponseEntity<InlineResponse2002> responseGetSubscription = + (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController + .getSubscription(subscriptionId); + + assertEquals(HttpStatus.NOT_FOUND, responseGetSubscription.getStatusCode()); + assertEquals(HttpStatus.NO_CONTENT, responseDelete.getStatusCode()); + } + + @Test + public void testDeleteSubscription_SubscripitonNotFoundInEtsiCatalogManager_SubscriptionDeletedFromLocalCache() + throws GeneralSecurityException { + final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest(); + final PkgmSubscription pkgmSubscription = buildPkgmSubscription(); + + mockRestServiceServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) + .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON)); + + mockRestServiceServer.expect(requestTo(msbEndpoint + "/" + ID)).andExpect(method(HttpMethod.DELETE)) + .andRespond(withStatus(HttpStatus.NOT_FOUND)); + + final ResponseEntity<InlineResponse2002> responsePost = + (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController + .postSubscriptionRequest(pkgmSubscriptionRequest); + + final Cache cache = cacheServiceProvider.getCache(Constants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE); + assertNotNull(cache.get(ID)); + + final ResponseEntity responseDelete = sol003PackageManagementSubscriptionController.deleteSubscription(ID); + + assertEquals(HttpStatus.NO_CONTENT, responseDelete.getStatusCode()); + assertNull(cache.get(ID)); + + } + + @Test + public void testFailDeleteSubscriptionWithInvalidSubscriptionId() throws URISyntaxException, InterruptedException { + final ResponseEntity<Void> responseDelete = (ResponseEntity<Void>) sol003PackageManagementSubscriptionController + .deleteSubscription("invalidSubscriptionId"); + assertEquals(HttpStatus.NOT_FOUND, responseDelete.getStatusCode()); + } + private PkgmSubscriptionRequest buildPkgmSubscriptionRequest() { final PkgmSubscriptionRequest pkgmSubscriptionRequest = new PkgmSubscriptionRequest(); final SubscriptionsFilter sub = buildSubscriptionsFilter(); @@ -227,11 +297,11 @@ public class Sol003PackageManagementSubscriptionControllerTest { } private PkgmSubscription buildPkgmSubscription() { - PkgmSubscription pkgmSubscription = new PkgmSubscription(); - PkgmNotificationsFilter pkgmNotificationsFilter = new PkgmNotificationsFilter(); - LinkSelf linkSelf = new LinkSelf(); - String id = UUID.randomUUID().toString(); - pkgmSubscription.setId(id); + final PkgmSubscription pkgmSubscription = new PkgmSubscription(); + final PkgmNotificationsFilter pkgmNotificationsFilter = new PkgmNotificationsFilter(); + final LinkSelf linkSelf = new LinkSelf(); + + pkgmSubscription.setId(ID); pkgmSubscription.setCallbackUri(msbEndpoint + "/" + pkgmSubscription.getId().toString()); pkgmSubscription.setFilter(pkgmNotificationsFilter); pkgmSubscription.setLinks(linkSelf); @@ -242,14 +312,14 @@ public class Sol003PackageManagementSubscriptionControllerTest { final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest(); final PkgmSubscription pkgmSubscription = buildPkgmSubscription(); - mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) + mockRestServiceServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON)); return pkgmSubscriptionRequest; } - private HttpHeaders buildHttpHeaders(String uri) throws URISyntaxException { + private HttpHeaders buildHttpHeaders(final String uri) throws URISyntaxException { final HttpHeaders headers = new HttpHeaders(); - URI myUri = new URI(uri); + final URI myUri = new URI(uri); headers.setLocation(myUri); return headers; } |