Refactor SOL003 Adapter to organize its modules
[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 / rest / EtsiSubscriptionNotificationController.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.rest;
22
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.util.AbstractMap;
26 import java.util.Map.Entry;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgChangeNotification;
29 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgOnboardingNotification;
30 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest.exceptions.InternalServerErrorException;
31 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest.exceptions.NotificationTypeNotSupportedException;
32 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.subscriptionmanagement.NotificationManager;
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;
45
46 /**
47  * This controller handles the ETSI Subscription Notification Endpoints.
48  *
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)
52  */
53 @Controller
54 @RequestMapping(value = ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL)
55 public class EtsiSubscriptionNotificationController {
56
57     private static final Logger logger = getLogger(EtsiSubscriptionNotificationController.class);
58     private final NotificationManager notificationManager;
59     private final Gson gson;
60
61     @Autowired
62     public EtsiSubscriptionNotificationController(final NotificationManager notificationManager) {
63         this.notificationManager = notificationManager;
64         this.gson = new GsonBuilder().create();
65     }
66
67     @GetMapping(value = "/notification")
68     public ResponseEntity<Void> testSubscriptionNotificationEndPoint() {
69         logger.debug("Testing Notification Endpoint");
70         return ResponseEntity.noContent().build();
71     }
72
73     /**
74      * POST notification on to subscriber.
75      * 
76      * @param notification The notification to send.
77      * @return Response Code: 204 No Content if Successful, ProblemDetails Object if not.
78      */
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);
82
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();
88         }
89         final String errorMessage = "An error occurred.  Sending of notification to VNFM failed.";
90         logger.error(errorMessage);
91         throw new InternalServerErrorException(errorMessage);
92     }
93
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);
103         }
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);
110
111         }
112
113         final String errorMessage = "An error occurred.  Notification type not supported for: " + notificationType;
114         logger.error(errorMessage);
115         throw new NotificationTypeNotSupportedException(errorMessage);
116
117     }
118
119     private String getNotificationType(final String notification) {
120         try {
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());
126         }
127         throw new NotificationTypeNotSupportedException(
128                 "Unable to parse notification type in object \n" + notification);
129     }
130
131 }