Implement Enhancements to the Microservice
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / PMNotificationHandler.java
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * pcims
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
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 package org.onap.dcaegen2.services.sonhms;
22
23 import com.fasterxml.jackson.annotation.JsonInclude.Include;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.onap.dcaegen2.services.sonhms.dao.HandOverMetricsRepository;
28 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
29 import org.onap.dcaegen2.services.sonhms.entity.HandOverMetrics;
30 import org.onap.dcaegen2.services.sonhms.model.AdditionalMeasurements;
31 import org.onap.dcaegen2.services.sonhms.model.CellConfig;
32 import org.onap.dcaegen2.services.sonhms.model.Common;
33 import org.onap.dcaegen2.services.sonhms.model.Configurations;
34 import org.onap.dcaegen2.services.sonhms.model.Data;
35 import org.onap.dcaegen2.services.sonhms.model.FapService;
36 import org.onap.dcaegen2.services.sonhms.model.HoDetails;
37 import org.onap.dcaegen2.services.sonhms.model.Lte;
38 import org.onap.dcaegen2.services.sonhms.model.LteCell;
39 import org.onap.dcaegen2.services.sonhms.model.NeighborListInUse;
40 import org.onap.dcaegen2.services.sonhms.model.PMNotification;
41 import org.onap.dcaegen2.services.sonhms.model.Payload;
42 import org.onap.dcaegen2.services.sonhms.model.Ran;
43 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class PMNotificationHandler {
48
49     private static Logger log = LoggerFactory.getLogger(DmaapNotificationsComponent.class);
50     PolicyDmaapClient policyDmaapClient;
51
52     public PMNotificationHandler() {
53
54     }
55
56     public PMNotificationHandler(PolicyDmaapClient policyDmaapClient) {
57         this.policyDmaapClient = policyDmaapClient;
58     }
59
60     /**
61      * handle pm notifications.
62      */
63     public Boolean handlePmNotifications(PMNotification pmNotification, int badThreshold) {
64         HandOverMetricsRepository handOverMetricsRepository = BeanUtil.getBean(HandOverMetricsRepository.class);
65         Boolean result;
66         try {
67             List<HoDetails> hoDetailsList = new ArrayList<>();
68             List<LteCell> lteCellList = new ArrayList<>();
69             String srcCellId = pmNotification.getEvent().getCommonEventHeader().getSourceName();
70             for (AdditionalMeasurements additionalMeasurements : pmNotification.getEvent().getMeasurement()
71                     .getAdditionalMeasurements()) {
72                 int attemptsCount = Integer
73                         .parseInt(additionalMeasurements.getArrayOfNamedHashMap().get(1).get("InterEnbOutAtt_X2HO"));
74                 int successCount = Integer
75                         .parseInt(additionalMeasurements.getArrayOfNamedHashMap().get(2).get("InterEnbOutSucc_X2HO"));
76                 float successRate = ((float) successCount / attemptsCount) * 100;
77                 if (successRate >= badThreshold) {
78                     HoDetails hoDetails = new HoDetails();
79                     hoDetails.setDstCellId(additionalMeasurements.getName());
80                     hoDetails.setAttemptsCount(attemptsCount);
81                     hoDetails.setSuccessCount(successCount);
82                     hoDetails.setSuccessRate(successRate);
83                     hoDetailsList.add(hoDetails);
84                     log.info("not bad  neighbor {}",additionalMeasurements.getName());
85                 } else {
86                     log.info(" bad  neighbor {}",additionalMeasurements.getName());
87                     LteCell lteCell = new LteCell();
88                     lteCell.setBlacklisted("true");
89                     lteCell.setCid(additionalMeasurements.getName());
90                     lteCell.setPlmnId(additionalMeasurements.getArrayOfNamedHashMap().get(0).get("networkId"));
91                     lteCell.setPnfName(pmNotification.getEvent().getCommonEventHeader().getReportingEntityName());
92                     lteCellList.add(lteCell);
93                 }
94             }
95             if (!lteCellList.isEmpty()) {
96                 log.info("triggering policy to remove bad neighbors");
97                 result = sendAnrUpdateToPolicy(pmNotification, lteCellList);
98                 log.info("Sent ANR update to policy {}", result);
99                 String hoDetailsString = handOverMetricsRepository.getHandOverMetrics(srcCellId);
100                 if (hoDetailsString != null) {
101                     ObjectMapper mapper = new ObjectMapper();
102                     String newHoDetailsString = null;
103                     try {
104                         newHoDetailsString = mapper.writeValueAsString(hoDetailsList);
105                     } catch (Exception e) {
106                         log.error("Error in writing handover metrics json ", e);
107                         return false;
108                     }
109                     handOverMetricsRepository.updateHoMetrics(newHoDetailsString, srcCellId);            
110                 }
111             }
112             if (!hoDetailsList.isEmpty()) {
113                 result = saveToHandOverMetrics(hoDetailsList, srcCellId);
114                 log.debug("save HO metrics result {} ", result);
115
116             }
117
118         } catch (Exception e) {
119             log.error("Error in handlePmNotifications ", e);
120             return false;
121         }
122         return true;
123
124     }
125
126     private Boolean sendAnrUpdateToPolicy(PMNotification pmNotification, List<LteCell> lteCellList) {
127         ObjectMapper mapper = new ObjectMapper();
128         try {
129             mapper.setSerializationInclusion(Include.NON_NULL);
130             ArrayList<Configurations> configurations = new ArrayList<>();
131             String cellId = pmNotification.getEvent().getCommonEventHeader().getSourceName();
132             Configurations configuration = new Configurations(
133                     new Data(new FapService(cellId, null, new CellConfig(new Lte(new Ran(new Common(cellId),
134                             new NeighborListInUse(null, lteCellList, String.valueOf(lteCellList.size()))))))));
135             configurations.add(configuration);
136             Payload payload = new Payload(configurations);
137             log.info("payload : {}",   payload);
138             String anrUpdateString = mapper.writeValueAsString(payload);
139             
140             Boolean result = policyDmaapClient.sendNotificationToPolicy(anrUpdateString);
141             log.debug("send notification to policy result {} ", result);
142         } catch (Exception e) {
143             log.error("Exception in sending Anr update to policy ", e);
144             return false;
145         }
146         return true;
147     }
148
149     private Boolean saveToHandOverMetrics(List<HoDetails> hoDetailsList, String srcCellId) {
150         HandOverMetricsRepository handOverMetricsRepository = BeanUtil.getBean(HandOverMetricsRepository.class);
151         ObjectMapper mapper = new ObjectMapper();
152         String hoDetailsString = null;
153         try {
154             hoDetailsString = mapper.writeValueAsString(hoDetailsList);
155         } catch (Exception e) {
156             log.error("Error in writing handover metrics json ", e);
157             return false;
158         }
159         
160         if (handOverMetricsRepository.getHandOverMetrics(srcCellId) == null) {
161             HandOverMetrics handOverMetrics = new HandOverMetrics();
162             handOverMetrics.setHoDetails(hoDetailsString);
163             handOverMetrics.setSrcCellId(srcCellId);
164             handOverMetricsRepository.save(handOverMetrics);
165         }
166         else {
167             handOverMetricsRepository.updateHoMetrics(hoDetailsString, srcCellId);
168         }
169         return true;
170     }
171 }