1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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;
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;
39 public class DmaapNotificationsComponent {
41 private static Logger log = LoggerFactory.getLogger(DmaapNotificationsComponent.class);
44 * Get sdnr notifications.
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);
53 ObjectMapper mapper = new ObjectMapper();
55 Notification notification = new Notification();
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);
66 * Get pm notifications.
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);
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);
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);