Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / restclient / SdnrRestClient.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 java.sql.Time;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.List;
28
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;
39
40 public class SdnrRestClient {
41
42     private static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
43     private static Logger log = LoggerFactory.getLogger(SdnrRestClient.class);
44
45     private SdnrRestClient() {
46
47     }
48
49     /**
50      * Method to get neibhbour list from SDNR.
51      *
52      * @throws ConfigDbNotFoundException
53      *             when request to configDB fails
54      */
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
59                 + "/" + ts;
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"));
69                 nbrList.add(cell);
70             }
71         }
72
73         return nbrList;
74     }
75
76     /**
77      * Method to get PCI from SDNR.
78      *
79      * @throws ConfigDbNotFoundException
80      *             when request to configDB fails
81      */
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 + "/"
86                 + ts;
87         String response = sendRequest(requestUrl);
88         JSONObject respObj = new JSONObject(response);
89         return respObj.getInt("value");
90     }
91
92     /**
93      * Method to get PNF name from SDNR.
94      *
95      * @throws ConfigDbNotFoundException
96      *             when request to configDB fails
97      */
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 + "/"
102                 + ts;
103         String response = sendRequest(requestUrl);
104         JSONObject responseObject = new JSONObject(response);
105         return responseObject.getString("value");
106     }
107
108     /**
109      * Method to send request.
110      */
111     private static String sendRequest(String url) throws ConfigDbNotFoundException {
112         ResponseEntity<String> response = SonHandlerRestTemplate.sendGetRequest(url,
113                 new ParameterizedTypeReference<String>() {
114                 });
115         if (response == null) {
116             throw new ConfigDbNotFoundException("Cannot reach Config DB");
117         }
118         return response.getBody();
119     }
120
121 }