Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / main / java / org / onap / so / adapters / etsi / sol003 / adapter / packagemanagement / subscriptionmanagement / NotificationManager.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.subscriptionmanagement;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.util.List;
25 import java.util.Optional;
26 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgChangeNotification;
27 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgOnboardingNotification;
28 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.vnfm.notification.model.VnfPackageChangeNotification;
29 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.vnfm.notification.model.VnfPackageOnboardingNotification;
30 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.PkgmSubscriptionRequest;
31 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthentication;
32 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthentication.AuthTypeEnum;
33 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest.exceptions.AuthenticationTypeNotSupportedException;
34 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest.exceptions.ConversionFailedException;
35 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest.exceptions.NotificationTypeNotSupportedException;
36 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.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;
41
42 /**
43  * Manages package management subscription notifications to the VNFMs
44  *
45  * @author Andrew Lamb (andrew.a.lamb@est.tech)
46  *
47  */
48 @Service
49 public class NotificationManager {
50
51     private static final Logger logger = getLogger(NotificationManager.class);
52     private final ConversionService conversionService;
53     private final SubscriptionManager subscriptionManager;
54     private final NotificationServiceProviderFactory notificationServiceProviderFactory;
55
56     @Autowired
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;
62     }
63
64     /**
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.
67      * 
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
71      */
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);
78         }
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);
83     }
84
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);
91         }
92
93         final SubscriptionsAuthentication subscriptionsAuthentication = subscriptionRequest.getAuthentication();
94         final AuthTypeEnum authType = getAuthType(subscriptionsAuthentication.getAuthType());
95         final NotificationServiceProvider sender = notificationServiceProviderFactory.getNotificationSender(authType);
96
97         final Object vnfmNotificationObject = convertEtsiCatalogNotification(notification);
98
99         if (sender.send(vnfmNotificationObject, subscriptionsAuthentication, subscriptionRequest.getCallbackUri())) {
100             logger.info("Notification delivered successfully {}", notification);
101             return true;
102         }
103
104         logger.error("Failed to deliver notification.");
105         return false;
106     }
107
108     private SubscriptionsAuthentication.AuthTypeEnum getAuthType(final List<AuthTypeEnum> authTypes) {
109         if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT)) {
110             return SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT;
111         }
112         if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS)) {
113             return SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS;
114         }
115         if (authTypes.contains(SubscriptionsAuthentication.AuthTypeEnum.BASIC)) {
116             return SubscriptionsAuthentication.AuthTypeEnum.BASIC;
117         }
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);
122     }
123
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);
131         }
132         final String errorMessage = "An error occurred. Unable to convert provided notification object.";
133         logger.error(errorMessage + "\n" + etsiCatalogNotification);
134         throw new ConversionFailedException(errorMessage);
135     }
136
137 }