fix apigateway for mediatorserver routes
[ccsdk/features.git] / sdnr / wt / apigateway / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / apigateway / database / DatabaseHttpClient.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.ccsdk.features.sdnr.wt.apigateway.database;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPClient;
30 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DatabaseHttpClient extends BaseHTTPClient {
35
36         private static Logger LOG = LoggerFactory.getLogger(DatabaseHttpClient.class);
37         private static final String URI = "/mwtn/mediator-server/_search";
38         private final Map<String, String> headers;
39
40         public DatabaseHttpClient(String base, boolean trustAllCerts) {
41                 super(base, trustAllCerts);
42                 this.headers = this.getHeaders();
43         }
44
45         private Map<String, String> getHeaders() {
46                 Map<String, String> h = new HashMap<String, String>();
47
48                 return h;
49         }
50
51         public Map<String, MediatorServerInfo> requestEntries() {
52                 Map<String, MediatorServerInfo> entries = new HashMap<String, MediatorServerInfo>();
53                 BaseHTTPResponse response = null;
54                 try {
55                         response = this.sendRequest(URI, "GET", (String) null, this.headers);
56                 } catch (IOException e) {
57                         LOG.warn("problem reading db entries of mediator server: {}", e.getMessage());
58                 }
59                 if (response != null && response.code == BaseHTTPResponse.CODE200) {
60                         try {
61                                 JSONObject o = new JSONObject(response.body);
62                                 if (o.has("hits")) {
63                                         JSONObject hits = o.getJSONObject("hits");
64                                         if (hits.has("hits")) {
65                                                 JSONArray hitsarray = hits.getJSONArray("hits");
66                                                 if (hitsarray.length() > 0) {
67                                                         for (int i = 0; i < hitsarray.length(); i++) {
68                                                                 JSONObject entry = hitsarray.getJSONObject(i);
69                                                                 entries.put(entry.getString("_id"),
70                                                                                 new MediatorServerInfo(entry.getJSONObject("_source").getString("name"),
71                                                                                                 entry.getJSONObject("_source").getString("url")));
72                                                         }
73                                                 }
74
75                                         }
76                                 }
77                         } catch (Exception e) {
78                                 LOG.warn("problem parsing response: {} | e={}", response, e.getMessage());
79                         }
80                 }
81                 return entries;
82         }
83 }