[DCAEGEN2]Release dcaegen2-services-son-handler maven dependency
[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-2020 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
30 import org.json.JSONObject;
31 import org.onap.dcaegen2.services.sonhms.dao.DmaapNotificationsRepository;
32 import org.onap.dcaegen2.services.sonhms.dao.PerformanceNotificationsRepository;
33 import org.onap.dcaegen2.services.sonhms.model.Notification;
34 import org.onap.dcaegen2.services.sonhms.model.PmNotification;
35 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class DmaapNotificationsComponent {
40
41         private static Logger log = LoggerFactory.getLogger(DmaapNotificationsComponent.class);
42
43         /**
44          * Get sdnr notifications.
45          */
46         public Either<Notification, Integer> getSdnrNotifications() {
47                 DmaapNotificationsRepository dmaapNotificationsRepository = BeanUtil
48                                 .getBean(DmaapNotificationsRepository.class);
49                 String notificationString = dmaapNotificationsRepository.getNotificationFromQueue();
50                 if (notificationString == null) {
51                         return Either.right(404);
52                 }
53                 ObjectMapper mapper = new ObjectMapper();
54
55                 Notification notification = new Notification();
56                 try { 
57                         notification = mapper.readValue(notificationString, Notification.class);
58                         return Either.left(notification);
59                 } catch (IOException e) {
60                         log.error("Exception in parsing notification", notificationString, e);
61                         return Either.right(400);
62                 }
63         }
64
65         /**
66          * Get pm notifications.
67          */
68         public Either<PmNotification, Integer> getPmNotifications() {
69                 PerformanceNotificationsRepository pmNotificationRepository = BeanUtil
70                                 .getBean(PerformanceNotificationsRepository.class);
71                 String pmNotificationString = pmNotificationRepository.getPerformanceNotificationFromQueue();
72                 if (pmNotificationString == null) {
73                         return Either.right(404);
74                 }
75                 try {
76                         JSONObject obj = new JSONObject(pmNotificationString);
77                         Configuration configuration = Configuration.getInstance();
78                         String configNfNamingCode = configuration.getNfNamingCode();
79                         String nfNamingCode = obj.getJSONObject("event").getJSONObject("commonEventHeader")
80                                         .getString("nfNamingCode");
81                         if (!nfNamingCode.equalsIgnoreCase(configNfNamingCode)) {
82                                 return Either.right(404);
83                         }
84                         ObjectMapper mapper = new ObjectMapper();
85                         PmNotification pmNotification = new PmNotification();
86                         pmNotification = mapper.readValue(pmNotificationString, PmNotification.class);
87                         return Either.left(pmNotification);
88                 } catch (Exception e) {
89                         log.error("Exception in parsing pm notification ", pmNotificationString, e);
90                         return Either.right(400);
91                 }
92         }
93
94 }
95