Implement adaptive SON functionality
[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-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.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.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;
40
41 public class OofRestClient {
42     private static Logger log = LoggerFactory.getLogger(OofRestClient.class);
43
44     private OofRestClient() {
45
46     }
47
48     /**
49      * rest client that pci uses to query the OOF for pci solutions.
50      * 
51      * @throws OofNotFoundException
52      *             when request to oof fails
53      */
54
55     public static String queryOof(int numSolutions, String transactionId, String requestType, List<String> cellIdList,
56             String networkId, List<String> optimizers, List<AnrInput> anrInputList, List<String> fixedPciCells) throws OofNotFoundException {
57         log.debug("inside queryoof");
58
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);
75         int timeout = 60;
76         requestInfo.setTimeout(timeout);
77
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);
84         }
85         if(!fixedPciCells.isEmpty()) {
86         cellInfo.setFixedPCICells(fixedPciCells); 
87         }
88         OofRequestBody oofRequestBody = new OofRequestBody();
89         oofRequestBody.setRequestInfo(requestInfo);
90         oofRequestBody.setCellInfo(cellInfo);
91
92         ObjectMapper mapper = new ObjectMapper();
93         String requestBody = "";
94         try {
95             requestBody = mapper.writeValueAsString(oofRequestBody);
96         } catch (JsonProcessingException e) {
97             log.error("Exception when forming JSON String {}", e);
98
99         }
100         log.info("requestBody{}", requestBody);
101
102         String requestUrl = configuration.getOofService() + configuration.getOofEndpoint();
103         log.info("requestUrl {}", requestUrl);
104         ResponseEntity<String> response = null; 
105         response = SonHandlerRestTemplate.sendPostRequestToOof(requestUrl, requestBody,
106                 new ParameterizedTypeReference<String>() {
107                 });
108         
109         if (response == null) {
110             throw new OofNotFoundException("Request to oof failed");
111         }
112         else if (response.getStatusCodeValue() != 202) {
113             throw new OofNotFoundException("Request to oof failed with status code" + response.getStatusCodeValue());
114         }
115         log.info("response {}", response);
116
117         return response.getBody();
118     }
119 }