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