1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.dcaegen2.services.sonhms;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import fj.data.Either;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
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;
38 public class FaultNotificationComponent {
40 private static Logger log = LoggerFactory.getLogger(FaultNotificationComponent.class);
43 * Get fault notifications.
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);
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);
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);