Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / restclient / OofRestClient.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.restclient;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31
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;
41
42 public class OofRestClient {
43     private static Logger log = LoggerFactory.getLogger(OofRestClient.class);
44
45     private OofRestClient() {
46
47     }
48
49     /**
50      * rest client that pci uses to query the OOF for pci solutions.
51      * 
52      * @throws OofNotFoundException
53      *             when request to oof fails
54      */
55
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");
59
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();
77         int timeout = 60;
78         try {
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");
82         }
83         requestInfo.setTimeout(timeout);
84
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);
91         }
92         OofRequestBody oofRequestBody = new OofRequestBody();
93         oofRequestBody.setRequestInfo(requestInfo);
94         oofRequestBody.setCellInfo(cellInfo);
95
96         ObjectMapper mapper = new ObjectMapper();
97         String requestBody = "";
98         try {
99             requestBody = mapper.writeValueAsString(oofRequestBody);
100         } catch (JsonProcessingException e) {
101             log.error("Exception when forming JSON String {}", e);
102
103         }
104         log.info("requestBody{}", requestBody);
105
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>() {
111                 });
112         if (response == null) {
113             throw new OofNotFoundException("Request to oof failed");
114         }
115         log.info("response {}", response);
116
117         return response.getBody();
118     }
119 }