diff options
author | Max Benjamin <max.benjamin@att.com> | 2020-02-10 15:49:02 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-02-10 15:49:02 +0000 |
commit | ac1a59fc6731c6261712eac5bec76816b11d1096 (patch) | |
tree | 62aa30102492b2fff3490dc87cc0c1e167ae255f /adapters/mso-ve-vnfm-adapter/src/main | |
parent | 93c822c255db9b1f8ee9f68e6677417e38919abe (diff) | |
parent | 9a13ad8797811159adea21e1d9eb819445fbf492 (diff) |
Merge "Added Basic Authorization in sending Subscription to VNFM"
Diffstat (limited to 'adapters/mso-ve-vnfm-adapter/src/main')
6 files changed, 90 insertions, 17 deletions
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<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; 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<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)); + } +} 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; @@ -50,11 +53,23 @@ public class SubscriberService { 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<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; } } |