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.rest;
23 import static org.onap.so.adapters.vnfmadapter.common.CommonConstants.ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL;
24 import static org.slf4j.LoggerFactory.getLogger;
25 import java.util.AbstractMap;
26 import java.util.Map.Entry;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.so.adapters.vnfmadapter.etsicatalog.notification.model.PkgChangeNotification;
29 import org.onap.so.adapters.vnfmadapter.etsicatalog.notification.model.PkgOnboardingNotification;
30 import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.NotificationManager;
31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.InternalServerErrorException;
32 import org.onap.so.adapters.vnfmadapter.rest.exceptions.NotificationTypeNotSupportedException;
33 import org.slf4j.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import com.google.gson.Gson;
42 import com.google.gson.GsonBuilder;
43 import com.google.gson.JsonObject;
44 import com.google.gson.JsonParser;
47 * This controller handles the ETSI Subscription Notification Endpoints.
49 * @author Ronan Kenny (ronan.kenny@est.tech)
50 * @author Gareth Roper (gareth.roper@est.tech)
51 * @author Andrew Lamb (andrew.a.lamb@est.tech)
54 @RequestMapping(value = ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL)
55 public class EtsiSubscriptionNotificationController {
57 private static final Logger logger = getLogger(EtsiSubscriptionNotificationController.class);
58 private final NotificationManager notificationManager;
59 private final Gson gson;
62 public EtsiSubscriptionNotificationController(final NotificationManager notificationManager) {
63 this.notificationManager = notificationManager;
64 this.gson = new GsonBuilder().create();
67 @GetMapping(value = "/notification")
68 public ResponseEntity<Void> testSubscriptionNotificationEndPoint() {
69 logger.debug("Testing Notification Endpoint");
70 return ResponseEntity.noContent().build();
74 * POST notification on to subscriber.
76 * @param notification The notification to send.
77 * @return Response Code: 204 No Content if Successful, ProblemDetails Object if not.
79 @PostMapping(value = "/notification", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
80 public ResponseEntity<?> postSubscriptionNotification(@RequestBody final String notification) {
81 logger.info("Posting subscription notification \n{}", notification);
83 final Entry<String, Object> notificationObject = getNotificationObject(notification);
84 if (notificationManager.processSubscriptionNotification(notificationObject.getValue(),
85 notificationObject.getKey())) {
86 logger.info("Notification Delivered Successfully");
87 return ResponseEntity.noContent().build();
89 final String errorMessage = "An error occurred. Sending of notification to VNFM failed.";
90 logger.error(errorMessage);
91 throw new InternalServerErrorException(errorMessage);
94 private Entry<String, Object> getNotificationObject(final String notification) {
95 final String notificationType = getNotificationType(notification);
96 if (PkgOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION.getValue()
97 .equals(notificationType)) {
98 final PkgOnboardingNotification pkgOnboardingNotification =
99 gson.fromJson(notification, PkgOnboardingNotification.class);
100 logger.info("Onboarding notification received:\n{}", pkgOnboardingNotification);
101 return new AbstractMap.SimpleEntry<>(pkgOnboardingNotification.getSubscriptionId(),
102 pkgOnboardingNotification);
104 if (PkgChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.getValue()
105 .equals(notificationType)) {
106 final PkgChangeNotification pkgChangeNotification =
107 gson.fromJson(notification, PkgChangeNotification.class);
108 logger.info("Change notification received:\n{}", pkgChangeNotification);
109 return new AbstractMap.SimpleEntry<>(pkgChangeNotification.getSubscriptionId(), pkgChangeNotification);
113 final String errorMessage = "An error occurred. Notification type not supported for: " + notificationType;
114 logger.error(errorMessage);
115 throw new NotificationTypeNotSupportedException(errorMessage);
119 private String getNotificationType(final String notification) {
121 final JsonParser parser = new JsonParser();
122 final JsonObject element = (JsonObject) parser.parse(notification);
123 return element.get("notificationType").getAsString();
124 } catch (final Exception e) {
125 logger.error("An error occurred processing notificiation: {}", e.getMessage());
127 throw new NotificationTypeNotSupportedException(
128 "Unable to parse notification type in object \n" + notification);