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.restclient;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.UUID;
32 import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
33 import org.onap.dcaegen2.services.sonhms.Configuration;
34 import org.onap.dcaegen2.services.sonhms.exceptions.OofNotFoundException;
35 import org.onap.dcaegen2.services.sonhms.model.AnrInput;
36 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.core.ParameterizedTypeReference;
40 import org.springframework.http.ResponseEntity;
42 public class OofRestClient {
43 private static Logger log = LoggerFactory.getLogger(OofRestClient.class);
45 private OofRestClient() {
50 * rest client that pci uses to query the OOF for pci solutions.
52 * @throws OofNotFoundException
53 * when request to oof fails
56 public static String queryOof(int numSolutions, String transactionId, String requestType, List<String> cellIdList,
57 String networkId, List<String> optimizers, List<AnrInput> anrInputList) throws OofNotFoundException {
58 log.debug("inside queryoof");
60 Configuration configuration = Configuration.getInstance();
61 UUID requestUuid = UUID.randomUUID();
62 String requestId = requestUuid.toString();
63 String callbackUrl = configuration.getCallbackUrl();
64 RequestInfo requestInfo = new RequestInfo();
65 requestInfo.setTransactionId(transactionId);
66 requestInfo.setRequestId(requestId);
67 requestInfo.setCallbackUrl(callbackUrl);
68 String sourceId = configuration.getSourceId();
69 requestInfo.setSourceId(sourceId);
70 requestInfo.setRequestType(requestType);
71 requestInfo.setNumSolutions(numSolutions);
72 requestInfo.setOptimizers(optimizers);
73 Map<String, String> callbackHeader = new HashMap<>();
74 callbackHeader.put("Content-Type", "application/json");
75 requestInfo.setCallbackHeader(callbackHeader);
76 ConfigPolicy config = ConfigPolicy.getInstance();
79 timeout = (int) config.getConfig().get("PCI_NEIGHBOR_CHANGE_CLUSTER_TIMEOUT_IN_SECS");
80 } catch (NullPointerException e) {
81 log.debug("No config policy available. Using default timeout 60 sec");
83 requestInfo.setTimeout(timeout);
85 CellInfo cellInfo = new CellInfo();
86 cellInfo.setCellIdList(cellIdList);
87 cellInfo.setNetworkId(networkId);
88 cellInfo.setTrigger("NbrListChange");
89 if (!anrInputList.isEmpty()) {
90 cellInfo.setAnrInputList(anrInputList);
92 OofRequestBody oofRequestBody = new OofRequestBody();
93 oofRequestBody.setRequestInfo(requestInfo);
94 oofRequestBody.setCellInfo(cellInfo);
96 ObjectMapper mapper = new ObjectMapper();
97 String requestBody = "";
99 requestBody = mapper.writeValueAsString(oofRequestBody);
100 } catch (JsonProcessingException e) {
101 log.error("Exception when forming JSON String {}", e);
104 log.info("requestBody{}", requestBody);
106 String requestUrl = configuration.getOofService() + "/api/oof/pci/v1";
107 log.debug("requestUrl {}", requestUrl);
108 ResponseEntity<String> response = null;
109 response = SonHandlerRestTemplate.sendPostRequestToOof(requestUrl, requestBody,
110 new ParameterizedTypeReference<String>() {
112 if (response == null) {
113 throw new OofNotFoundException("Request to oof failed");
115 log.info("response {}", response);
117 return response.getBody();