1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2020 Ericsson. 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.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement;
import static org.slf4j.LoggerFactory.getLogger;
import org.onap.so.adapters.vnfmadapter.packagemanagement.model.SubscriptionsAuthentication;
import org.onap.so.adapters.vnfmadapter.packagemanagement.model.SubscriptionsAuthentication.AuthTypeEnum;
import org.onap.so.adapters.vnfmadapter.rest.exceptions.InternalServerErrorException;
import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
import org.onap.so.configuration.rest.HttpHeadersProvider;
import org.onap.so.rest.service.HttpRestServiceProvider;
import org.slf4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
/**
* @author Waqas Ikram (waqas.ikram@est.tech)
* @author Andrew Lamb (andrew.a.lamb@est.tech)
*
*/
@Service
public class OAuthNotificationServiceProvider extends AbstractNotificationServiceProvider
implements NotificationServiceProvider {
private static final Logger logger = getLogger(OAuthNotificationServiceProvider.class);
@Override
public boolean send(final Object notification, final SubscriptionsAuthentication subscriptionsAuthentication,
final String callbackUri) {
logger.info("Sending notification to uri: {}", callbackUri);
final String token = getAccessToken(subscriptionsAuthentication);
if (token == null) {
logger.error("Failed to get access token");
return false;
}
final HttpHeadersProvider httpHeadersProvider = getHttpHeadersProvider(token);
final HttpRestServiceProvider httpRestServiceProvider = getHttpRestServiceProvider();
final ResponseEntity<Void> responseEntity = httpRestServiceProvider.postHttpRequest(notification, callbackUri,
httpHeadersProvider.getHttpHeaders(), Void.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
logger.info("Notification sent successfully.");
return true;
}
logger.error("Failed to send notification.");
return false;
}
@Override
public AuthTypeEnum getAuthType() {
return AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS;
}
private BasicHttpHeadersProvider getHttpHeadersProvider(final String token) {
final String authHeader = "Bearer " + token;
return new BasicHttpHeadersProvider(authHeader);
}
private String getAccessToken(final SubscriptionsAuthentication subscriptionsAuthentication) {
logger.info("Requesting Access Token.");
final String tokenEndpoint = subscriptionsAuthentication.getParamsOauth2ClientCredentials().getTokenEndpoint();
final HttpHeadersProvider httpHeadersProvider = getBasicHttpHeadersProviderWithBasicAuth(
subscriptionsAuthentication.getParamsOauth2ClientCredentials().getClientId(),
subscriptionsAuthentication.getParamsOauth2ClientCredentials().getClientPassword());
final HttpRestServiceProvider httpRestServiceProvider = getHttpRestServiceProvider();
final ResponseEntity<OAuthTokenResponse> responseEntity = httpRestServiceProvider.postHttpRequest(null,
tokenEndpoint, httpHeadersProvider.getHttpHeaders(), OAuthTokenResponse.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
if (responseEntity.getBody() != null) {
logger.info("Returning Access Token.");
return responseEntity.getBody().getAccessToken();
}
}
final String errorMessage = "An error occurred. Unable to retrieve OAuth Token from VNFM for notification.";
logger.error(errorMessage);
throw new InternalServerErrorException(errorMessage);
}
}
|