1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021-2022 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 log.info("Response from CPS is : " + response);
69 List<CellPciPair> nbrList = new ArrayList<>();
70 JSONObject responseJson = new JSONObject(response);
71 JSONArray nbrListObj = responseJson.getJSONArray("NRCellRelation");
72 for (int i = 0; i < nbrListObj.length(); i++) {
73 JSONObject cellObj = nbrListObj.getJSONObject(i);
74 JSONObject obj = cellObj.getJSONObject("attributes");
75 if (obj.getBoolean("isHOAllowed")) {
76 CellPciPair cell = new CellPciPair(obj.getString("nRTCI"), obj.getInt("nRPCI"));
85 * Method to get PCI from CPS.
87 * @throws CpsNotFoundException when request to CPS fails
90 public int getPci(String cellId) throws CpsNotFoundException {
92 Configuration configuration = Configuration.getInstance();
93 int responseObject = 0;
94 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetPciUrl();
95 JSONObject inputparam = new JSONObject();
96 JSONObject reqbody = new JSONObject();
97 inputparam.put("cellId", cellId);
98 reqbody.put("inputParameters", inputparam);
99 String response = sendRequest(requestUrl, reqbody);
100 log.info("Response from CPS is : " + response);
101 JSONArray requestArray = new JSONArray(response);
102 for (int i=0;i<requestArray.length();i++) {
103 int pciValue = requestArray.getJSONObject(i).getInt("nRPCI");
104 responseObject = pciValue;
105 log.info("The nRPCI value is : " + responseObject);
107 return responseObject;
111 * Method to get PNF name from CPS.
113 * @throws CpsNotFoundException when request to CPS fails
116 public String getPnfName(String cellId) throws CpsNotFoundException {
117 Configuration configuration = Configuration.getInstance();
118 String responseObject = "";
119 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetPnfUrl();
120 JSONObject inputparam = new JSONObject();
121 JSONObject reqbody = new JSONObject();
122 inputparam.put("cellId", cellId);
123 reqbody.put("inputParameters", inputparam);
124 String response = sendRequest(requestUrl, reqbody);
125 log.info("Response from CPS is : " + response);
126 JSONArray requestArray = new JSONArray(response);
127 for (int i=0;i<requestArray.length();i++) {
128 String pnfName = requestArray.getJSONObject(i).optString("idGNBDUFunction");
129 responseObject = pnfName;
131 return responseObject;
135 * Method to get NearRTRIC ID from CPS.
137 * @throws CpsNotFoundException when request to CPS fails
140 public static String getRicId(String cellId) throws CpsNotFoundException {
141 Configuration configuration = Configuration.getInstance();
142 String responseObject = "";
143 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetRicIdUrl();
144 JSONObject inputparam = new JSONObject();
145 JSONObject reqbody = new JSONObject();
146 inputparam.put("cellId", cellId);
147 reqbody.put("inputParameters", inputparam);
148 String response = sendRequest(requestUrl, reqbody);
149 log.info("Response from CPS is : " + response);
150 JSONArray requestArray = new JSONArray(response);
151 for (int i=0;i<requestArray.length();i++) {
152 String ricId = requestArray.getJSONObject(i).optString("idNearRTRIC");
153 responseObject = ricId;
155 return responseObject;
159 * Method to get CellData name from CPS.
161 * @throws CpsNotFoundException when request to CPS fails
164 public JSONObject getCellData(String cellId) throws CpsNotFoundException {
166 Configuration configuration = Configuration.getInstance();
167 String requestUrl = configuration.getCpsServiceUrl() + "/" + configuration.getGetCellDataUrl();
168 JSONObject inputparam = new JSONObject();
169 JSONObject reqbody = new JSONObject();
170 inputparam.put("cellId", cellId);
171 reqbody.put("inputParameters", inputparam);
172 String response = sendRequest(requestUrl, reqbody);
173 log.info("Response from CPS is : " + response);
174 JSONObject responseObject = new JSONObject(response);
175 return responseObject;
178 private static String sendRequest(String url, JSONObject reqbody) throws CpsNotFoundException {
179 ResponseEntity<String> response = SonHandlerRestTemplate.sendPostRequest(url, reqbody.toString(),
180 new ParameterizedTypeReference<String>() {
182 if (response == null) {
183 throw new CpsNotFoundException("Cannot reach CPS");
185 return response.getBody();