|
| 1 | +/* |
| 2 | + Copyright 2017 Ericsson AB. |
| 3 | + For a full list of individual contributors, please see the commit history. |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | +package com.ericsson.ei.subscriptionhandler; |
| 15 | + |
| 16 | +import java.text.DateFormat; |
| 17 | +import java.text.SimpleDateFormat; |
| 18 | +import java.util.Date; |
| 19 | + |
| 20 | +import javax.annotation.PostConstruct; |
| 21 | + |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.annotation.Value; |
| 26 | +import org.springframework.http.HttpStatus; |
| 27 | +import org.springframework.stereotype.Component; |
| 28 | + |
| 29 | +import com.ericsson.ei.mongodbhandler.MongoDBHandler; |
| 30 | +import com.fasterxml.jackson.databind.JsonNode; |
| 31 | +import com.mongodb.BasicDBObject; |
| 32 | +import com.mongodb.util.JSON; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class represents the REST POST notification mechanism and the alternate |
| 36 | + * way to save the aggregatedObject details in the database when the |
| 37 | + * notification fails. |
| 38 | + * |
| 39 | + * @author xjibbal |
| 40 | + * |
| 41 | + */ |
| 42 | + |
| 43 | +@Component |
| 44 | +public class InformSubscription { |
| 45 | + |
| 46 | + @Value("${notification.failAttempt}") |
| 47 | + private int failAttempt; |
| 48 | + |
| 49 | + @Value("${missedNotificationCollectionName}") |
| 50 | + private String missedNotificationCollectionName; |
| 51 | + |
| 52 | + @Value("${missedNotificationDataBaseName}") |
| 53 | + private String missedNotificationDataBaseName; |
| 54 | + |
| 55 | + @Value("${notification.ttl.value}") |
| 56 | + private int ttlValue; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + SpringRestTemplate restTemplate; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + MongoDBHandler mongoDBHandler; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + SendMail sendMail; |
| 66 | + |
| 67 | + static Logger log = (Logger) LoggerFactory.getLogger(InformSubscription.class); |
| 68 | + |
| 69 | + /** |
| 70 | + * This method extracts the mode of notification through which the |
| 71 | + * subscriber should be notified, from the subscription Object. And if the |
| 72 | + * notification fails, then it saved in the database. |
| 73 | + * |
| 74 | + * @param aggregatedObject |
| 75 | + * @param subscriptionJson |
| 76 | + */ |
| 77 | + public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson) { |
| 78 | + String subscriptionName = subscriptionJson.get("subscriptionName").toString().replaceAll("^\"|\"$", ""); |
| 79 | + log.info("SubscriptionName : " + subscriptionName); |
| 80 | + String notificationType = subscriptionJson.get("notificationType").toString().replaceAll("^\"|\"$", ""); |
| 81 | + log.info("NotificationType : " + notificationType); |
| 82 | + String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll("^\"|\"$", ""); |
| 83 | + log.info("NotificationMeta : " + notificationMeta); |
| 84 | + if (notificationType.trim().equals("REST_POST")) { |
| 85 | + log.info("Notification through REST_POST"); |
| 86 | + int result = restTemplate.postData(aggregatedObject, notificationMeta); |
| 87 | + if (result == HttpStatus.OK.value()) { |
| 88 | + log.info("The result is : " + result); |
| 89 | + } else { |
| 90 | + for (int i = 0; i < failAttempt; i++) { |
| 91 | + result = restTemplate.postData(aggregatedObject, notificationMeta); |
| 92 | + log.info("After trying for " + (i + 1) + " times, the result is : " + result); |
| 93 | + if (result == HttpStatus.OK.value()) |
| 94 | + break; |
| 95 | + } |
| 96 | + if (result != HttpStatus.OK.value()) { |
| 97 | + String input = prepareMissedNotification(aggregatedObject, subscriptionName, notificationMeta); |
| 98 | + log.info("Input missed Notification document : " + input); |
| 99 | + mongoDBHandler.createTTLIndex(missedNotificationDataBaseName, missedNotificationCollectionName, |
| 100 | + "Time", ttlValue); |
| 101 | + boolean output = mongoDBHandler.insertDocument(missedNotificationDataBaseName, |
| 102 | + missedNotificationCollectionName, input); |
| 103 | + log.info("The output of insertion of missed Notification : " + output); |
| 104 | + if (output == false) { |
| 105 | + log.info("failed to insert the notification into database"); |
| 106 | + } else |
| 107 | + log.info("Notification saved in the database"); |
| 108 | + } |
| 109 | + if (notificationType.equals("EMAIL")) { |
| 110 | + log.info("Notification through EMAIL"); |
| 111 | + sendMail.sendMail(notificationMeta, aggregatedObject); |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * This method saves the missed Notification into a single document along |
| 120 | + * with Subscription name, notification meta and time period. |
| 121 | + * |
| 122 | + * @param aggregatedObject |
| 123 | + * @param subscriptionName |
| 124 | + * @param notificationType |
| 125 | + * |
| 126 | + * @return String |
| 127 | + */ |
| 128 | + public String prepareMissedNotification(String aggregatedObject, String subscriptionName, String notificationMeta) { |
| 129 | + String time = null; |
| 130 | + Date date = null; |
| 131 | + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| 132 | + date = new Date(); |
| 133 | + time = dateFormat.format(date); |
| 134 | + try { |
| 135 | + date = dateFormat.parse(time); |
| 136 | + } catch (Exception e) { |
| 137 | + log.info(e.getMessage(), e); |
| 138 | + } |
| 139 | + BasicDBObject document = new BasicDBObject(); |
| 140 | + document.put("subscriptionName", subscriptionName); |
| 141 | + document.put("notificationMeta", notificationMeta); |
| 142 | + document.put("Time", date); |
| 143 | + document.put("AggregatedObject", JSON.parse(aggregatedObject)); |
| 144 | + return document.toString(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * This method is responsible to display the configurable application |
| 149 | + * properties and to create TTL index on the missed Notification collection. |
| 150 | + */ |
| 151 | + @PostConstruct |
| 152 | + public void init() { |
| 153 | + log.debug("missedNotificationCollectionName : " + missedNotificationCollectionName); |
| 154 | + log.debug("missedNotificationDataBaseName : " + missedNotificationDataBaseName); |
| 155 | + log.debug("notification.failAttempt : " + failAttempt); |
| 156 | + log.debug("Missed Notification TTL value : " + ttlValue); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments