Fix bug in filtering new FM notification
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / utils / ClusterUtils.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
10  *  
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *  
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  *  
20  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms.utils;
23
24 import fj.data.Either;
25
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;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34
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;
54
55 public class ClusterUtils {
56
57     private static Logger log = LoggerFactory.getLogger(ClusterUtils.class);
58     Configuration config = Configuration.getInstance();
59
60     public List<ClusterDetails> getAllClusters() {
61         ClusterDetailsComponent clusterDetailsComponent = new ClusterDetailsComponent();
62         return clusterDetailsComponent.getClusterDetails();
63     }
64
65     /**
66      * Get cluster for FM notifications.
67      */
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());
76
77                 Set<String> clusterCells = getCellsInCluster(cluster);
78                 for (String clusterCell : clusterCells) {
79                     if (cellId.equals(clusterCell)) {
80                         cellsInCluster.put(cellId, clusterDetail.getClusterId());
81                         break;
82
83                     }
84
85                 }
86
87             }
88             if (!cellsInCluster.containsKey(cellId)) {
89                 newCells.add(cellId);
90             }
91
92         }
93
94         faultNotificationtoClusterMapping.setCellsinCluster(cellsInCluster);
95         faultNotificationtoClusterMapping.setNewCells(newCells);
96         return faultNotificationtoClusterMapping;
97
98     }
99
100     /**
101      * Get clusters for notifications.
102      */
103     public NotificationToClusterMapping getClustersForNotification(Notification notification,
104             List<ClusterDetails> clusterDetails) {
105
106         NotificationToClusterMapping mapping = new NotificationToClusterMapping();
107
108         Map<FapServiceList, String> cellsInCluster = new HashMap<>();
109         List<FapServiceList> newCells = new ArrayList<>();
110
111         List<FapServiceList> fapServiceList = notification.getPayload().getRadioAccess().getFapServiceList();
112
113         for (FapServiceList fapService : fapServiceList) {
114
115             for (ClusterDetails clusterDetail : clusterDetails) {
116
117                 Set<String> cellsInNotification = getCellsInNotification(fapService);
118
119                 Graph cluster = new Graph(clusterDetail.getClusterInfo());
120
121                 Set<String> clusterCells = getCellsInCluster(cluster);
122
123                 log.debug("cells in cluster {}", clusterCells);
124
125                 cellsInNotification.retainAll(clusterCells);
126
127                 if (!cellsInNotification.isEmpty()) {
128                     log.debug("cell or it's neighbour in the cluster");
129                     cellsInCluster.put(fapService, clusterDetail.getClusterId());
130                     break;
131                 }
132             }
133
134             if (!cellsInCluster.containsKey(fapService)) {
135                 newCells.add(fapService);
136             }
137         }
138
139         mapping.setCellsinCluster(cellsInCluster);
140         mapping.setNewCells(newCells);
141         return mapping;
142     }
143
144     /**
145      * Get cluster details from cluster ID.
146      */
147     public Either<ClusterDetails, Integer> getClusterDetailsFromClusterId(String clusterId,
148             List<ClusterDetails> clusterDetails) {
149
150         for (ClusterDetails clusterDetail : clusterDetails) {
151             if (clusterDetail.getClusterId().equals(clusterId)) {
152                 return Either.left(clusterDetail);
153             }
154         }
155         return Either.right(404);
156     }
157
158     /**
159      * Get clusters for Cell.
160      */
161     public Either<Graph, Integer> getClusterForCell(FapServiceList fapService, List<Graph> newClusters) {
162
163         if (newClusters.isEmpty()) {
164             return Either.right(404);
165         }
166
167         for (Graph cluster : newClusters) {
168
169             Set<String> cellsInNotification = getCellsInNotification(fapService);
170             Set<String> clusterCells = getCellsInCluster(cluster);
171
172             cellsInNotification.retainAll(clusterCells);
173
174             if (!(cellsInNotification.isEmpty())) {
175                 return Either.left(cluster);
176             }
177
178         }
179
180         return Either.right(404);
181     }
182
183     /**
184      * Get clusters for FM Cell.
185      */
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);
190         }
191         for (Graph cluster : newClusters) {
192
193             Set<String> clusterCells = getCellsInCluster(cluster);
194             for (String clusterCell : clusterCells) {
195                 if (cellId.equals(clusterCell)) {
196                     return Either.left(cluster);
197
198                 }
199
200             }
201
202         }
203         return Either.right(404);
204
205     }
206
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());
214         }
215
216         return cellsInNotification;
217     }
218
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());
227         }
228
229         return clusterCells;
230     }
231     
232     /**
233      * Create cluster.
234      */
235     public Graph createCluster(Map<CellPciPair, ArrayList<CellPciPair>> clusterMap) throws ConfigDbNotFoundException, CpsNotFoundException
236     {
237         Graph cluster = new Graph();
238         log.debug("cluster formation started");
239
240         Set<CellPciPair> keySet = clusterMap.keySet();
241         Iterator<CellPciPair> iterate = keySet.iterator();
242         CellPciPair val = (CellPciPair) iterate.next();
243
244         List<CellPciPair> firstNeighbourlist = clusterMap.get(val);
245
246         for (int i = 0; i < firstNeighbourlist.size(); i++) {
247             String cell = firstNeighbourlist.get(i).getCellId();
248             int phy = firstNeighbourlist.get(i).getPhysicalCellId();
249
250             CellPciPair val1 = new CellPciPair();
251             val1.setCellId(cell);
252             val1.setPhysicalCellId(phy);
253             cluster.addEdge(val, val1);
254
255             List<CellPciPair> nbrList = config.getConfigurationClient().getNbrList(cell);
256
257             for (CellPciPair nbr : nbrList) {
258                 String cid = nbr.getCellId();
259                 int pci = nbr.getPhysicalCellId();
260                 CellPciPair val3 = new CellPciPair();
261                 val3.setCellId(cid);
262                 val3.setPhysicalCellId(pci);
263
264                 cluster.addEdge(val1, val3);
265             }
266         }
267
268         log.debug("final cluster: {}", cluster);
269         return cluster;
270     }
271
272     /**
273      * Save cluster.
274      */
275     public String saveCluster(Graph cluster, UUID clusterId, Long threadId) {
276
277         String cellPciNeighbourString = cluster.getPciNeighbourJson();
278
279         log.debug("cluster hahsmap to string : {}", cellPciNeighbourString);
280         cluster.setGraphId(clusterId);
281
282         ClusterDetails details = new ClusterDetails();
283         details.setClusterId(clusterId.toString());
284         details.setClusterInfo(cellPciNeighbourString);
285         details.setChildThreadId(threadId);
286
287         ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class);
288         clusterDetailsRepository.save(details);
289
290         return clusterId.toString();
291     }
292
293     /**
294      * Update cluster.
295      */
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());
301     }
302
303     /**
304      * Find cluster Map.
305      */
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);
318
319         return clusterMap;
320     }
321
322     /**
323      * Modify cluster.
324      */
325     public Graph modifyCluster(Graph cluster, Map<CellPciPair, ArrayList<CellPciPair>> clusterMap) {
326
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);
332
333         Map<CellPciPair, ArrayList<CellPciPair>> existingClusterMap;
334         existingClusterMap = cluster.getCellPciNeighbourMap();
335         // coe
336
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);
341         }
342
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);
349
350             if (cellId.equals(cell)) {
351
352                 // removes the old neighbours and adds new neighbours for that cell
353                 cluster.updateVertex(mapVal, mainCellPciPair);
354
355             }
356
357         }
358
359         /////// update cluster with new pci values for the same cell
360
361         if (existingClusterMap.containsKey(mainCellPciPair)) {
362             ArrayList<CellPciPair> oldClusterArray;
363             oldClusterArray = existingClusterMap.get(mainCellPciPair);
364             oldClusterArray.clear();
365
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();
370                 val2.setCellId(cid);
371                 val2.setPhysicalCellId(phy);
372                 cluster.addEdge(mainCellPciPair, val2);
373             }
374
375         }
376
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);
389
390                 if (cid1.equals(cell)) {
391
392                     // removes the old neighbours and adds new neighbours for that cell
393                     cluster.updateVertex(mapVal, val3);
394
395                 }
396
397             }
398         }
399
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);
408             }
409
410         }
411
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);
419         }
420
421         log.info("Modified Cluster {}", cluster);
422
423         return cluster;
424     }
425
426 }