Modify policy-notification to align with policy.
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / dmaap / PolicyNotificationCallback.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2020 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.dmaap;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.onap.dcaegen2.services.sonhms.Configuration;
29 import org.onap.dcaegen2.services.sonhms.dao.FixedPciCellsRepository;
30 import org.onap.dcaegen2.services.sonhms.dao.PciUpdateRepository;
31 import org.onap.dcaegen2.services.sonhms.entity.FixedPciCells;
32 import org.onap.dcaegen2.services.sonhms.model.Configurations;
33 import org.onap.dcaegen2.services.sonhms.model.Payload;
34 import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
35 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
36 import org.slf4j.Logger;
37
38 public class PolicyNotificationCallback extends NotificationCallback {
39
40         private static final Logger log = org.slf4j.LoggerFactory.getLogger(PolicyNotificationCallback.class);
41
42         @Override
43         public void activateCallBack(String msg) {
44                 handlePolicyNotification(msg);
45         }
46
47         private void handlePolicyNotification(String msg) {
48                 log.info("Message received from policy: " +msg);
49                 PciUpdateRepository pciUpdateRepository = BeanUtil.getBean(PciUpdateRepository.class);
50                 Configuration configuration = Configuration.getInstance();
51                 try {
52                         ObjectMapper mapper = new ObjectMapper();
53                         PolicyNotification policyResponse = mapper.readValue(msg, PolicyNotification.class);
54                         String payload = policyResponse.getPayload();
55                         Payload payloadObject = mapper.readValue(payload, Payload.class);
56                         List<Configurations> configurationList = payloadObject.getConfiguration();
57                         for (Configurations config : configurationList) {
58                                 int status = config.getStatus().getCode();
59                                 if (status != 200) {
60                                         String cellId = config.getData().getFapservice().getAlias();
61
62                                         int negativeAckCount = pciUpdateRepository.getNegativeAckCountforCellId(cellId);
63                                         if (negativeAckCount > configuration.getPolicyNegativeAckThreshold()) {
64                                                 long fixedPci = pciUpdateRepository.getOldPciforCellId(cellId);
65                                                  
66                                                 FixedPciCellsRepository fixedPciCellsRepository = BeanUtil
67                                                                 .getBean(FixedPciCellsRepository.class);
68                                                 FixedPciCells fixedPciCells = new FixedPciCells();
69                                                 fixedPciCells.setCellId(cellId);
70                                                 fixedPciCells.setFixedPci(fixedPci);
71                                                 fixedPciCellsRepository.save(fixedPciCells);
72                                                 pciUpdateRepository.deleterecordforCellId(cellId);
73                                         } else {
74
75                                                 pciUpdateRepository.increaseNegativeAckCountforCellId(++negativeAckCount, cellId);
76                                         }
77                                 } else {
78
79                                         String cellId = config.getData().getFapservice().getAlias();
80                                         pciUpdateRepository.deleterecordforCellId(cellId);
81                                 }
82
83                                 String statusToString = Integer.toString(status);
84                                 log.info("Handled response from policy, status code {}", statusToString);
85                         }
86                 } catch (Exception e) {
87                         log.info("caught exception while fetching policy response:" + e);
88
89                 }
90         }
91 }