From 9a13ad8797811159adea21e1d9eb819445fbf492 Mon Sep 17 00:00:00 2001 From: Piotr Borelowski Date: Thu, 6 Feb 2020 17:22:57 +0100 Subject: Added Basic Authorization in sending Subscription to VNFM Ve-Vnfm (SOL002) Adapter project Issue-ID: SO-2574 Signed-off-by: Piotr Borelowski Change-Id: I819e95c4e212695bc38ab5b7d221be712f87320e --- .../onap/so/adapters/vevnfm/aai/AaiConnection.java | 6 +-- .../configuration/ApplicationConfiguration.java | 12 +++++- .../provider/AuthorizationHeadersProvider.java | 48 ++++++++++++++++++++++ .../so/adapters/vevnfm/service/StartupService.java | 11 ++--- .../adapters/vevnfm/service/SubscriberService.java | 21 ++++++++-- .../vevnfm/subscription/SubscribeSender.java | 9 ++-- .../vevnfm/service/StartupServiceTest.java | 33 ++++++++++----- .../vevnfm/subscription/SubscribeSenderTest.java | 8 ++-- 8 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java (limited to 'adapters') diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java index 91a79b29bf..188b6718f2 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java @@ -40,7 +40,7 @@ public class AaiConnection { private static final int FIRST_INDEX = 0; - public String receiveVnfm() { + public EsrSystemInfo receiveVnfm() { final AAIResourcesClient resourcesClient = new AAIResourcesClient(); final Optional response = resourcesClient.get(EsrVnfmList.class, AAIUriFactory.createResourceUri(AAIObjectType.VNFM_LIST)); @@ -61,7 +61,7 @@ public class AaiConnection { return null; } - private String receiveVnfmServiceUrl(final AAIResourcesClient resourcesClient, final String vnfmId) { + private EsrSystemInfo receiveVnfmServiceUrl(final AAIResourcesClient resourcesClient, final String vnfmId) { final Optional response = resourcesClient.get(EsrVnfm.class, AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).depth(Depth.ONE)); @@ -74,7 +74,7 @@ public class AaiConnection { return null; } - return esrSystemInfo.get(FIRST_INDEX).getServiceUrl(); + return esrSystemInfo.get(FIRST_INDEX); } return null; diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java index 108b2ee896..411572ff5b 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java @@ -20,6 +20,8 @@ package org.onap.so.adapters.vevnfm.configuration; +import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider; +import org.onap.so.configuration.rest.HttpHeadersProvider; import org.onap.so.rest.service.HttpRestServiceProvider; import org.onap.so.rest.service.HttpRestServiceProviderImpl; import org.springframework.context.annotation.Bean; @@ -30,7 +32,13 @@ import org.springframework.web.client.RestTemplate; public class ApplicationConfiguration { @Bean - public HttpRestServiceProvider restProvider(final RestTemplate restTemplate) { - return new HttpRestServiceProviderImpl(restTemplate); + public AuthorizationHeadersProvider headersProvider() { + return new AuthorizationHeadersProvider(); + } + + @Bean + public HttpRestServiceProvider restProvider(final RestTemplate restTemplate, + final HttpHeadersProvider headersProvider) { + return new HttpRestServiceProviderImpl(restTemplate, headersProvider); } } 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 new file mode 100644 index 0000000000..eca5240cb5 --- /dev/null +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java @@ -0,0 +1,48 @@ +/*- + * ============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 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 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); + } + } + + private boolean isPreviousAuthorizationBlank() { + return previousAuthorization == null || previousAuthorization.isEmpty() + || Strings.isBlank(previousAuthorization.get(0)); + } +} diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java index 7a9ec9659e..dfbafa223c 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java @@ -21,6 +21,7 @@ package org.onap.so.adapters.vevnfm.service; import org.apache.logging.log4j.util.Strings; +import org.onap.aai.domain.yang.EsrSystemInfo; import org.onap.so.adapters.vevnfm.aai.AaiConnection; import org.onap.so.adapters.vevnfm.exception.VeVnfmException; import org.springframework.beans.factory.annotation.Autowired; @@ -35,16 +36,16 @@ public class StartupService { @Autowired private SubscriberService subscriberService; - private static void isValid(final String endpoint) throws VeVnfmException { - if (Strings.isBlank(endpoint)) { + private static void isValid(final EsrSystemInfo info) throws VeVnfmException { + if (Strings.isBlank(info.getServiceUrl())) { throw new VeVnfmException("No 'url' field in VNFM info"); } } public void run() throws Exception { - final String endpoint = aaiConnection.receiveVnfm(); - isValid(endpoint); - final boolean done = subscriberService.subscribe(endpoint); + final EsrSystemInfo info = aaiConnection.receiveVnfm(); + isValid(info); + final boolean done = subscriberService.subscribe(info); if (!done) { throw new VeVnfmException("Could not subscribe to VNFM"); 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 c1a56fb452..0e77ce4073 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 @@ -20,7 +20,10 @@ package org.onap.so.adapters.vevnfm.service; +import com.squareup.okhttp.Credentials; import java.util.Collections; +import org.onap.aai.domain.yang.EsrSystemInfo; +import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider; import org.onap.so.adapters.vevnfm.subscription.SubscribeSender; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication; @@ -49,12 +52,24 @@ public class SubscriberService { @Value("${spring.security.usercredentials[0].openpass}") private String openpass; + @Autowired + private AuthorizationHeadersProvider headersProvider; + @Autowired private SubscribeSender sender; - public boolean subscribe(final String endpoint) { - final LccnSubscriptionRequest request = createRequest(); - return sender.send(endpoint, request); + private static String getAuthorization(final EsrSystemInfo info) { + return Credentials.basic(info.getUserName(), info.getPassword()); + } + + public boolean subscribe(final EsrSystemInfo info) { + try { + headersProvider.addAuthorization(getAuthorization(info)); + final LccnSubscriptionRequest request = createRequest(); + return sender.send(info, request); + } finally { + headersProvider.resetPrevious(); + } } private LccnSubscriptionRequest createRequest() { diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java index 1b3a049bcf..8fdfb41d26 100644 --- a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java +++ b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java @@ -20,6 +20,7 @@ package org.onap.so.adapters.vevnfm.subscription; +import org.onap.aai.domain.yang.EsrSystemInfo; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; import org.onap.so.rest.service.HttpRestServiceProvider; import org.slf4j.Logger; @@ -41,8 +42,8 @@ public class SubscribeSender { @Autowired private HttpRestServiceProvider restProvider; - public boolean send(final String endpoint, final LccnSubscriptionRequest request) { - final ResponseEntity response = restProvider.postHttpRequest(request, getUrl(endpoint), String.class); + public boolean send(final EsrSystemInfo info, final LccnSubscriptionRequest request) { + final ResponseEntity response = restProvider.postHttpRequest(request, getUrl(info), String.class); final HttpStatus statusCode = response.getStatusCode(); final String body = response.getBody(); @@ -52,7 +53,7 @@ public class SubscribeSender { return HttpStatus.CREATED == statusCode; } - private String getUrl(final String endpoint) { - return endpoint + vnfmSubscription; + private String getUrl(final EsrSystemInfo info) { + return info.getServiceUrl() + vnfmSubscription; } } diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java index 8c480d0415..0f9c23e261 100644 --- a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java +++ b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java @@ -21,11 +21,14 @@ package org.onap.so.adapters.vevnfm.service; import static org.mockito.Mockito.*; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.EsrSystemInfo; import org.onap.so.adapters.vevnfm.aai.AaiConnection; import org.onap.so.adapters.vevnfm.exception.VeVnfmException; @@ -41,38 +44,46 @@ public class StartupServiceTest { @InjectMocks private StartupService startupService; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testSuccess() throws Exception { // given - final String endpoint = "lh"; - - when(aaiConnection.receiveVnfm()).thenReturn(endpoint); - when(subscriberService.subscribe(endpoint)).thenReturn(true); + final EsrSystemInfo info = new EsrSystemInfo(); + info.setServiceUrl("lh"); + when(aaiConnection.receiveVnfm()).thenReturn(info); + when(subscriberService.subscribe(info)).thenReturn(true); // when startupService.run(); // then verify(aaiConnection, times(1)).receiveVnfm(); - verify(subscriberService, times(1)).subscribe(endpoint); + verify(subscriberService, times(1)).subscribe(info); } - @Test(expected = VeVnfmException.class) + @Test public void testFailureAai() throws Exception { // given - when(aaiConnection.receiveVnfm()).thenReturn(null); + final EsrSystemInfo info = new EsrSystemInfo(); + when(aaiConnection.receiveVnfm()).thenReturn(info); + + thrown.expect(VeVnfmException.class); // when startupService.run(); } - @Test(expected = VeVnfmException.class) + @Test public void testFailureSubscriber() throws Exception { // given - final String endpoint = "lh"; + final EsrSystemInfo info = new EsrSystemInfo(); + info.setServiceUrl("lh"); + when(aaiConnection.receiveVnfm()).thenReturn(info); + when(subscriberService.subscribe(info)).thenReturn(false); - when(aaiConnection.receiveVnfm()).thenReturn(endpoint); - when(subscriberService.subscribe(endpoint)).thenReturn(false); + thrown.expect(VeVnfmException.class); // when startupService.run(); diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java index 62a624a983..d1fda0eee6 100644 --- a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java +++ b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java @@ -31,6 +31,7 @@ import org.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.onap.aai.domain.yang.EsrSystemInfo; import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; import org.springframework.beans.factory.annotation.Autowired; @@ -79,16 +80,17 @@ public class SubscribeSenderTest { @Test public void testSuccess() { // given - final String endpoint = "lh"; + final EsrSystemInfo info = new EsrSystemInfo(); + info.setServiceUrl("lh"); final LccnSubscriptionRequest request = new LccnSubscriptionRequest(); - mockRestServer.expect(once(), requestTo(SLASH + endpoint + vnfmSubscription)) + mockRestServer.expect(once(), requestTo(SLASH + info.getServiceUrl() + vnfmSubscription)) .andExpect(header(CONTENT_TYPE, CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE))) .andExpect(method(HttpMethod.POST)).andExpect(content().json(GSON.toJson(request))) .andRespond(withStatus(HttpStatus.CREATED).body(MINIMAL_JSON_CONTENT)); // when - final boolean done = sender.send(endpoint, request); + final boolean done = sender.send(info, request); // then assertTrue(done); -- cgit 1.2.3-korg