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.utils;
24 import fj.data.Either;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
33 import java.util.UUID;
35 import org.onap.dcaegen2.services.sonhms.ClusterDetailsComponent;
36 import org.onap.dcaegen2.services.sonhms.Configuration;
37 import org.onap.dcaegen2.services.sonhms.FaultNotificationtoClusterMapping;
38 import org.onap.dcaegen2.services.sonhms.NotificationToClusterMapping;
39 import org.onap.dcaegen2.services.sonhms.child.Graph;
40 import org.onap.dcaegen2.services.sonhms.dao.ClusterDetailsRepository;
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.exceptions.CpsNotFoundException;
44 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
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.restclient.ConfigInterface;
49 import org.onap.dcaegen2.services.sonhms.restclient.ConfigurationClient;
50 import org.onap.dcaegen2.services.sonhms.restclient.CpsClient;
51 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class ClusterUtils {
57 private static Logger log = LoggerFactory.getLogger(ClusterUtils.class);
58 Configuration config = Configuration.getInstance();
60 public List<ClusterDetails> getAllClusters() {
61 ClusterDetailsComponent clusterDetailsComponent = new ClusterDetailsComponent();
62 return clusterDetailsComponent.getClusterDetails();
66 * Get cluster for FM notifications.
68 public FaultNotificationtoClusterMapping getClustersForFmNotification(Set<String> cellIds,
69 List<ClusterDetails> clusterDetails) {
70 List<String> newCells = new ArrayList<>();
71 Map<String, String> cellsInCluster = new HashMap<String, String>();
72 FaultNotificationtoClusterMapping faultNotificationtoClusterMapping = new FaultNotificationtoClusterMapping();
73 for (String cellId : cellIds) {
74 for (ClusterDetails clusterDetail : clusterDetails) {
75 Graph cluster = new Graph(clusterDetail.getClusterInfo());
77 Set<String> clusterCells = getCellsInCluster(cluster);
78 for (String clusterCell : clusterCells) {
79 if (cellId.equals(clusterCell)) {
80 cellsInCluster.put(cellId, clusterDetail.getClusterId());
88 if (!cellsInCluster.containsKey(cellId)) {
94 faultNotificationtoClusterMapping.setCellsinCluster(cellsInCluster);
95 faultNotificationtoClusterMapping.setNewCells(newCells);
96 return faultNotificationtoClusterMapping;
101 * Get clusters for notifications.
103 public NotificationToClusterMapping getClustersForNotification(Notification notification,
104 List<ClusterDetails> clusterDetails) {
106 NotificationToClusterMapping mapping = new NotificationToClusterMapping();
108 Map<FapServiceList, String> cellsInCluster = new HashMap<>();
109 List<FapServiceList> newCells = new ArrayList<>();
111 List<FapServiceList> fapServiceList = notification.getPayload().getRadioAccess().getFapServiceList();
113 for (FapServiceList fapService : fapServiceList) {
115 for (ClusterDetails clusterDetail : clusterDetails) {
117 Set<String> cellsInNotification = getCellsInNotification(fapService);
119 Graph cluster = new Graph(clusterDetail.getClusterInfo());
121 Set<String> clusterCells = getCellsInCluster(cluster);
123 log.debug("cells in cluster {}", clusterCells);
125 cellsInNotification.retainAll(clusterCells);
127 if (!cellsInNotification.isEmpty()) {
128 log.debug("cell or it's neighbour in the cluster");
129 cellsInCluster.put(fapService, clusterDetail.getClusterId());
134 if (!cellsInCluster.containsKey(fapService)) {
135 newCells.add(fapService);
139 mapping.setCellsinCluster(cellsInCluster);
140 mapping.setNewCells(newCells);
145 * Get cluster details from cluster ID.
147 public Either<ClusterDetails, Integer> getClusterDetailsFromClusterId(String clusterId,
148 List<ClusterDetails> clusterDetails) {
150 for (ClusterDetails clusterDetail : clusterDetails) {
151 if (clusterDetail.getClusterId().equals(clusterId)) {
152 return Either.left(clusterDetail);
155 return Either.right(404);
159 * Get clusters for Cell.
161 public Either<Graph, Integer> getClusterForCell(FapServiceList fapService, List<Graph> newClusters) {
163 if (newClusters.isEmpty()) {
164 return Either.right(404);
167 for (Graph cluster : newClusters) {
169 Set<String> cellsInNotification = getCellsInNotification(fapService);
170 Set<String> clusterCells = getCellsInCluster(cluster);
172 cellsInNotification.retainAll(clusterCells);
174 if (!(cellsInNotification.isEmpty())) {
175 return Either.left(cluster);
180 return Either.right(404);
184 * Get clusters for FM Cell.
186 public Either<Graph, Integer> getClusterForFmCell(String cellId, List<Graph> newClusters) {
187 if (newClusters.isEmpty()) {
188 log.info("getClusterForFMCell 404");
189 return Either.right(404);
191 for (Graph cluster : newClusters) {
193 Set<String> clusterCells = getCellsInCluster(cluster);
194 for (String clusterCell : clusterCells) {
195 if (cellId.equals(clusterCell)) {
196 return Either.left(cluster);
203 return Either.right(404);
207 private Set<String> getCellsInNotification(FapServiceList fapService) {
208 Set<String> cellsInNotification = new HashSet<>();
209 cellsInNotification.add(fapService.getAlias());
210 List<LteNeighborListInUseLteCell> nbrList = fapService.getCellConfig().getLte().getRan().getNeighborListInUse()
211 .getLteNeighborListInUseLteCell();
212 for (LteNeighborListInUseLteCell nbr : nbrList) {
213 cellsInNotification.add(nbr.getAlias());
216 return cellsInNotification;
219 private Set<String> getCellsInCluster(Graph cluster) {
220 Map<CellPciPair, ArrayList<CellPciPair>> cellPciNeighbourMap = cluster.getCellPciNeighbourMap();
221 log.debug("cell_pci_map {}", cellPciNeighbourMap);
222 Set<CellPciPair> keys = cellPciNeighbourMap.keySet();
223 Set<String> clusterCells = new HashSet<>();
224 for (CellPciPair cellPciPair : keys) {
225 log.debug("cells {}", cellPciPair.getCellId());
226 clusterCells.add(cellPciPair.getCellId());
235 public Graph createCluster(Map<CellPciPair, ArrayList<CellPciPair>> clusterMap) throws ConfigDbNotFoundException, CpsNotFoundException
237 Graph cluster = new Graph();
238 log.debug("cluster formation started");
240 Set<CellPciPair> keySet = clusterMap.keySet();
241 Iterator<CellPciPair> iterate = keySet.iterator();
242 CellPciPair val = (CellPciPair) iterate.next();
244 List<CellPciPair> firstNeighbourlist = clusterMap.get(val);
246 for (int i = 0; i < firstNeighbourlist.size(); i++) {
247 String cell = firstNeighbourlist.get(i).getCellId();
248 int phy = firstNeighbourlist.get(i).getPhysicalCellId();
250 CellPciPair val1 = new CellPciPair();
251 val1.setCellId(cell);
252 val1.setPhysicalCellId(phy);
253 cluster.addEdge(val, val1);
255 List<CellPciPair> nbrList = config.getConfigurationClient().getNbrList(cell);
257 for (CellPciPair nbr : nbrList) {
258 String cid = nbr.getCellId();
259 int pci = nbr.getPhysicalCellId();
260 CellPciPair val3 = new CellPciPair();
262 val3.setPhysicalCellId(pci);
264 cluster.addEdge(val1, val3);
268 log.debug("final cluster: {}", cluster);
275 public String saveCluster(Graph cluster, UUID clusterId, Long threadId) {
277 String cellPciNeighbourString = cluster.getPciNeighbourJson();
279 log.debug("cluster hahsmap to string : {}", cellPciNeighbourString);
280 cluster.setGraphId(clusterId);
282 ClusterDetails details = new ClusterDetails();
283 details.setClusterId(clusterId.toString());
284 details.setClusterInfo(cellPciNeighbourString);
285 details.setChildThreadId(threadId);
287 ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class);
288 clusterDetailsRepository.save(details);
290 return clusterId.toString();
296 public void updateCluster(Graph cluster) {
297 String cellPciNeighbourString = cluster.getPciNeighbourJson();
298 UUID clusterId = cluster.getGraphId();
299 ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class);
300 clusterDetailsRepository.updateCluster(cellPciNeighbourString, clusterId.toString());
306 public Map<CellPciPair, ArrayList<CellPciPair>> findClusterMap(String cellId) throws ConfigDbNotFoundException, CpsNotFoundException {
307 log.info("inside clusterMap");
308 int phyCellId = config.getConfigurationClient().getPci(cellId);
309 log.info("phyCellId of clustermap is" + phyCellId);
310 CellPciPair main = new CellPciPair();
311 main.setCellId(cellId);
312 main.setPhysicalCellId(phyCellId);
313 ArrayList<CellPciPair> cellPciPairs;
314 cellPciPairs = (ArrayList<CellPciPair>) config.getConfigurationClient().getNbrList(cellId);
315 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<>();
316 clusterMap.put(main, cellPciPairs);
317 log.info("clusterMap{}", clusterMap);
325 public Graph modifyCluster(Graph cluster, Map<CellPciPair, ArrayList<CellPciPair>> clusterMap) {
327 Set<CellPciPair> keySet = clusterMap.keySet();
328 Iterator<CellPciPair> iterate = keySet.iterator();
329 CellPciPair mainCellPciPair = (CellPciPair) iterate.next();
330 String cellId = mainCellPciPair.getCellId();
331 List<CellPciPair> newNeighbourList = clusterMap.get(mainCellPciPair);
333 Map<CellPciPair, ArrayList<CellPciPair>> existingClusterMap;
334 existingClusterMap = cluster.getCellPciNeighbourMap();
337 List<CellPciPair> tempCellPair = new ArrayList<>();
338 for (Map.Entry<CellPciPair, ArrayList<CellPciPair>> entry : existingClusterMap.entrySet()) {
339 CellPciPair oldClusterKeys = entry.getKey();
340 tempCellPair.add(oldClusterKeys);
343 for (CellPciPair entry : tempCellPair) {
344 String cell = entry.getCellId();
345 int physicalCell = entry.getPhysicalCellId();
346 CellPciPair mapVal = new CellPciPair();
347 mapVal.setCellId(cell);
348 mapVal.setPhysicalCellId(physicalCell);
350 if (cellId.equals(cell)) {
352 // removes the old neighbours and adds new neighbours for that cell
353 cluster.updateVertex(mapVal, mainCellPciPair);
359 /////// update cluster with new pci values for the same cell
361 if (existingClusterMap.containsKey(mainCellPciPair)) {
362 ArrayList<CellPciPair> oldClusterArray;
363 oldClusterArray = existingClusterMap.get(mainCellPciPair);
364 oldClusterArray.clear();
366 for (int i = 0; i < newNeighbourList.size(); i++) {
367 String cid = newNeighbourList.get(i).getCellId();
368 int phy = newNeighbourList.get(i).getPhysicalCellId();
369 CellPciPair val2 = new CellPciPair();
371 val2.setPhysicalCellId(phy);
372 cluster.addEdge(mainCellPciPair, val2);
377 for (CellPciPair entry : tempCellPair) {
378 String cell = entry.getCellId();
379 int physicalCell = entry.getPhysicalCellId();
380 CellPciPair mapVal = new CellPciPair();
381 mapVal.setCellId(cell);
382 mapVal.setPhysicalCellId(physicalCell);
383 for (int j = 0; j < newNeighbourList.size(); j++) {
384 String cid1 = newNeighbourList.get(j).getCellId();
385 int phy1 = newNeighbourList.get(j).getPhysicalCellId();
386 CellPciPair val3 = new CellPciPair();
387 val3.setCellId(cid1);
388 val3.setPhysicalCellId(phy1);
390 if (cid1.equals(cell)) {
392 // removes the old neighbours and adds new neighbours for that cell
393 cluster.updateVertex(mapVal, val3);
400 for (int j = 0; j < newNeighbourList.size(); j++) {
401 String cid1 = newNeighbourList.get(j).getCellId();
402 int phy1 = newNeighbourList.get(j).getPhysicalCellId();
403 CellPciPair val3 = new CellPciPair();
404 val3.setCellId(cid1);
405 val3.setPhysicalCellId(phy1);
406 if (existingClusterMap.containsKey(val3)) {
407 cluster.addEdge(mainCellPciPair, val3);
412 for (int k = 0; k < newNeighbourList.size(); k++) {
413 String cid2 = newNeighbourList.get(k).getCellId();
414 int phy2 = newNeighbourList.get(k).getPhysicalCellId();
415 CellPciPair val5 = new CellPciPair();
416 val5.setCellId(cid2);
417 val5.setPhysicalCellId(phy2);
418 cluster.addEdge(mainCellPciPair, val5);
421 log.info("Modified Cluster {}", cluster);