1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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;
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;
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;
53 public class PmNotificationHandler {
55 private static Logger log = LoggerFactory.getLogger(DmaapNotificationsComponent.class);
56 PolicyDmaapClient policyDmaapClient;
58 public PmNotificationHandler() {
62 public PmNotificationHandler(PolicyDmaapClient policyDmaapClient) {
63 this.policyDmaapClient = policyDmaapClient;
67 * handle PM notifications.
69 public Boolean handlePmNotifications(PMNotification pmNotification, int badThreshold) {
70 HandOverMetricsRepository handOverMetricsRepository = BeanUtil.getBean(HandOverMetricsRepository.class);
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());
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);
101 if (!lteCellList.isEmpty()) {
102 log.info("triggering policy to remove bad neighbors");
103 Flag policyTriggerFlag = BeanUtil.getBean(Flag.class);
105 while (policyTriggerFlag.getHolder().equals("CHILD")) {
109 policyTriggerFlag.setHolder("PM");
110 result = sendAnrUpdateToPolicy(pmNotification, lteCellList);
111 log.info("Sent ANR update to policy {}", result);
112 policyTriggerFlag.setHolder("NONE");
114 String hoDetailsString = handOverMetricsRepository.getHandOverMetrics(srcCellId);
115 if (hoDetailsString != null) {
116 ObjectMapper mapper = new ObjectMapper();
117 String newHoDetailsString = null;
119 newHoDetailsString = mapper.writeValueAsString(hoDetailsList);
120 } catch (Exception e) {
121 log.error("Error in writing handover metrics json ", e);
124 handOverMetricsRepository.updateHoMetrics(newHoDetailsString, srcCellId);
127 if (!hoDetailsList.isEmpty()) {
128 result = saveToHandOverMetrics(hoDetailsList, srcCellId);
129 log.debug("save HO metrics result {} ", result);
133 } catch (Exception e) {
134 log.error("Error in handlePmNotifications ", e);
141 private Boolean sendAnrUpdateToPolicy(PMNotification pmNotification, List<LteCell> lteCellList) {
142 ObjectMapper mapper = new ObjectMapper();
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);
162 } catch (Exception e) {
163 log.error("Exception in sending Anr update to policy ", e);
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;
174 hoDetailsString = mapper.writeValueAsString(hoDetailsList);
175 } catch (Exception e) {
176 log.error("Error in writing handover metrics json ", e);
180 if (handOverMetricsRepository.getHandOverMetrics(srcCellId) == null) {
181 HandOverMetrics handOverMetrics = new HandOverMetrics();
182 handOverMetrics.setHoDetails(hoDetailsString);
183 handOverMetrics.setSrcCellId(srcCellId);
184 handOverMetricsRepository.save(handOverMetrics);
186 handOverMetricsRepository.updateHoMetrics(hoDetailsString, srcCellId);