1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 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 java.util.ArrayList;
25 import java.util.List;
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.onap.dcaegen2.services.sonhms.Configuration;
30 import org.onap.dcaegen2.services.sonhms.exceptions.CpsNotFoundException;
31 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
32 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.core.ParameterizedTypeReference;
36 import org.springframework.http.ResponseEntity;
39 * A subclass which contains the methods
40 * to get required information from the CPS Client
44 public class CpsClient extends ConfigInterface {
46 private static Logger log = LoggerFactory.getLogger(CpsClient.class);
53 * Method to get neighbour list from CPS.
55 * @throws CpsNotFoundException when request to CPS fails
58 public List<CellPciPair> getNbrList(String cellId) throws CpsNotFoundException {
60 Configuration configuration = Configuration.getInstance();
61 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetNbrListUrl();
62 JSONObject inputparam = new JSONObject();
63 JSONObject reqbody = new JSONObject();
64 inputparam.put("cellId", cellId);
65 reqbody.put("inputParameters", inputparam);
66 log.debug("request url: {}", requestUrl);
67 String response = sendRequest(requestUrl, reqbody);
68 List<CellPciPair> nbrList = new ArrayList<>();
70 JSONArray nbrListObj = new JSONArray(response);
71 for (int i = 0; i < nbrListObj.length(); i++) {
72 JSONObject cellObj = nbrListObj.getJSONObject(i);
73 JSONObject obj = cellObj.getJSONObject("attributes");
74 if (obj.getBoolean("isHOAllowed")) {
75 CellPciPair cell = new CellPciPair(obj.getString("nRTCI"), obj.getInt("nRPCI"));
84 * Method to get PCI from CPS.
86 * @throws CpsNotFoundException when request to CPS fails
89 public int getPci(String cellId) throws CpsNotFoundException {
91 Configuration configuration = Configuration.getInstance();
92 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetPciUrl();
93 JSONObject inputparam = new JSONObject();
94 JSONObject reqbody = new JSONObject();
95 inputparam.put("cellId", cellId);
96 reqbody.put("inputParameters", inputparam);
97 String response = sendRequest(requestUrl, reqbody);
98 JSONObject respObj = new JSONObject(response);
99 return respObj.getInt("value");
103 * Method to get PNF name from CPS.
105 * @throws CpsNotFoundException when request to CPS fails
108 public String getPnfName(String cellId) throws CpsNotFoundException {
109 Configuration configuration = Configuration.getInstance();
110 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetPnfUrl();
111 JSONObject inputparam = new JSONObject();
112 JSONObject reqbody = new JSONObject();
113 inputparam.put("cellId", cellId);
114 reqbody.put("inputParameters", inputparam);
115 String response = sendRequest(requestUrl, reqbody);
116 JSONObject responseObject = new JSONObject(response);
117 return responseObject.getString("value");
121 * Method to get CellData name from CPS.
123 * @throws CpsNotFoundException when request to CPS fails
126 public JSONObject getCellData(String cellId) throws CpsNotFoundException {
128 Configuration configuration = Configuration.getInstance();
129 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetCellDataUrl();
130 JSONObject inputparam = new JSONObject();
131 JSONObject reqbody = new JSONObject();
132 inputparam.put("cellId", cellId);
133 reqbody.put("inputParameters", inputparam);
134 String response = sendRequest(requestUrl, reqbody);
135 JSONObject responseObject = new JSONObject(response);
136 return responseObject;
139 private String sendRequest(String url, JSONObject reqbody) throws CpsNotFoundException {
140 ResponseEntity<String> response = SonHandlerRestTemplate.sendPostRequest(url, reqbody.toString(),
141 new ParameterizedTypeReference<String>() {
143 if (response == null) {
144 throw new CpsNotFoundException("Cannot reach CPS");
146 return response.getBody();