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.Configuration;
33 import org.onap.dcaegen2.services.sonhms.exceptions.OofNotFoundException;
34 import org.onap.dcaegen2.services.sonhms.model.AnrInput;
35 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.core.ParameterizedTypeReference;
39 import org.springframework.http.ResponseEntity;
41 public class OofRestClient {
42 private static Logger log = LoggerFactory.getLogger(OofRestClient.class);
44 private OofRestClient() {
49 * rest client that pci uses to query the OOF for pci solutions.
51 * @throws OofNotFoundException
52 * when request to oof fails
55 public static String queryOof(int numSolutions, String transactionId, String requestType, List<String> cellIdList,
56 String networkId, List<String> optimizers, List<AnrInput> anrInputList) throws OofNotFoundException {
57 log.debug("inside queryoof");
59 Configuration configuration = Configuration.getInstance();
60 UUID requestUuid = UUID.randomUUID();
61 String requestId = requestUuid.toString();
62 String callbackUrl = configuration.getCallbackUrl();
63 RequestInfo requestInfo = new RequestInfo();
64 requestInfo.setTransactionId(transactionId);
65 requestInfo.setRequestId(requestId);
66 requestInfo.setCallbackUrl(callbackUrl);
67 String sourceId = configuration.getSourceId();
68 requestInfo.setSourceId(sourceId);
69 requestInfo.setRequestType(requestType);
70 requestInfo.setNumSolutions(numSolutions);
71 requestInfo.setOptimizers(optimizers);
72 Map<String, String> callbackHeader = new HashMap<>();
73 callbackHeader.put("Content-Type", "application/json");
74 requestInfo.setCallbackHeader(callbackHeader);
76 requestInfo.setTimeout(timeout);
78 CellInfo cellInfo = new CellInfo();
79 cellInfo.setCellIdList(cellIdList);
80 cellInfo.setNetworkId(networkId);
81 cellInfo.setTrigger("NbrListChange");
82 if (!anrInputList.isEmpty()) {
83 cellInfo.setAnrInputList(anrInputList);
85 OofRequestBody oofRequestBody = new OofRequestBody();
86 oofRequestBody.setRequestInfo(requestInfo);
87 oofRequestBody.setCellInfo(cellInfo);
89 ObjectMapper mapper = new ObjectMapper();
90 String requestBody = "";
92 requestBody = mapper.writeValueAsString(oofRequestBody);
93 } catch (JsonProcessingException e) {
94 log.error("Exception when forming JSON String {}", e);
97 log.info("requestBody{}", requestBody);
99 String requestUrl = configuration.getOofService() + configuration.getOofEndpoint();
100 log.info("requestUrl {}", requestUrl);
101 ResponseEntity<String> response = null;
102 response = SonHandlerRestTemplate.sendPostRequestToOof(requestUrl, requestBody,
103 new ParameterizedTypeReference<String>() {
106 if (response == null) {
107 throw new OofNotFoundException("Request to oof failed");
109 else if (response.getStatusCodeValue() != 202) {
110 throw new OofNotFoundException("Request to oof failed with status code" + response.getStatusCodeValue());
112 log.info("response {}", response);
114 return response.getBody();