Fix bug in filtering new FM notification
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / FaultNotificationComponent.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019-2021 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 java.util.ArrayList;
30 import java.util.List;
31
32 import org.json.JSONObject;
33 import org.onap.dcaegen2.services.sonhms.dao.FaultNotificationsRepository;
34 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class FaultNotificationComponent {
39
40         private static Logger log = LoggerFactory.getLogger(FaultNotificationComponent.class);
41
42         /**
43          * Get fault notifications.
44          */
45         public Either<List<FaultEvent>, Integer> getFaultNotifications() {
46                 FaultNotificationsRepository faultNotificationsRepository = BeanUtil
47                                 .getBean(FaultNotificationsRepository.class);
48                 String notificationString = faultNotificationsRepository.getFaultNotificationFromQueue();
49                 log.info("get fault notifications method");
50                 log.info("Notification String    " + notificationString);
51                 if (notificationString == null) {
52                         return Either.right(404);
53                 }
54                 try {
55                         JSONObject obj = new JSONObject(notificationString);
56                         Configuration configuration = Configuration.getInstance();
57                         String configNfNamingCode = configuration.getNfNamingCode();
58                         String nfNamingCode = obj.getJSONObject("event").getJSONObject("commonEventHeader")
59                                         .getString("nfNamingCode");
60                         if (!nfNamingCode.equalsIgnoreCase(configNfNamingCode)) {
61                                 return Either.right(404);
62                         }
63                         ObjectMapper mapper = new ObjectMapper();
64                         FaultEvent faultEvent = new FaultEvent();
65                         List<FaultEvent> faultEvents = new ArrayList<>();
66                         faultEvent = mapper.readValue(notificationString, FaultEvent.class);
67                         log.info("Parsing FM notification");
68                         faultEvents.add(faultEvent);
69                         return Either.left(faultEvents);
70                 } catch (Exception e) {
71                         log.error("Exception in parsing Notification {}", e);
72                         return Either.right(400);
73                 }
74         }
75
76 }
77