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.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import java.util.concurrent.BlockingQueue;
37 import java.util.concurrent.ExecutorService;
39 import org.onap.dcaegen2.services.sonhms.child.ChildThread;
40 import org.onap.dcaegen2.services.sonhms.child.Graph;
41 import org.onap.dcaegen2.services.sonhms.entity.ClusterDetails;
42 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
43 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
44 import org.onap.dcaegen2.services.sonhms.model.ClusterMap;
45 import org.onap.dcaegen2.services.sonhms.exceptions.CpsNotFoundException;
46 import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
47 import org.onap.dcaegen2.services.sonhms.model.LteNeighborListInUseLteCell;
48 import org.onap.dcaegen2.services.sonhms.model.Notification;
49 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
50 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class EventHandler {
56 private static Logger log = LoggerFactory.getLogger(EventHandler.class);
58 private static Map<Long, ChildThread> childThreadMap = new HashMap<>();
60 private BlockingQueue<List<String>> childStatusQueue;
62 private Map<Long, String> childStatus;
64 private ExecutorService pool;
66 private ClusterUtils clusterUtils;
68 private ThreadUtils threadUtils;
73 public EventHandler(BlockingQueue<List<String>> childStatusQueue, ExecutorService pool,
74 Map<Long, String> childStatus, ClusterUtils clusterUtils, ThreadUtils threadUtils) {
76 this.childStatusQueue = childStatusQueue;
77 this.childStatus = childStatus;
79 this.clusterUtils = clusterUtils;
80 this.threadUtils = threadUtils;
84 * Handles fault notifications.
86 public Boolean handleFaultNotification(List<FaultEvent> fmNotification) {
88 log.info("Handling Fault notification");
89 log.info("fm notification {}", fmNotification);
91 Set<String> cellIds = new HashSet<>();
92 List<ClusterDetails> clusterDetails = clusterUtils.getAllClusters();
93 String networkId = "";
94 Map<String, ArrayList<Integer>> collisionConfusionMap = new HashMap<>();
95 for (FaultEvent faultEvent : fmNotification) {
96 String cellId = faultEvent.getEvent().getCommonEventHeader().getSourceName();
98 networkId = faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getNetworkId();
100 ArrayList<Integer> counts = new ArrayList<>();
101 counts.add(faultEvent.getEvent().getFaultFields().getEventCategory().contains("PCICollision")?1:0);
102 counts.add(faultEvent.getEvent().getFaultFields().getEventCategory().contains("PCIConfusion")?1:0);
103 collisionConfusionMap.put(cellId, counts);
105 FaultNotificationtoClusterMapping faultNotificationtoClusterMapping = clusterUtils
106 .getClustersForFmNotification(cellIds, clusterDetails);
107 faultNotificationtoClusterMapping.setCollisionConfusionMap(collisionConfusionMap);
110 if (faultNotificationtoClusterMapping.getCellsinCluster() != null
111 && !faultNotificationtoClusterMapping.getCellsinCluster().isEmpty()) {
113 handleMatchedFmCells(faultNotificationtoClusterMapping, clusterDetails);
114 } catch (ConfigDbNotFoundException | CpsNotFoundException e) {
115 log.error("Config DB Exception {} or Cps Exception {} ", e);
118 // unmatched new cells
119 if (faultNotificationtoClusterMapping.getNewCells() != null
120 && !faultNotificationtoClusterMapping.getNewCells().isEmpty()) {
121 handleUnmatchedFmCells(faultNotificationtoClusterMapping, networkId);
128 * handle matched fm cells.
131 private void handleMatchedFmCells(FaultNotificationtoClusterMapping faultNotificationtoClusterMapping,
132 List<ClusterDetails> clusterDetails) throws ConfigDbNotFoundException, CpsNotFoundException {
133 Map<String, String> cellsinCluster = faultNotificationtoClusterMapping.getCellsinCluster();
134 log.info("Handling Matching cells for FM notification");
136 for (Entry<String, String> entry : cellsinCluster.entrySet()) {
138 String cellId = entry.getKey();
139 String clusterId = entry.getValue();
140 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(cellId);
142 Either<ClusterDetails, Integer> clusterDetail = clusterUtils.getClusterDetailsFromClusterId(clusterId,
145 if (clusterDetail.isRight()) {
146 log.error("Cannot find the cluster for Cluster ID");
149 long threadId = clusterDetail.left().value().getChildThreadId();
151 if (childStatus.get(threadId).equals("triggeredOof")) {
152 log.info("OOF triggered for the cluster, buffering notification");
153 bufferNotification(clusterMap, clusterId);
155 childThreadMap.get(threadId).putInQueue(clusterMap);
163 * handle unmatched fm cells.
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 | CpsNotFoundException 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 | CpsNotFoundException e) {
203 log.error("Config DB or CPS 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, CpsNotFoundException {
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 | CpsNotFoundException e) {
266 log.error("Error connecting with configDB {} or CPS {}", 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() {