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.etsisol003adapter.pkgm.rest;
23 import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL;
24 import static org.slf4j.LoggerFactory.getLogger;
25 import java.io.IOException;
26 import java.time.LocalDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.AbstractMap;
29 import java.util.Map.Entry;
30 import javax.ws.rs.core.MediaType;
31 import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.PkgChangeNotification;
32 import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.PkgOnboardingNotification;
33 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.InternalServerErrorException;
34 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.NotificationTypeNotSupportedException;
35 import org.onap.so.adapters.etsisol003adapter.pkgm.subscriptionmanagement.NotificationManager;
36 import org.slf4j.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46 import com.google.gson.JsonObject;
47 import com.google.gson.JsonParser;
48 import com.google.gson.TypeAdapter;
49 import com.google.gson.stream.JsonReader;
50 import com.google.gson.stream.JsonWriter;
53 * This controller handles the ETSI Subscription Notification Endpoints.
55 * @author Ronan Kenny (ronan.kenny@est.tech)
56 * @author Gareth Roper (gareth.roper@est.tech)
57 * @author Andrew Lamb (andrew.a.lamb@est.tech)
60 @RequestMapping(value = ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL)
61 public class EtsiSubscriptionNotificationController {
63 private static final Logger logger = getLogger(EtsiSubscriptionNotificationController.class);
64 private final NotificationManager notificationManager;
65 private final Gson gson;
68 public EtsiSubscriptionNotificationController(final NotificationManager notificationManager) {
69 this.notificationManager = notificationManager;
70 this.gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter()).create();
73 @GetMapping(value = "/notification")
74 public ResponseEntity<Void> testSubscriptionNotificationEndPoint() {
75 logger.debug("Testing Notification Endpoint");
76 return ResponseEntity.noContent().build();
80 * POST notification on to subscriber.
82 * @param notification The notification to send.
83 * @return Response Code: 204 No Content if Successful, ProblemDetails Object if not.
85 @PostMapping(value = "/notification", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
86 public ResponseEntity<?> postSubscriptionNotification(@RequestBody final Object notification) {
87 logger.info("Posting subscription notification class: {} \n{}", notification.getClass(), notification);
88 final String notificationString = gson.toJson(notification);
90 final Entry<String, Object> notificationObject = getNotificationObject(notificationString);
91 if (notificationManager.processSubscriptionNotification(notificationObject.getValue(),
92 notificationObject.getKey())) {
93 logger.info("Notification Delivered Successfully");
94 return ResponseEntity.noContent().build();
96 final String errorMessage = "An error occurred. Sending of notification to VNFM failed.";
97 logger.error(errorMessage);
98 throw new InternalServerErrorException(errorMessage);
101 private Entry<String, Object> getNotificationObject(final String notification) {
102 logger.info("getNotificationObject() notification: {}", notification);
103 final String notificationType = getNotificationType(notification);
104 if (PkgOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION.getValue()
105 .equals(notificationType)) {
106 final PkgOnboardingNotification pkgOnboardingNotification =
107 gson.fromJson(notification, PkgOnboardingNotification.class);
108 logger.info("Onboarding notification received:\n{}", pkgOnboardingNotification);
109 return new AbstractMap.SimpleEntry<>(pkgOnboardingNotification.getSubscriptionId(),
110 pkgOnboardingNotification);
112 if (PkgChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.getValue()
113 .equals(notificationType)) {
114 final PkgChangeNotification pkgChangeNotification =
115 gson.fromJson(notification, PkgChangeNotification.class);
116 logger.info("Change notification received:\n{}", pkgChangeNotification);
117 return new AbstractMap.SimpleEntry<>(pkgChangeNotification.getSubscriptionId(), pkgChangeNotification);
121 final String errorMessage = "An error occurred. Notification type not supported for: " + notificationType;
122 logger.error(errorMessage);
123 throw new NotificationTypeNotSupportedException(errorMessage);
127 private String getNotificationType(final String notification) {
129 logger.info("getNotificationType() notification: {}", notification);
130 final JsonParser parser = new JsonParser();
131 final JsonObject element = (JsonObject) parser.parse(notification);
132 return element.get("notificationType").getAsString();
133 } catch (final Exception e) {
134 logger.error("An error occurred processing notificiation: {}", e.getMessage());
136 throw new NotificationTypeNotSupportedException(
137 "Unable to parse notification type in object \n" + notification);
140 public static class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
142 private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
145 public void write(final JsonWriter out, final LocalDateTime localDateTime) throws IOException {
146 if (localDateTime == null) {
149 out.value(FORMATTER.format(localDateTime));
154 public LocalDateTime read(final JsonReader in) throws IOException {
160 final String dateTime = in.nextString();
161 return LocalDateTime.parse(dateTime, FORMATTER);