Implement DMaaP message handling from policy
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / DmaapNotificationsComponent.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *  
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *  
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *  
20  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import fj.data.Either;
27
28 import java.io.IOException;
29 import org.onap.dcaegen2.services.sonhms.dao.DmaapNotificationsRepository;
30 import org.onap.dcaegen2.services.sonhms.dao.PerformanceNotificationsRepository;
31 import org.onap.dcaegen2.services.sonhms.model.Notification;
32 import org.onap.dcaegen2.services.sonhms.model.PmNotification;
33 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class DmaapNotificationsComponent {
38     
39     private static Logger log = LoggerFactory.getLogger(DmaapNotificationsComponent.class);
40     
41     /**
42      * Get sdnr notifications.
43      */
44     public Either<Notification, Integer> getSdnrNotifications() {
45         DmaapNotificationsRepository dmaapNotificationsRepository = BeanUtil
46                 .getBean(DmaapNotificationsRepository.class);
47         String notificationString = dmaapNotificationsRepository.getNotificationFromQueue();
48         if (notificationString == null) {
49             return Either.right(404);
50         }
51         ObjectMapper mapper = new ObjectMapper();
52
53         Notification notification = new Notification();
54         try {
55             notification = mapper.readValue(notificationString, Notification.class);
56             return Either.left(notification);
57         } catch (IOException e) {
58             log.error("Exception in parsing notification", notificationString, e);
59             return Either.right(400);
60         }
61     }
62     
63     /**
64      * Get pm notifications.
65      */
66     public Either<PmNotification,Integer> getPmNotifications() {
67         PerformanceNotificationsRepository pmNotificationRepository =
68                 BeanUtil.getBean(PerformanceNotificationsRepository.class);
69         String pmNotificationString = pmNotificationRepository.getPerformanceNotificationFromQueue();
70         if (pmNotificationString == null) {
71             return Either.right(404);
72         }
73         ObjectMapper mapper = new ObjectMapper();        
74         PmNotification pmNotification = new PmNotification();
75         
76         try {
77             pmNotification = mapper.readValue(pmNotificationString, PmNotification.class);
78             return Either.left(pmNotification);
79         } catch (IOException e)  {
80             log.error("Exception in parsing pm notification ",pmNotificationString,e);
81             return Either.right(400);
82         }
83     }
84
85 }