1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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=========================================================
19 ******************************************************************************/
21 package org.onap.dcaegen2.services.sonhms;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import java.util.concurrent.BlockingQueue;
36 import java.util.concurrent.ExecutorService;
38 import org.onap.dcaegen2.services.sonhms.child.ChildThread;
39 import org.onap.dcaegen2.services.sonhms.child.Graph;
40 import org.onap.dcaegen2.services.sonhms.entity.ClusterDetails;
41 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
42 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
43 import org.onap.dcaegen2.services.sonhms.model.ClusterMap;
44 import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
45 import org.onap.dcaegen2.services.sonhms.model.LteNeighborListInUseLteCell;
46 import org.onap.dcaegen2.services.sonhms.model.Notification;
47 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
48 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class EventHandler {
54 private static Logger log = LoggerFactory.getLogger(EventHandler.class);
56 private static Map<Long, ChildThread> childThreadMap = new HashMap<>();
58 private BlockingQueue<List<String>> childStatusQueue;
60 private Map<Long, String> childStatus;
62 private ExecutorService pool;
64 private ClusterUtils clusterUtils;
66 private ThreadUtils threadUtils;
71 public EventHandler(BlockingQueue<List<String>> childStatusQueue, ExecutorService pool,
72 Map<Long, String> childStatus, ClusterUtils clusterUtils, ThreadUtils threadUtils) {
74 this.childStatusQueue = childStatusQueue;
75 this.childStatus = childStatus;
77 this.clusterUtils = clusterUtils;
78 this.threadUtils = threadUtils;
82 * Handles fault notifications.
84 public Boolean handleFaultNotification(List<FaultEvent> fmNotification) {
86 log.info("Handling Fault notification");
87 log.info("fm notification {}", fmNotification);
89 Set<String> cellIds = new HashSet<>();
90 List<ClusterDetails> clusterDetails = clusterUtils.getAllClusters();
91 String networkId = "";
92 Map<String, ArrayList<Integer>> collisionConfusionMap = new HashMap<>();
94 for (FaultEvent faultEvent : fmNotification) {
95 String cellId = faultEvent.getEvent().getCommonEventHeader().getSourceName();
97 networkId = faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getNetworkId();
98 ArrayList<Integer> counts = new ArrayList<>();
99 counts.add(faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getCollisions());
100 counts.add(faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getConfusions());
101 collisionConfusionMap.put(cellId, counts);
103 FaultNotificationtoClusterMapping faultNotificationtoClusterMapping = clusterUtils
104 .getClustersForFmNotification(cellIds, clusterDetails);
106 faultNotificationtoClusterMapping.setCollisionConfusionMap(collisionConfusionMap);
108 if (faultNotificationtoClusterMapping.getCellsinCluster() != null && !faultNotificationtoClusterMapping.getCellsinCluster().isEmpty()) {
110 handleMatchedFmCells(faultNotificationtoClusterMapping, clusterDetails);
111 } catch (ConfigDbNotFoundException e) {
112 log.error("Config DB Exception {}", e);
116 // unmatched new cells
117 if (faultNotificationtoClusterMapping.getNewCells() != null && !faultNotificationtoClusterMapping.getNewCells().isEmpty()) {
118 handleUnmatchedFmCells(faultNotificationtoClusterMapping, networkId);
126 * handle matched fm cells.
129 private void handleMatchedFmCells(FaultNotificationtoClusterMapping faultNotificationtoClusterMapping,
130 List<ClusterDetails> clusterDetails) throws ConfigDbNotFoundException {
131 Map<String, String> cellsinCluster = faultNotificationtoClusterMapping.getCellsinCluster();
132 log.info("Handling Matching cells for FM notification");
134 for (Entry<String, String> entry : cellsinCluster.entrySet()) {
136 String cellId = entry.getKey();
137 String clusterId = entry.getValue();
138 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(cellId);
140 Either<ClusterDetails, Integer> clusterDetail = clusterUtils.getClusterDetailsFromClusterId(clusterId,
143 if (clusterDetail.isRight()) {
144 log.error("Cannot find the cluster for Cluster ID");
147 long threadId = clusterDetail.left().value().getChildThreadId();
149 if (childStatus.get(threadId).equals("triggeredOof")) {
150 log.info("OOF triggered for the cluster, buffering notification");
151 bufferNotification(clusterMap, clusterId);
153 childThreadMap.get(threadId).putInQueue(clusterMap);
161 * handle unmatched fm cells.
164 * @param faultNotificationtoClusterMapping
166 private void handleUnmatchedFmCells(FaultNotificationtoClusterMapping faultNotificationtoClusterMapping,
168 List<String> newCells = faultNotificationtoClusterMapping.getNewCells();
169 log.info("Handle Unmatching cells for FM notificatins newCells{}", newCells);
170 List<Graph> newClusters = new ArrayList<>();
172 for (String cellId : newCells) {
173 ArrayList<Integer> collisionConfusionCount = faultNotificationtoClusterMapping.getCollisionConfusionMap()
175 log.info("Handle Unmatching cells for FM notificatins,collisionConfusionCount{}", collisionConfusionCount);
177 Either<Graph, Integer> existingCluster = clusterUtils.getClusterForFMCell(cellId, newClusters);
178 if (existingCluster.isRight()) {
180 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(cellId);
181 Graph cluster = clusterUtils.createCluster(clusterMap);
182 cluster.setNetworkId(networkId);
183 Map<String, ArrayList<Integer>> collisionConfusionMap = new HashMap<>();
184 collisionConfusionMap.put(cellId, collisionConfusionCount);
185 cluster.setCollisionConfusionMap(collisionConfusionMap);
187 newClusters.add(cluster);
188 } catch (ConfigDbNotFoundException e) {
189 log.error("Error connecting with configDB {}", e);
194 Graph cluster = existingCluster.left().value();
196 Graph modifiedCluster = null;
198 modifiedCluster = clusterUtils.modifyCluster(cluster, clusterUtils.findClusterMap(cellId));
199 Map<String, ArrayList<Integer>> collisionConfusionMap = cluster.getCollisionConfusionMap();
200 collisionConfusionMap.put(cellId, collisionConfusionCount);
201 cluster.setCollisionConfusionMap(collisionConfusionMap);
202 } catch (ConfigDbNotFoundException e) {
203 log.error("Config DB not found {}", e);
205 newClusters.remove(cluster);
206 newClusters.add(modifiedCluster);
211 // create new child thread
212 log.info("New clusters {}", newClusters);
214 threadUtils.createNewThread(newClusters, childStatusQueue, pool, this, null);
219 * handle sdnr notification.
221 public Boolean handleSdnrNotification(Notification notification) {
222 // Check if notification matches with a cluster
223 log.info("Handling SDNR notification");
225 List<ClusterDetails> clusterDetails = clusterUtils.getAllClusters();
227 NotificationToClusterMapping mapping = clusterUtils.getClustersForNotification(notification,
231 if (mapping.getCellsinCluster() != null) {
232 handleMatchingCells(mapping.getCellsinCluster(), clusterDetails);
236 if (mapping.getNewCells() != null) {
237 handleUnMatchingCells(mapping.getNewCells());
239 } catch (Exception e) {
240 log.error("Exception in sdnr notification handling {}", e);
248 private void handleUnMatchingCells(List<FapServiceList> newCells) throws ConfigDbNotFoundException {
250 log.info("handling unmatched cells");
252 List<Graph> newClusters = new ArrayList<>();
254 for (FapServiceList fapService : newCells) {
256 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(fapService.getAlias());
257 Either<Graph, Integer> existingCluster = clusterUtils.getClusterForCell(fapService, newClusters);
258 if (existingCluster.isRight()) {
260 Graph cluster = clusterUtils.createCluster(clusterMap);
261 cluster.setNetworkId(fapService.getCellConfig().getLte().getRan().getNeighborListInUse()
262 .getLteNeighborListInUseLteCell().get(0).getPlmnid());
263 cluster.setCollisionConfusionMap(new HashMap<>());
264 newClusters.add(cluster);
265 } catch (ConfigDbNotFoundException e) {
266 log.error("Error connecting with configDB {}", e);
271 Graph cluster = existingCluster.left().value();
272 Graph modifiedCluster = clusterUtils.modifyCluster(cluster,
273 clusterUtils.findClusterMap(fapService.getAlias()));
274 newClusters.remove(cluster);
275 newClusters.add(modifiedCluster);
280 // create new child thread
282 threadUtils.createNewThread(newClusters, childStatusQueue, pool, this, null);
286 private void handleMatchingCells(Map<FapServiceList, String> cellsInCluster, List<ClusterDetails> clusterDetails)
287 throws ConfigDbNotFoundException {
289 log.info("handling matching cells");
291 for (Entry<FapServiceList, String> entry : cellsInCluster.entrySet()) {
293 FapServiceList fapService = entry.getKey();
294 String clusterId = entry.getValue();
295 String cellId = fapService.getAlias();
296 int pci = fapService.getX0005b9Lte().getPhyCellIdInUse();
297 ArrayList<CellPciPair> neighbours = new ArrayList<>();
298 for (LteNeighborListInUseLteCell neighbour : fapService.getCellConfig().getLte().getRan()
299 .getNeighborListInUse().getLteNeighborListInUseLteCell()) {
300 String neighbourCellId = neighbour.getAlias();
301 int neighbourPci = neighbour.getPhyCellId();
302 neighbours.add(new CellPciPair(neighbourCellId, neighbourPci));
305 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<>();
306 clusterMap.put(new CellPciPair(cellId, pci), neighbours);
308 Either<ClusterDetails, Integer> clusterDetail = clusterUtils.getClusterDetailsFromClusterId(clusterId,
311 if (clusterDetail.isRight()) {
312 log.error("Cannot find the cluster for Cluster ID");
315 long threadId = clusterDetail.left().value().getChildThreadId();
317 if (childStatus.get(threadId).equals("triggeredOof")) {
318 log.info("OOF triggered for the cluster, buffering notification");
320 bufferNotification(clusterMap, clusterId);
322 log.info("Forwarding notification to child thread {}", threadId);
323 childThreadMap.get(threadId).putInQueue(clusterMap);
329 private void bufferNotification(Map<CellPciPair, ArrayList<CellPciPair>> clusterMap, String clusterId) {
331 log.info("Buffering notifications ...");
332 ObjectMapper mapper = new ObjectMapper();
333 String serviceListString = "";
335 ClusterMap clusterMapJson = new ClusterMap();
337 clusterMapJson.setCell(clusterMap.keySet().iterator().next());
338 clusterMapJson.setNeighbourList(clusterMap.get(clusterMap.keySet().iterator().next()));
341 serviceListString = mapper.writeValueAsString(clusterMapJson);
342 } catch (JsonProcessingException e) {
343 log.error("JSON processing exception: {}", e);
345 BufferNotificationComponent bufferNotifComponent = new BufferNotificationComponent();
346 bufferNotifComponent.bufferNotification(serviceListString, clusterId);
351 * handle child status update.
353 public void handleChildStatusUpdate(List<String> childStatus) {
355 log.info("Handling child status update");
357 Long childThreadId = Long.parseLong(childStatus.get(0));
358 addChildStatus(childThreadId, childStatus.get(1));
360 // if child status is OOF result success, handle buffered notifications
361 if (childStatus.get(1).equals("done")) {
362 deleteChildStatus(childThreadId);
364 // else kill the child thread
368 public static void addChildThreadMap(Long childThreadId, ChildThread child) {
369 childThreadMap.put(childThreadId, child);
372 public static Map<Long, ChildThread> getChildThreadMap() {
373 return childThreadMap;
376 public void addChildStatus(Long threadId, String status) {
377 this.childStatus.put(threadId, status);
380 public String getChildStatus(Long threadId) {
381 return childStatus.get(threadId);
385 public void deleteChildStatus(Long childThreadId) {
386 this.childStatus.remove(childThreadId);
390 public ExecutorService getPool() {