Added Basic Authorization in sending Subscription to VNFM 56/101256/9
authorPiotr Borelowski <p.borelowski@partner.samsung.com>
Thu, 6 Feb 2020 16:22:57 +0000 (17:22 +0100)
committerPiotr Borelowski <p.borelowski@partner.samsung.com>
Mon, 10 Feb 2020 14:39:08 +0000 (15:39 +0100)
Ve-Vnfm (SOL002) Adapter project

Issue-ID: SO-2574
Signed-off-by: Piotr Borelowski <p.borelowski@partner.samsung.com>
Change-Id: I819e95c4e212695bc38ab5b7d221be712f87320e

adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/provider/AuthorizationHeadersProvider.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java
adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java
adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java

index 91a79b2..188b671 100644 (file)
@@ -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<EsrVnfmList> 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<EsrVnfm> 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;
index 108b2ee..411572f 100644 (file)
@@ -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 (file)
index 0000000..eca5240
--- /dev/null
@@ -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<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);
+        }
+    }
+
+    private boolean isPreviousAuthorizationBlank() {
+        return previousAuthorization == null || previousAuthorization.isEmpty()
+                || Strings.isBlank(previousAuthorization.get(0));
+    }
+}
index 7a9ec96..dfbafa2 100644 (file)
@@ -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");
index c1a56fb..0e77ce4 100644 (file)
 
 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() {
index 1b3a049..8fdfb41 100644 (file)
@@ -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<String> response = restProvider.postHttpRequest(request, getUrl(endpoint), String.class);
+    public boolean send(final EsrSystemInfo info, final LccnSubscriptionRequest request) {
+        final ResponseEntity<String> 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;
     }
 }
index 8c480d0..0f9c23e 100644 (file)
 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();
index 62a624a..d1fda0e 100644 (file)
@@ -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);