2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement;
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.util.List;
25 import java.util.Optional;
26 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.notification.model.PkgChangeNotification;
27 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.notification.model.PkgOnboardingNotification;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication.AuthTypeEnum;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.notification.model.VnfPackageChangeNotification;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.notification.model.VnfPackageOnboardingNotification;
33 import org.onap.so.adapters.vnfmadapter.rest.exceptions.AuthenticationTypeNotSupportedException;
34 import org.onap.so.adapters.vnfmadapter.rest.exceptions.ConversionFailedException;
35 import org.onap.so.adapters.vnfmadapter.rest.exceptions.NotificationTypeNotSupportedException;
36 import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionNotFoundException;
37 import org.slf4j.Logger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.core.convert.ConversionService;
40 import org.springframework.stereotype.Service;
43 * Manages package management subscription notifications to the VNFMs
45 * @author Andrew Lamb (andrew.a.lamb@est.tech)
49 public class NotificationManager {
51 private static final Logger logger = getLogger(NotificationManager.class);
52 private final ConversionService conversionService;
53 private final SubscriptionManager subscriptionManager;
54 private final NotificationServiceProviderFactory notificationServiceProviderFactory;
57 public NotificationManager(final SubscriptionManager subscriptionManager, final ConversionService conversionService,
58 final NotificationServiceProviderFactory notificationServiceProviderFactory) {
59 this.subscriptionManager = subscriptionManager;
60 this.conversionService = conversionService;
61 this.notificationServiceProviderFactory = notificationServiceProviderFactory;
65 * Process a subscription notification. Checks for a subscription request stored in the adapter and if there is, it
66 * sends the notification to the subscribed vnfm.
68 * @param notification the notification to send to the vnfm
69 * @param subscriptionId the id of the subscription request
70 * @return true if the notification is successfully sent
72 public boolean processSubscriptionNotification(final Object notification, final String subscriptionId) {
73 final Optional<PkgmSubscriptionRequest> optionalSubscription =
74 subscriptionManager.getSubscriptionRequest(subscriptionId);
75 if (optionalSubscription.isPresent()) {
76 final PkgmSubscriptionRequest subscriptionRequest = optionalSubscription.get();
77 return notifyVnfm(subscriptionRequest, notification);
79 final String errorMessage = "No subscription found with subscriptionId " + subscriptionId
80 + ". Unable to forward notification to subscriber.";
81 logger.error(errorMessage);
82 throw new SubscriptionNotFoundException(errorMessage);
85 private boolean notifyVnfm(final PkgmSubscriptionRequest subscriptionRequest, final Object notification) {
86 if (!(notification instanceof PkgOnboardingNotification) && !(notification instanceof PkgChangeNotification)) {
87 final String errorMessage =
88 "An error occurred. Notification type not supported for: " + notification.getClass();
89 logger.error(errorMessage);
90 throw new NotificationTypeNotSupportedException(errorMessage);
93 final SubscriptionsAuthentication subscriptionsAuthentication = subscriptionRequest.getAuthentication();
94 final AuthTypeEnum authType = getAuthType(subscriptionsAuthentication.getAuthType());
95 final NotificationServiceProvider sender = notificationServiceProviderFactory.getNotificationSender(authType);
97 final Object vnfmNotificationObject = convertEtsiCatalogNotification(notification);
99 if (sender.send(vnfmNotificationObject, subscriptionsAuthentication, subscriptionRequest.getCallbackUri())) {
100 logger.info("Notification delivered successfully {}", notification);
104 logger.error("Failed to deliver notification.");
108 private SubscriptionsAuthentication.AuthTypeEnum getAuthType(final List<AuthTypeEnum> authTypes) {
109 if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT)) {
110 return SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT;
112 if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS)) {
113 return SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS;
115 if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.BASIC)) {
116 return SubscriptionsAuthentication.AuthTypeEnum.BASIC;
118 final String errorMessage =
119 "An error occurred. No supported authentication type provided in subscription request.";
120 logger.error(errorMessage);
121 throw new AuthenticationTypeNotSupportedException(errorMessage);
124 private Object convertEtsiCatalogNotification(final Object etsiCatalogNotification) {
125 logger.info("Converting notification:\n {}", etsiCatalogNotification);
126 if (conversionService.canConvert(etsiCatalogNotification.getClass(), VnfPackageOnboardingNotification.class)) {
127 return conversionService.convert(etsiCatalogNotification, VnfPackageOnboardingNotification.class);
128 } else if (conversionService.canConvert(etsiCatalogNotification.getClass(),
129 VnfPackageChangeNotification.class)) {
130 return conversionService.convert(etsiCatalogNotification, VnfPackageChangeNotification.class);
132 final String errorMessage = "An error occurred. Unable to convert provided notification object.";
133 logger.error(errorMessage + "\n" + etsiCatalogNotification);
134 throw new ConversionFailedException(errorMessage);