Modify ANR Payload aligned to A1 schema in SDNR
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / restclient / CpsClient.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
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.util.ArrayList;
25 import java.util.List;
26
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;
37
38 /**
39  *  A subclass which contains the methods
40  *  to get required information from the CPS Client
41  *
42  */
43
44 public class CpsClient extends ConfigInterface {
45
46     private static Logger log = LoggerFactory.getLogger(CpsClient.class);
47
48     public CpsClient() {
49
50     }
51
52     /**
53      * Method to get neighbour list from CPS.
54      *
55      * @throws CpsNotFoundException when request to CPS fails
56      */
57     @Override
58     public List<CellPciPair> getNbrList(String cellId) throws CpsNotFoundException {
59
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"));
77                 nbrList.add(cell);
78             }
79         }
80
81         return nbrList;
82     }
83
84     /**
85      * Method to get PCI from CPS.
86      *
87      * @throws CpsNotFoundException when request to CPS fails
88      */
89     @Override
90     public int getPci(String cellId) throws CpsNotFoundException {
91
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);
106         }
107         return responseObject;
108     }
109
110     /**
111      * Method to get PNF name from CPS.
112      *
113      * @throws CpsNotFoundException when request to CPS fails
114      */
115     @Override
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;
130         }
131         return responseObject;
132     }
133
134     /*
135      * Method to get NearRTRIC ID from CPS.
136      *
137      * @throws CpsNotFoundException when request to CPS fails
138      */
139
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;
154         }
155         return responseObject;
156     }
157
158     /**
159      * Method to get CellData name from CPS.
160      *
161      * @throws CpsNotFoundException when request to CPS fails
162      */
163     @Override
164     public JSONObject getCellData(String cellId) throws CpsNotFoundException {
165
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;
176     }
177
178     private static String sendRequest(String url, JSONObject reqbody) throws CpsNotFoundException {
179         ResponseEntity<String> response = SonHandlerRestTemplate.sendPostRequest(url, reqbody.toString(),
180                 new ParameterizedTypeReference<String>() {
181                 });
182         if (response == null) {
183             throw new CpsNotFoundException("Cannot reach CPS");
184         }
185         return response.getBody();
186     }
187
188 }