183de84487619bcb97807f7306dd9d264fe1aeec
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / child / ChildThreadUtils.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.child;
23
24 import com.fasterxml.jackson.annotation.JsonInclude.Include;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
34 import org.onap.dcaegen2.services.sonhms.Configuration;
35 import org.onap.dcaegen2.services.sonhms.dao.SonRequestsRepository;
36 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
37 import org.onap.dcaegen2.services.sonhms.entity.SonRequests;
38 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
39 import org.onap.dcaegen2.services.sonhms.model.CellConfig;
40 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
41 import org.onap.dcaegen2.services.sonhms.model.Common;
42 import org.onap.dcaegen2.services.sonhms.model.Configurations;
43 import org.onap.dcaegen2.services.sonhms.model.Data;
44 import org.onap.dcaegen2.services.sonhms.model.FapService;
45 import org.onap.dcaegen2.services.sonhms.model.Lte;
46 import org.onap.dcaegen2.services.sonhms.model.LteCell;
47 import org.onap.dcaegen2.services.sonhms.model.NeighborListInUse;
48 import org.onap.dcaegen2.services.sonhms.model.Payload;
49 import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
50 import org.onap.dcaegen2.services.sonhms.model.Ran;
51 import org.onap.dcaegen2.services.sonhms.model.X0005b9Lte;
52 import org.onap.dcaegen2.services.sonhms.restclient.AsyncResponseBody;
53 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
54 import org.onap.dcaegen2.services.sonhms.restclient.Solutions;
55 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
56 import org.slf4j.Logger;
57
58 public class ChildThreadUtils {
59
60     private static final Logger log = org.slf4j.LoggerFactory.getLogger(ChildThreadUtils.class);
61     private ConfigPolicy configPolicy;
62     private PnfUtils pnfUtils;
63     private PolicyDmaapClient policyDmaapClient;
64
65     /**
66      * Parameterized constructor.
67      */
68     public ChildThreadUtils(ConfigPolicy configPolicy, PnfUtils pnfUtils, PolicyDmaapClient policyDmaapClient) {
69         this.configPolicy = configPolicy;
70         this.pnfUtils = pnfUtils;
71         this.policyDmaapClient = policyDmaapClient;
72     }
73
74     /**
75      * Save request.
76      */
77     public void saveRequest(String transactionId, long childThreadId) {
78         SonRequestsRepository sonRequestsRepository = BeanUtil.getBean(SonRequestsRepository.class);
79
80         SonRequests sonRequest = new SonRequests();
81         sonRequest.setTransactionId(transactionId);
82         sonRequest.setChildThreadId(childThreadId);
83         sonRequestsRepository.save(sonRequest);
84     }
85
86     /**
87      * Determines whether to trigger Oof or wait for notifications.
88      */
89     public Boolean triggerOrWait(Map<String, ArrayList<Integer>> collisionConfusionResult) {
90         // determine collision or confusion
91
92         Configuration configuration = Configuration.getInstance();
93         int collisionSum = 0;
94         int confusionSum = 0;
95
96         for (Map.Entry<String, ArrayList<Integer>> entry : collisionConfusionResult.entrySet()) {
97
98             ArrayList<Integer> arr;
99             arr = entry.getValue();
100             // check for 0 collision and confusion
101             if (!arr.isEmpty()) {
102                 collisionSum = collisionSum + arr.get(0);
103                 confusionSum = confusionSum + arr.get(1);
104             }
105         }
106         return ((collisionSum >= configuration.getMinCollision()) && (confusionSum >= configuration.getMinConfusion()));
107
108     }
109
110     /**
111      * get policy notification string from oof result.
112      *
113      */
114     public String getNotificationString(String pnfName, String requestId, String payloadString, Long alarmStartTime,
115             String action) {
116
117         String closedLoopControlName = "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459";
118         try {
119             closedLoopControlName = (String) configPolicy.getConfig().get("PCI_MODCONFIG_POLICY_NAME");
120         } catch (NullPointerException e) {
121             log.error("Config policy not found, Using default");
122         }
123
124         PolicyNotification policyNotification = new PolicyNotification(closedLoopControlName, requestId, alarmStartTime,
125                 pnfName, action);
126
127         policyNotification.setClosedLoopControlName(closedLoopControlName);
128         policyNotification.setPayload(payloadString);
129         ObjectMapper mapper = new ObjectMapper();
130         mapper.setSerializationInclusion(Include.NON_NULL);
131
132         String notification = "";
133         try {
134             notification = mapper.writeValueAsString(policyNotification);
135         } catch (JsonProcessingException e1) {
136             log.debug("JSON processing exception: {}", e1);
137         }
138         return notification;
139     }
140
141     /**
142      * Sends Dmaap notification to Policy.
143      *
144      * @throws ConfigDbNotFoundException
145      *             when config db is unreachable
146      */
147     public Boolean sendToPolicy(AsyncResponseBody async) throws ConfigDbNotFoundException {
148
149         if (log.isDebugEnabled()) {
150             log.debug(async.toString());
151         }
152
153         Solutions solutions;
154         solutions = async.getSolutions();
155         if (!solutions.getPciSolutions().isEmpty()) {
156             Map<String, List<CellPciPair>> pnfs = pnfUtils.getPnfs(solutions);
157             for (Map.Entry<String, List<CellPciPair>> entry : pnfs.entrySet()) {
158                 String pnfName = entry.getKey();
159                 List<CellPciPair> cellPciPairs = entry.getValue();
160                 ArrayList<Configurations> configurations = new ArrayList<>();
161                 for (CellPciPair cellPciPair : cellPciPairs) {
162                     String cellId = cellPciPair.getCellId();
163                     int pci = cellPciPair.getPhysicalCellId();
164                     Configurations configuration = new Configurations(new Data(new FapService(cellId,
165                             new X0005b9Lte(pci, pnfName), new CellConfig(new Lte(new Ran(new Common(cellId), null))))),
166                             null);
167                     configurations.add(configuration);
168                 }
169
170                 Payload payload = new Payload(configurations);
171                 ObjectMapper mapper = new ObjectMapper();
172                 mapper.setSerializationInclusion(Include.NON_NULL);
173                 String payloadString = "";
174                 try {
175                     payloadString = mapper.writeValueAsString(payload);
176                 } catch (JsonProcessingException e) {
177                     log.debug("JSON processing exception: {}", e);
178                 }
179
180                 String requestId = UUID.randomUUID().toString();
181
182                 String notification = getNotificationString(pnfName, requestId, payloadString,
183                         System.currentTimeMillis(), "ModifyConfig");
184                 log.info("Policy Notification: {}", notification);
185                 boolean status = policyDmaapClient.sendNotificationToPolicy(notification);
186                 log.debug("sent Message: {}", status);
187                 if (status) {
188                     log.debug("Message sent to policy");
189                 } else {
190                     log.debug("Sending notification to policy failed");
191                 }
192                 policyDmaapClient.handlePolicyResponse(requestId);
193                 log.info("handled policy response in ModifyConfig");
194
195             }
196         }
197         if (!solutions.getAnrSolutions().isEmpty()) {
198             Map<String, List<Map<String, List<String>>>> anrPnfs;
199             List<Configurations> configurations = new ArrayList<>();
200             anrPnfs = pnfUtils.getPnfsForAnrSolutions(solutions.getAnrSolutions());
201             for (Map.Entry<String, List<Map<String, List<String>>>> entry : anrPnfs.entrySet()) {
202                 String pnfName = entry.getKey();
203                 for (Map<String, List<String>> cellRemNeighborsPair : anrPnfs.get(pnfName)) {
204                     for (Map.Entry<String, List<String>> entry1 : cellRemNeighborsPair.entrySet()) {
205                         String cellId = entry1.getKey();
206                         List<LteCell> lteCellList = new ArrayList<>();
207                         for (String removeableNeighbor : entry1.getValue()) {
208                             LteCell lteCell = new LteCell();
209                             lteCell.setBlacklisted("true");
210                             lteCell.setPlmnId(solutions.getNetworkId());
211                             lteCell.setCid(removeableNeighbor);
212                             int pci = SdnrRestClient.getPci(cellId);
213                             lteCell.setPhyCellId(pci);
214                             lteCell.setPnfName(pnfName);
215                             lteCellList.add(lteCell);
216                         }
217                         Configurations configuration = new Configurations(new Data(new FapService(cellId, null,
218                                 new CellConfig(new Lte(new Ran(new Common(cellId), new NeighborListInUse(null,
219                                         lteCellList, String.valueOf(lteCellList.size()))))))),
220                                 null);
221                         configurations.add(configuration);
222                     }
223                 }
224                 Payload payload = new Payload(configurations);
225                 ObjectMapper mapper = new ObjectMapper();
226                 mapper.setSerializationInclusion(Include.NON_NULL);
227                 String payloadString = null;
228                 try {
229                     payloadString = mapper.writeValueAsString(payload);
230                 } catch (JsonProcessingException e) {
231                     log.error("Exception in writing anrupdate string", e);
232                 }
233                 String requestId = UUID.randomUUID().toString();
234                 String notification = getNotificationString(pnfName, requestId, payloadString,
235                         System.currentTimeMillis(), "ModifyConfigANR");
236                 log.info("Policy Notification: {}", notification);
237                 Boolean result = policyDmaapClient.sendNotificationToPolicy(notification);
238                 log.info("send notification to policy result {} ", result);
239                 policyDmaapClient.handlePolicyResponse(requestId);
240                 log.info("handled policy response in ModifyConfigANR");
241
242             }
243
244         }
245         return true;
246     }
247
248 }