2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.msb.wrapper;
\r
19 import java.io.IOException;
\r
21 import org.apache.http.ParseException;
\r
22 import org.apache.http.client.methods.CloseableHttpResponse;
\r
23 import org.apache.http.client.methods.HttpGet;
\r
24 import org.apache.http.impl.client.CloseableHttpClient;
\r
25 import org.apache.http.impl.client.HttpClients;
\r
26 import org.apache.http.util.EntityUtils;
\r
27 import org.openo.msb.api.MetricsInfo;
\r
28 import org.openo.msb.wrapper.util.MetricsUtil;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
\r
33 import com.fasterxml.jackson.annotation.PropertyAccessor;
\r
34 import com.fasterxml.jackson.databind.DeserializationFeature;
\r
35 import com.fasterxml.jackson.databind.ObjectMapper;
\r
37 public class MetricsServiceWrapper {
\r
39 private static final Logger LOGGER = LoggerFactory
\r
40 .getLogger(MetricsServiceWrapper.class);
\r
42 public static MetricsInfo getMetricsInfo() {
\r
44 String metricsUrl = MetricsUtil.adminContextPath;
\r
45 String metricsJson = sendGetRequest(metricsUrl);
\r
47 metricsJson = metricsJson.replace("E_", "");//.replaceAll("(?![0-9])(\\.)(?![0-9])", "_").replace("-", "_")
\r
48 ObjectMapper mapper = new ObjectMapper();
\r
49 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
\r
50 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
\r
52 MetricsInfo metricsInfo = new MetricsInfo();
\r
54 metricsInfo = mapper.readValue(metricsJson, MetricsInfo.class);
\r
55 } catch (Exception e) {
\r
56 // TODO Auto-generated catch block
\r
57 LOGGER.error("Jackson readValue to metricsInfo throw exception", e);
\r
63 public static String sendGetRequest(String url) {
\r
64 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
65 HttpGet httpGet = new HttpGet(url);
\r
68 CloseableHttpResponse res = httpClient.execute(httpGet);
\r
70 if (res.getStatusLine().getStatusCode() == MetricsUtil.SC_OK) {
\r
71 return EntityUtils.toString(res.getEntity());
\r
76 } catch (ParseException e) {
\r
77 LOGGER.error("HttpClient throw ParseException:" + url, e);
\r
78 } catch (IOException e) {
\r
79 LOGGER.error("HttpClient throw IOException:" + url, e);
\r
84 } catch (IOException e) {
\r
85 // TODO Auto-generated catch block
\r
86 LOGGER.error("HttpClient Close throw IOException", e);
\r