Fix bug in filtering new FM notification
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / MainThread.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 fj.data.Either;
25
26 import java.sql.Timestamp;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.BlockingQueue;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.LinkedBlockingQueue;
34
35 import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
36 import org.onap.dcaegen2.services.sonhms.model.Notification;
37 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
38 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class MainThread implements Runnable {
43
44     private static Logger log = LoggerFactory.getLogger(MainThread.class);
45
46     private NewSdnrNotification newNotification;
47
48     private NewFmNotification newFmNotification;
49
50     private BlockingQueue<List<String>> childStatusQueue;
51
52     private DmaapNotificationsComponent dmaapNotificationsComponent;
53
54     private FaultNotificationComponent faultNotificationComponent;
55
56     private EventHandler eventHandler;
57
58     private Map<String, FaultEvent> bufferedFmNotificationCells;
59
60     private List<String> sdnrNotificationCells;
61
62     private Boolean isTimer;
63
64     private Timestamp startTimer;
65
66     List<FaultEvent> fmNotificationToBuffer;
67
68     /**
69      * parameterized constructor.
70      */
71     public MainThread(NewSdnrNotification newNotification, NewFmNotification newFmNotification) {
72         super();
73         this.newFmNotification = newFmNotification;
74         this.newNotification = newNotification;
75         childStatusQueue = new LinkedBlockingQueue<>();
76         dmaapNotificationsComponent = new DmaapNotificationsComponent();
77         faultNotificationComponent = new FaultNotificationComponent();
78         sdnrNotificationCells = new ArrayList<>();
79         fmNotificationToBuffer = new ArrayList<>();
80         bufferedFmNotificationCells = new HashMap<>();
81         eventHandler = new EventHandler(childStatusQueue,
82                 Executors.newFixedThreadPool(Configuration.getInstance().getMaximumClusters()), new HashMap<>(),
83                 new ClusterUtils(), new ThreadUtils());
84         isTimer = false;
85         startTimer = new Timestamp(System.currentTimeMillis());
86
87     }
88
89     @Override
90     public void run() {
91         log.info("Starting Main Thread");
92
93         // Check for Notifications from Dmaap and Child thread
94         Boolean done = false;
95
96         while (!done) {
97
98             Timestamp currentTime = new Timestamp(System.currentTimeMillis());
99             if (isTimer) {
100                 Long difference = currentTime.getTime() - startTimer.getTime();
101                 if (difference > 5000) {
102                     log.info("FM handling difference > 5000");
103
104                     for (String sdnrCell : sdnrNotificationCells) {
105                         bufferedFmNotificationCells.remove(sdnrCell);
106                     }
107
108                     log.info("FM bufferedFMNotificationCells {}", bufferedFmNotificationCells.values());
109                     List<FaultEvent> fmNotificationsToHandle = new ArrayList<>(bufferedFmNotificationCells.values());
110                     Boolean result = eventHandler.handleFaultNotification(fmNotificationsToHandle);
111                     bufferedFmNotificationCells = new HashMap<>();
112                     isTimer = false;
113                     log.info("FM notification handling {}", result);
114                 }
115             }
116
117             try {
118                 if (!childStatusQueue.isEmpty()) {
119                     List<String> childState = childStatusQueue.poll();
120                     if (childState != null) {
121                         eventHandler.handleChildStatusUpdate(childState);
122                     }
123                 }
124
125                 if (newNotification.getNewNotif()) {
126                     Either<Notification, Integer> notification = dmaapNotificationsComponent.getSdnrNotifications();
127                     if (notification.isRight()) {
128                         if (notification.right().value() == 400) {
129                             log.error("Error parsing the notification from SDNR");
130                         } else if (notification.right().value() == 404) {
131                             newNotification.setNewNotif(false);
132                         }
133                     } else if (notification.isLeft()) {
134                         List<FapServiceList> fapServiceLists = (notification.left().value()).getPayload()
135                                 .getRadioAccess().getFapServiceList();
136                         for (FapServiceList fapServiceList : fapServiceLists) {
137                             sdnrNotificationCells.add(fapServiceList.getAlias());
138
139                         }
140
141                         Boolean result = eventHandler.handleSdnrNotification(notification.left().value());
142                         log.debug("SDNR notification handling {}", result);
143
144                     }
145
146                 }
147                 if (newFmNotification.getNewNotif()) {
148                                         log.info("newFmNotification has come");
149
150                                         String faultCellId = "";
151                                         Either<List<FaultEvent>, Integer> fmNotifications = faultNotificationComponent
152                                                         .getFaultNotifications();
153                                         if (fmNotifications.isRight()) {
154                                                 if (fmNotifications.right().value() == 400) {
155                                                         log.info("Error parsing notifications");
156                                                 } else if (fmNotifications.right().value() == 404) {
157                                                         newFmNotification.setNewNotif(false);
158                                                 }
159                                         } else {
160                                                 for (FaultEvent fmNotification : fmNotifications.left().value()) {
161                                                         if (fmNotification.getEvent().getFaultFields().getSpecificProblem()
162                                                                         .equals("Optimised PCI")) {
163                                                                 log.info("PCI problem cleared for :" + fmNotification);
164                                                         } else if ((fmNotification.getEvent().getFaultFields().getAlarmCondition()
165                                                                                 .equalsIgnoreCase("RanPciCollisionConfusionOccurred"))) 
166                                                         {
167                                                                 faultCellId = fmNotification.getEvent().getCommonEventHeader().getSourceName();
168                                                                 bufferedFmNotificationCells.put(faultCellId, fmNotification);
169                                                                 log.info("Buffered FM cell {}", faultCellId);
170                                                                 log.info("fmNotification{}", fmNotification);
171                                                         } else {
172                                                                 log.info("Error resolving faultNotification for :" + fmNotification);
173                                                         }
174                                                 }
175
176                                                 if (!(bufferedFmNotificationCells.isEmpty())) {
177                                                         log.info("bufferedFMNotificationCells before staring timer {}",
178                                                                         bufferedFmNotificationCells.keySet());
179
180                                                         for (String sdnrCell : sdnrNotificationCells) {
181                                                                 bufferedFmNotificationCells.remove(sdnrCell);
182                                                         }
183
184                                                         startTimer = new Timestamp(System.currentTimeMillis());
185                                                         isTimer = true;
186                                                         log.info("Buffered FM cell {}", bufferedFmNotificationCells.keySet());
187                                                 }
188                                         }
189
190                                 }
191
192             } catch (Exception e) {
193                 log.error("Exception in main Thread", e);
194                 done = true;
195             }
196
197         }
198
199     }
200 }