/******************************************************************************* * ============LICENSE_START======================================================= * pcims * ================================================================================ * Copyright (C) 2018 Wipro Limited. * ============================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= ******************************************************************************/ package com.wipro.www.sonhms; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.wipro.www.sonhms.child.ChildThread; import com.wipro.www.sonhms.child.Graph; import com.wipro.www.sonhms.dao.CellInfoRepository; import com.wipro.www.sonhms.dao.ClusterDetailsRepository; import com.wipro.www.sonhms.entity.CellInfo; import com.wipro.www.sonhms.entity.ClusterDetails; import com.wipro.www.sonhms.exceptions.ConfigDbNotFoundException; import com.wipro.www.sonhms.model.CellPciPair; import com.wipro.www.sonhms.model.FapServiceList; import com.wipro.www.sonhms.model.LteNeighborListInUseLteCell; import com.wipro.www.sonhms.model.Notification; import com.wipro.www.sonhms.model.ThreadId; import com.wipro.www.sonhms.restclient.SdnrRestClient; import com.wipro.www.sonhms.utils.BeanUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import org.slf4j.Logger; public class SdnrNotificationHandlingState implements SonState { private static Map childThreadMap = new HashMap<>(); private static final Logger log = org.slf4j.LoggerFactory.getLogger(SdnrNotificationHandlingState.class); @Override public void stateChange(SonContext sonContext) { // logic to determine if notif to be processed log.debug("inside statechange of sdnr notif state"); String notification = sonContext.getSdnrNotification(); Notification notificationObject; try { ObjectMapper mapper = new ObjectMapper(); notificationObject = mapper.readValue(notification, Notification.class); log.debug("notificationObject{}", notificationObject); List serviceList = notificationObject.getPayload().getRadioAccess().getFapServiceList(); for (FapServiceList fapService : serviceList) { String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity(); log.debug("cellId:{}", cellId); log.debug("inside for loop"); List clusterDetails = getAllClusters(); ClusterDetails clusterDetail = getClusterForNotification(fapService, clusterDetails); if (clusterDetail == null) { // form the cluster Graph cluster = createCluster(fapService); // save to db UUID clusterId = UUID.randomUUID(); cluster.setGraphId(clusterId); // create the child thread log.debug("creating new child"); BlockingQueue queue = new LinkedBlockingQueue<>(); ThreadId threadId = new ThreadId(); threadId.setChildThreadId(0); ChildThread child = new ChildThread(sonContext.getChildStatusUpdate(), cluster, queue, threadId); queue.put(fapService); MainThreadComponent mainThreadComponent = BeanUtil.getBean(MainThreadComponent.class); mainThreadComponent.getPool().execute(child); waitForThreadId(threadId); saveCluster(cluster, clusterId, threadId.getChildThreadId()); addChildThreadMap(threadId.getChildThreadId(), child); sonContext.addChildStatus(threadId.getChildThreadId(), "processingNotifications"); } else { if (isOofTriggeredForCluster(sonContext, clusterDetail)) { sonContext.setNotifToBeProcessed(false); bufferNotification(fapService, clusterDetail.getClusterId()); } else { sonContext.setNotifToBeProcessed(true); log.debug("childThreadId:{}", clusterDetail.getChildThreadId()); childThreadMap.get(clusterDetail.getChildThreadId()).putInQueue(fapService); } } } } catch (Exception e) { log.error("caught in sdnr notif handling state{}", e); } WaitState waitState = WaitState.getInstance(); sonContext.setPciState(waitState); sonContext.stateChange(sonContext); } private void waitForThreadId(ThreadId threadId) { try { synchronized (threadId) { while (threadId.getChildThreadId() == 0) { threadId.wait(); } } } catch (InterruptedException e) { log.error("ChildThread queue error {}", e); Thread.currentThread().interrupt(); } } private String saveCluster(Graph cluster, UUID clusterId, Long threadId) { String cellPciNeighbourString = cluster.getPciNeighbourJson(); log.debug("cluster hahsmap to string : {}", cellPciNeighbourString); cluster.setGraphId(clusterId); ClusterDetails details = new ClusterDetails(); details.setClusterId(clusterId.toString()); details.setClusterInfo(cellPciNeighbourString); details.setChildThreadId(threadId); ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class); clusterDetailsRepository.save(details); return clusterId.toString(); } private Graph createCluster(FapServiceList fapService) throws ConfigDbNotFoundException { Graph cluster = new Graph(); log.debug("cluster formation started"); int phycellId = fapService.getX0005b9Lte().getPhyCellIdInUse(); String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity(); CellInfoRepository cellInfoRepository = BeanUtil.getBean(CellInfoRepository.class); cellInfoRepository.save(new CellInfo(cellId, fapService.getX0005b9Lte().getPnfName())); CellPciPair val = new CellPciPair(); val.setCellId(cellId); val.setPhysicalCellId(phycellId); List neighbourlist; neighbourlist = fapService.getCellConfig().getLte().getRan().getNeighborListInUse() .getLteNeighborListInUseLteCell(); log.debug("Neighbor list size: {}", neighbourlist.size()); for (int i = 0; i < neighbourlist.size(); i++) { String cell = neighbourlist.get(i).getAlias(); int phy = neighbourlist.get(i).getPhyCellId(); cellInfoRepository.save(new CellInfo(cell, neighbourlist.get(i).getPnfName())); log.debug("cellID: {}", cell); log.debug("PCI: {}", phy); CellPciPair val1 = new CellPciPair(); val1.setCellId(cell); val1.setPhysicalCellId(phy); cluster.addEdge(val, val1); log.debug("cluster: {}", cluster); List nbrList = SdnrRestClient.getNbrList(neighbourlist.get(i).getAlias()); for (CellPciPair nbr : nbrList) { String cid = nbr.getCellId(); int pci = nbr.getPhysicalCellId(); CellPciPair val3 = new CellPciPair(); val3.setCellId(cid); val3.setPhysicalCellId(pci); cluster.addEdge(val1, val3); } } log.debug("final cluster: {}", cluster); return cluster; } private void bufferNotification(FapServiceList fapService, String clusterId) { ObjectMapper mapper = new ObjectMapper(); BufferNotificationComponent bufferNotifComponent = new BufferNotificationComponent(); String serviceListString = ""; try { serviceListString = mapper.writeValueAsString(fapService); } catch (JsonProcessingException e) { log.debug("JSON processing exception: {}", e); } bufferNotifComponent.bufferNotification(serviceListString, clusterId); } private boolean isOofTriggeredForCluster(SonContext pciContext, ClusterDetails clusterDetail) { Long childThreadId = clusterDetail.getChildThreadId(); String childStatus = pciContext.getChildStatus(childThreadId); return childStatus.equals("triggeredOof"); } private ClusterDetails getClusterForNotification(FapServiceList fapService, List clusterDetails) { String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity(); Map> cellPciNeighbourMap; for (ClusterDetails clusterDetail : clusterDetails) { Graph cluster = new Graph(clusterDetail.getClusterInfo()); cellPciNeighbourMap = cluster.getCellPciNeighbourMap(); Set keys = cellPciNeighbourMap.keySet(); Iterator traverse = keys.iterator(); while (traverse.hasNext()) { CellPciPair key = traverse.next(); String currentCellId = key.getCellId(); if (cellId.equals(currentCellId)) { return clusterDetail; } } } return null; } private List getAllClusters() { ClusterDetailsComponent clusterDetailsComponent = new ClusterDetailsComponent(); return clusterDetailsComponent.getClusterDetails(); } public static void addChildThreadMap(Long childThreadId, ChildThread child) { childThreadMap.put(childThreadId, child); } public static Map getChildThreadMap() { return childThreadMap; } }