Fix bug in filtering new FM notification
[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 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         List<CellPciPair> nbrList = new ArrayList<>();
69
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"));
76                 nbrList.add(cell);
77             }
78         }
79
80         return nbrList;
81     }
82
83     /**
84      * Method to get PCI from CPS.
85      *
86      * @throws CpsNotFoundException when request to CPS fails
87      */
88     @Override
89     public int getPci(String cellId) throws CpsNotFoundException {
90
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");
100     }
101
102     /**
103      * Method to get PNF name from CPS.
104      *
105      * @throws CpsNotFoundException when request to CPS fails
106      */
107     @Override
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");
118     }
119
120     /**
121      * Method to get CellData name from CPS.
122      *
123      * @throws CpsNotFoundException when request to CPS fails
124      */
125     @Override
126     public JSONObject getCellData(String cellId) throws CpsNotFoundException {
127
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;
137     }
138
139     private String sendRequest(String url, JSONObject reqbody) throws CpsNotFoundException {
140         ResponseEntity<String> response = SonHandlerRestTemplate.sendPostRequest(url, reqbody.toString(),
141                 new ParameterizedTypeReference<String>() {
142                 });
143         if (response == null) {
144             throw new CpsNotFoundException("Cannot reach CPS");
145         }
146         return response.getBody();
147     }
148
149 }