1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.model.FapServiceList;
46 import org.onap.dcaegen2.services.sonhms.model.LteNeighborListInUseLteCell;
47 import org.onap.dcaegen2.services.sonhms.model.Notification;
48 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
49 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class EventHandler {
55 private static Logger log = LoggerFactory.getLogger(EventHandler.class);
57 private static Map<Long, ChildThread> childThreadMap = new HashMap<>();
59 private BlockingQueue<List<String>> childStatusQueue;
61 private Map<Long, String> childStatus;
63 private ExecutorService pool;
65 private ClusterUtils clusterUtils;
67 private ThreadUtils threadUtils;
72 public EventHandler(BlockingQueue<List<String>> childStatusQueue, ExecutorService pool,
73 Map<Long, String> childStatus, ClusterUtils clusterUtils, ThreadUtils threadUtils) {
75 this.childStatusQueue = childStatusQueue;
76 this.childStatus = childStatus;
78 this.clusterUtils = clusterUtils;
79 this.threadUtils = threadUtils;
83 * Handles fault notifications.
85 public Boolean handleFaultNotification(List<FaultEvent> fmNotification) {
87 log.info("Handling Fault notification");
88 log.info("fm notification {}", fmNotification);
90 Set<String> cellIds = new HashSet<>();
91 List<ClusterDetails> clusterDetails = clusterUtils.getAllClusters();
92 String networkId = "";
93 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();
99 ArrayList<Integer> counts = new ArrayList<>();
100 counts.add(faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getCollisions());
101 counts.add(faultEvent.getEvent().getFaultFields().getAlarmAdditionalInformation().getConfusions());
102 collisionConfusionMap.put(cellId, counts);
104 FaultNotificationtoClusterMapping faultNotificationtoClusterMapping = clusterUtils
105 .getClustersForFmNotification(cellIds, clusterDetails);
107 faultNotificationtoClusterMapping.setCollisionConfusionMap(collisionConfusionMap);
109 if (faultNotificationtoClusterMapping.getCellsinCluster() != null && !faultNotificationtoClusterMapping.getCellsinCluster().isEmpty()) {
111 handleMatchedFmCells(faultNotificationtoClusterMapping, clusterDetails);
112 } catch (ConfigDbNotFoundException e) {
113 log.error("Config DB Exception {}", e);
117 // unmatched new cells
118 if (faultNotificationtoClusterMapping.getNewCells() != null && !faultNotificationtoClusterMapping.getNewCells().isEmpty()) {
119 handleUnmatchedFmCells(faultNotificationtoClusterMapping, networkId);
127 * handle matched fm cells.
130 private void handleMatchedFmCells(FaultNotificationtoClusterMapping faultNotificationtoClusterMapping,
131 List<ClusterDetails> clusterDetails) throws ConfigDbNotFoundException {
132 Map<String, String> cellsinCluster = faultNotificationtoClusterMapping.getCellsinCluster();
133 log.info("Handling Matching cells for FM notification");
135 for (Entry<String, String> entry : cellsinCluster.entrySet()) {
137 String cellId = entry.getKey();
138 String clusterId = entry.getValue();
139 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(cellId);
141 Either<ClusterDetails, Integer> clusterDetail = clusterUtils.getClusterDetailsFromClusterId(clusterId,
144 if (clusterDetail.isRight()) {
145 log.error("Cannot find the cluster for Cluster ID");
148 long threadId = clusterDetail.left().value().getChildThreadId();
150 if (childStatus.get(threadId).equals("triggeredOof")) {
151 log.info("OOF triggered for the cluster, buffering notification");
152 bufferNotification(clusterMap, clusterId);
154 childThreadMap.get(threadId).putInQueue(clusterMap);
162 * handle unmatched fm cells.
165 * @param faultNotificationtoClusterMapping
167 private void handleUnmatchedFmCells(FaultNotificationtoClusterMapping faultNotificationtoClusterMapping,
169 List<String> newCells = faultNotificationtoClusterMapping.getNewCells();
170 log.info("Handle Unmatching cells for FM notificatins newCells{}", newCells);
171 List<Graph> newClusters = new ArrayList<>();
173 for (String cellId : newCells) {
174 ArrayList<Integer> collisionConfusionCount = faultNotificationtoClusterMapping.getCollisionConfusionMap()
176 log.info("Handle Unmatching cells for FM notificatins,collisionConfusionCount{}", collisionConfusionCount);
178 Either<Graph, Integer> existingCluster = clusterUtils.getClusterForFMCell(cellId, newClusters);
179 if (existingCluster.isRight()) {
181 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(cellId);
182 Graph cluster = clusterUtils.createCluster(clusterMap);
183 cluster.setNetworkId(networkId);
184 Map<String, ArrayList<Integer>> collisionConfusionMap = new HashMap<>();
185 collisionConfusionMap.put(cellId, collisionConfusionCount);
186 cluster.setCollisionConfusionMap(collisionConfusionMap);
188 newClusters.add(cluster);
189 } catch (ConfigDbNotFoundException e) {
190 log.error("Error connecting with configDB {}", e);
195 Graph cluster = existingCluster.left().value();
197 Graph modifiedCluster = null;
199 modifiedCluster = clusterUtils.modifyCluster(cluster, clusterUtils.findClusterMap(cellId));
200 Map<String, ArrayList<Integer>> collisionConfusionMap = cluster.getCollisionConfusionMap();
201 collisionConfusionMap.put(cellId, collisionConfusionCount);
202 cluster.setCollisionConfusionMap(collisionConfusionMap);
203 } catch (ConfigDbNotFoundException e) {
204 log.error("Config DB not found {}", e);
206 newClusters.remove(cluster);
207 newClusters.add(modifiedCluster);
212 // create new child thread
213 log.info("New clusters {}", newClusters);
215 threadUtils.createNewThread(newClusters, childStatusQueue, pool, this, null);
220 * handle sdnr notification.
222 public Boolean handleSdnrNotification(Notification notification) {
223 // Check if notification matches with a cluster
224 log.info("Handling SDNR notification");
226 List<ClusterDetails> clusterDetails = clusterUtils.getAllClusters();
228 NotificationToClusterMapping mapping = clusterUtils.getClustersForNotification(notification,
232 if (mapping.getCellsinCluster() != null) {
233 handleMatchingCells(mapping.getCellsinCluster(), clusterDetails);
237 if (mapping.getNewCells() != null) {
238 handleUnMatchingCells(mapping.getNewCells());
240 } catch (Exception e) {
241 log.error("Exception in sdnr notification handling {}", e);
249 private void handleUnMatchingCells(List<FapServiceList> newCells) throws ConfigDbNotFoundException {
251 log.info("handling unmatched cells");
253 List<Graph> newClusters = new ArrayList<>();
255 for (FapServiceList fapService : newCells) {
257 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = clusterUtils.findClusterMap(fapService.getAlias());
258 Either<Graph, Integer> existingCluster = clusterUtils.getClusterForCell(fapService, newClusters);
259 if (existingCluster.isRight()) {
261 Graph cluster = clusterUtils.createCluster(clusterMap);
262 cluster.setNetworkId(fapService.getCellConfig().getLte().getRan().getNeighborListInUse()
263 .getLteNeighborListInUseLteCell().get(0).getPlmnid());
264 cluster.setCollisionConfusionMap(new HashMap<>());
265 newClusters.add(cluster);
266 } catch (ConfigDbNotFoundException e) {
267 log.error("Error connecting with configDB {}", e);
272 Graph cluster = existingCluster.left().value();
273 Graph modifiedCluster = clusterUtils.modifyCluster(cluster,
274 clusterUtils.findClusterMap(fapService.getAlias()));
275 newClusters.remove(cluster);
276 newClusters.add(modifiedCluster);
281 // create new child thread
283 threadUtils.createNewThread(newClusters, childStatusQueue, pool, this, null);
287 private void handleMatchingCells(Map<FapServiceList, String> cellsInCluster, List<ClusterDetails> clusterDetails)
288 throws ConfigDbNotFoundException {
290 log.info("handling matching cells");
292 for (Entry<FapServiceList, String> entry : cellsInCluster.entrySet()) {
294 FapServiceList fapService = entry.getKey();
295 String clusterId = entry.getValue();
296 String cellId = fapService.getAlias();
297 int pci = fapService.getX0005b9Lte().getPhyCellIdInUse();
298 ArrayList<CellPciPair> neighbours = new ArrayList<>();
299 for (LteNeighborListInUseLteCell neighbour : fapService.getCellConfig().getLte().getRan()
300 .getNeighborListInUse().getLteNeighborListInUseLteCell()) {
301 String neighbourCellId = neighbour.getAlias();
302 int neighbourPci = neighbour.getPhyCellId();
303 neighbours.add(new CellPciPair(neighbourCellId, neighbourPci));
306 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<>();
307 clusterMap.put(new CellPciPair(cellId, pci), neighbours);
309 Either<ClusterDetails, Integer> clusterDetail = clusterUtils.getClusterDetailsFromClusterId(clusterId,
312 if (clusterDetail.isRight()) {
313 log.error("Cannot find the cluster for Cluster ID");
316 long threadId = clusterDetail.left().value().getChildThreadId();
318 if (childStatus.get(threadId).equals("triggeredOof")) {
319 log.info("OOF triggered for the cluster, buffering notification");
321 bufferNotification(clusterMap, clusterId);
323 log.info("Forwarding notification to child thread {}", threadId);
324 childThreadMap.get(threadId).putInQueue(clusterMap);
330 private void bufferNotification(Map<CellPciPair, ArrayList<CellPciPair>> clusterMap, String clusterId) {
332 log.info("Buffering notifications ...");
333 ObjectMapper mapper = new ObjectMapper();
334 String serviceListString = "";
336 ClusterMap clusterMapJson = new ClusterMap();
338 clusterMapJson.setCell(clusterMap.keySet().iterator().next());
339 clusterMapJson.setNeighbourList(clusterMap.get(clusterMap.keySet().iterator().next()));
342 serviceListString = mapper.writeValueAsString(clusterMapJson);
343 } catch (JsonProcessingException e) {
344 log.error("JSON processing exception: {}", e);
346 BufferNotificationComponent bufferNotifComponent = new BufferNotificationComponent();
347 bufferNotifComponent.bufferNotification(serviceListString, clusterId);
352 * handle child status update.
354 public void handleChildStatusUpdate(List<String> childStatus) {
356 log.info("Handling child status update");
358 Long childThreadId = Long.parseLong(childStatus.get(0));
359 addChildStatus(childThreadId, childStatus.get(1));
361 // if child status is OOF result success, handle buffered notifications
362 if (childStatus.get(1).equals("done")) {
363 deleteChildStatus(childThreadId);
365 // else kill the child thread
369 public static void addChildThreadMap(Long childThreadId, ChildThread child) {
370 childThreadMap.put(childThreadId, child);
373 public static Map<Long, ChildThread> getChildThreadMap() {
374 return childThreadMap;
377 public void addChildStatus(Long threadId, String status) {
378 this.childStatus.put(threadId, status);
381 public String getChildStatus(Long threadId) {
382 return childStatus.get(threadId);
386 public void deleteChildStatus(Long childThreadId) {
387 this.childStatus.remove(childThreadId);
391 public ExecutorService getPool() {