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;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.dcaegen2.services.sonhms.Configuration;
32 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
33 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
34 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.core.ParameterizedTypeReference;
38 import org.springframework.http.ResponseEntity;
40 public class SdnrRestClient {
42 private static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
43 private static Logger log = LoggerFactory.getLogger(SdnrRestClient.class);
45 private SdnrRestClient() {
50 * Method to get neibhbour list from SDNR.
52 * @throws ConfigDbNotFoundException
53 * when request to configDB fails
55 public static List<CellPciPair> getNbrList(String cellId) throws ConfigDbNotFoundException {
56 Configuration configuration = Configuration.getInstance();
57 String ts = new SimpleDateFormat(DATETIMEFORMAT).format(new Time(System.currentTimeMillis()));
58 String requestUrl = configuration.getConfigDbService() + "/api/sdnc-config-db/v3/getNbrList" + "/" + cellId
60 log.debug("request url: {}", requestUrl);
61 String response = sendRequest(requestUrl);
62 List<CellPciPair> nbrList = new ArrayList<>();
63 JSONObject responseJson = new JSONObject(response);
64 JSONArray nbrListObj = responseJson.getJSONArray("nbrList");
65 for (int i = 0; i < nbrListObj.length(); i++) {
66 JSONObject cellObj = nbrListObj.getJSONObject(i);
67 if (cellObj.getBoolean("ho")) {
68 CellPciPair cell = new CellPciPair(cellObj.getString("targetCellId"), cellObj.getInt("pciValue"));
77 * Method to get PCI from SDNR.
79 * @throws ConfigDbNotFoundException
80 * when request to configDB fails
82 public static int getPci(String cellId) throws ConfigDbNotFoundException {
83 Configuration configuration = Configuration.getInstance();
84 String ts = new SimpleDateFormat(DATETIMEFORMAT).format(new Time(System.currentTimeMillis()));
85 String requestUrl = configuration.getConfigDbService() + "/api/sdnc-config-db/v3/getPCI" + "/" + cellId + "/"
87 String response = sendRequest(requestUrl);
88 JSONObject respObj = new JSONObject(response);
89 return respObj.getInt("value");
93 * Method to get PNF name from SDNR.
95 * @throws ConfigDbNotFoundException
96 * when request to configDB fails
98 public static String getPnfName(String cellId) throws ConfigDbNotFoundException {
99 Configuration configuration = Configuration.getInstance();
100 String ts = new SimpleDateFormat(DATETIMEFORMAT).format(new Time(System.currentTimeMillis()));
101 String requestUrl = configuration.getConfigDbService() + "/api/sdnc-config-db/v3/getPnfId" + "/" + cellId + "/"
103 String response = sendRequest(requestUrl);
104 JSONObject responseObject = new JSONObject(response);
105 return responseObject.getString("value");
109 * Method to send request.
111 private static String sendRequest(String url) throws ConfigDbNotFoundException {
112 ResponseEntity<String> response = SonHandlerRestTemplate.sendGetRequest(url,
113 new ParameterizedTypeReference<String>() {
115 if (response == null) {
116 throw new ConfigDbNotFoundException("Cannot reach Config DB");
118 return response.getBody();