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 com.wipro.www.sonhms;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.wipro.www.sonhms.child.ChildThread;
26 import com.wipro.www.sonhms.child.Graph;
27 import com.wipro.www.sonhms.dao.CellInfoRepository;
28 import com.wipro.www.sonhms.dao.ClusterDetailsRepository;
29 import com.wipro.www.sonhms.entity.CellInfo;
30 import com.wipro.www.sonhms.entity.ClusterDetails;
31 import com.wipro.www.sonhms.exceptions.ConfigDbNotFoundException;
32 import com.wipro.www.sonhms.model.CellPciPair;
33 import com.wipro.www.sonhms.model.FapServiceList;
34 import com.wipro.www.sonhms.model.LteNeighborListInUseLteCell;
35 import com.wipro.www.sonhms.model.Notification;
36 import com.wipro.www.sonhms.model.ThreadId;
37 import com.wipro.www.sonhms.restclient.SdnrRestClient;
38 import com.wipro.www.sonhms.utils.BeanUtil;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
46 import java.util.UUID;
47 import java.util.concurrent.BlockingQueue;
48 import java.util.concurrent.LinkedBlockingQueue;
50 import org.slf4j.Logger;
52 public class SdnrNotificationHandlingState implements SonState {
53 private static Map<Long, ChildThread> childThreadMap = new HashMap<>();
54 private static final Logger log = org.slf4j.LoggerFactory.getLogger(SdnrNotificationHandlingState.class);
57 public void stateChange(SonContext sonContext) {
58 // logic to determine if notif to be processed
59 log.debug("inside statechange of sdnr notif state");
60 String notification = sonContext.getSdnrNotification();
61 Notification notificationObject;
64 ObjectMapper mapper = new ObjectMapper();
65 notificationObject = mapper.readValue(notification, Notification.class);
66 log.debug("notificationObject{}", notificationObject);
68 List<FapServiceList> serviceList = notificationObject.getPayload().getRadioAccess().getFapServiceList();
69 for (FapServiceList fapService : serviceList) {
70 String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();
71 log.debug("cellId:{}", cellId);
72 log.debug("inside for loop");
74 List<ClusterDetails> clusterDetails = getAllClusters();
76 ClusterDetails clusterDetail = getClusterForNotification(fapService, clusterDetails);
78 if (clusterDetail == null) {
80 Graph cluster = createCluster(fapService);
82 UUID clusterId = UUID.randomUUID();
83 cluster.setGraphId(clusterId);
84 // create the child thread
85 log.debug("creating new child");
86 BlockingQueue<FapServiceList> queue = new LinkedBlockingQueue<>();
87 ThreadId threadId = new ThreadId();
88 threadId.setChildThreadId(0);
89 ChildThread child = new ChildThread(sonContext.getChildStatusUpdate(), cluster, queue, threadId);
90 queue.put(fapService);
91 MainThreadComponent mainThreadComponent = BeanUtil.getBean(MainThreadComponent.class);
92 mainThreadComponent.getPool().execute(child);
94 waitForThreadId(threadId);
96 saveCluster(cluster, clusterId, threadId.getChildThreadId());
97 addChildThreadMap(threadId.getChildThreadId(), child);
98 sonContext.addChildStatus(threadId.getChildThreadId(), "processingNotifications");
103 if (isOofTriggeredForCluster(sonContext, clusterDetail)) {
104 sonContext.setNotifToBeProcessed(false);
105 bufferNotification(fapService, clusterDetail.getClusterId());
107 sonContext.setNotifToBeProcessed(true);
108 log.debug("childThreadId:{}", clusterDetail.getChildThreadId());
109 childThreadMap.get(clusterDetail.getChildThreadId()).putInQueue(fapService);
113 } catch (Exception e) {
114 log.error("caught in sdnr notif handling state{}", e);
117 WaitState waitState = WaitState.getInstance();
118 sonContext.setPciState(waitState);
119 sonContext.stateChange(sonContext);
122 private void waitForThreadId(ThreadId threadId) {
124 synchronized (threadId) {
125 while (threadId.getChildThreadId() == 0) {
129 } catch (InterruptedException e) {
131 log.error("ChildThread queue error {}", e);
132 Thread.currentThread().interrupt();
136 private String saveCluster(Graph cluster, UUID clusterId, Long threadId) {
138 String cellPciNeighbourString = cluster.getPciNeighbourJson();
140 log.debug("cluster hahsmap to string : {}", cellPciNeighbourString);
141 cluster.setGraphId(clusterId);
143 ClusterDetails details = new ClusterDetails();
144 details.setClusterId(clusterId.toString());
145 details.setClusterInfo(cellPciNeighbourString);
146 details.setChildThreadId(threadId);
148 ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class);
149 clusterDetailsRepository.save(details);
151 return clusterId.toString();
154 private Graph createCluster(FapServiceList fapService) throws ConfigDbNotFoundException {
156 Graph cluster = new Graph();
157 log.debug("cluster formation started");
158 int phycellId = fapService.getX0005b9Lte().getPhyCellIdInUse();
159 String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();
161 CellInfoRepository cellInfoRepository = BeanUtil.getBean(CellInfoRepository.class);
162 cellInfoRepository.save(new CellInfo(cellId, fapService.getX0005b9Lte().getPnfName()));
164 CellPciPair val = new CellPciPair();
165 val.setCellId(cellId);
166 val.setPhysicalCellId(phycellId);
167 List<LteNeighborListInUseLteCell> neighbourlist;
168 neighbourlist = fapService.getCellConfig().getLte().getRan().getNeighborListInUse()
169 .getLteNeighborListInUseLteCell();
170 log.debug("Neighbor list size: {}", neighbourlist.size());
172 for (int i = 0; i < neighbourlist.size(); i++) {
173 String cell = neighbourlist.get(i).getAlias();
174 int phy = neighbourlist.get(i).getPhyCellId();
176 cellInfoRepository.save(new CellInfo(cell, neighbourlist.get(i).getPnfName()));
178 log.debug("cellID: {}", cell);
179 log.debug("PCI: {}", phy);
180 CellPciPair val1 = new CellPciPair();
181 val1.setCellId(cell);
182 val1.setPhysicalCellId(phy);
183 cluster.addEdge(val, val1);
184 log.debug("cluster: {}", cluster);
186 List<CellPciPair> nbrList = SdnrRestClient.getNbrList(neighbourlist.get(i).getAlias());
188 for (CellPciPair nbr : nbrList) {
189 String cid = nbr.getCellId();
190 int pci = nbr.getPhysicalCellId();
191 CellPciPair val3 = new CellPciPair();
193 val3.setPhysicalCellId(pci);
195 cluster.addEdge(val1, val3);
199 log.debug("final cluster: {}", cluster);
203 private void bufferNotification(FapServiceList fapService, String clusterId) {
205 ObjectMapper mapper = new ObjectMapper();
206 BufferNotificationComponent bufferNotifComponent = new BufferNotificationComponent();
207 String serviceListString = "";
209 serviceListString = mapper.writeValueAsString(fapService);
210 } catch (JsonProcessingException e) {
211 log.debug("JSON processing exception: {}", e);
213 bufferNotifComponent.bufferNotification(serviceListString, clusterId);
217 private boolean isOofTriggeredForCluster(SonContext pciContext, ClusterDetails clusterDetail) {
218 Long childThreadId = clusterDetail.getChildThreadId();
219 String childStatus = pciContext.getChildStatus(childThreadId);
220 return childStatus.equals("triggeredOof");
224 private ClusterDetails getClusterForNotification(FapServiceList fapService, List<ClusterDetails> clusterDetails) {
226 String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();
227 Map<CellPciPair, ArrayList<CellPciPair>> cellPciNeighbourMap;
229 for (ClusterDetails clusterDetail : clusterDetails) {
230 Graph cluster = new Graph(clusterDetail.getClusterInfo());
231 cellPciNeighbourMap = cluster.getCellPciNeighbourMap();
232 Set<CellPciPair> keys = cellPciNeighbourMap.keySet();
233 Iterator<CellPciPair> traverse = keys.iterator();
234 while (traverse.hasNext()) {
235 CellPciPair key = traverse.next();
236 String currentCellId = key.getCellId();
237 if (cellId.equals(currentCellId)) {
238 return clusterDetail;
246 private List<ClusterDetails> getAllClusters() {
247 ClusterDetailsComponent clusterDetailsComponent = new ClusterDetailsComponent();
248 return clusterDetailsComponent.getClusterDetails();
251 public static void addChildThreadMap(Long childThreadId, ChildThread child) {
252 childThreadMap.put(childThreadId, child);
255 public static Map<Long, ChildThread> getChildThreadMap() {
256 return childThreadMap;