bump the version
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / dmf / mr / service / impl / MetricsServiceImpl.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.service.impl;
23
24 import java.io.IOException;
25
26 import org.json.JSONObject;
27 import org.springframework.stereotype.Component;
28
29 import org.onap.dmaap.dmf.mr.CambriaApiException;
30 import org.onap.dmaap.dmf.mr.backends.MetricsSet;
31 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
32 import org.onap.dmaap.dmf.mr.service.MetricsService;
33 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.att.nsa.metrics.CdmMeasuredItem;
37
38 /**
39  * 
40  * 
41  * This will provide all the generated metrics details also it can provide the
42  * get metrics details
43  * 
44  * 
45  * @author nilanjana.maity
46  *
47  *
48  */
49 @Component
50 public class MetricsServiceImpl implements MetricsService {
51
52         
53         private static final EELFLogger LOG = EELFManager.getInstance().getLogger(MetricsService.class);
54         /**
55          * 
56          * 
57          * @param ctx
58          * @throws IOException
59          * 
60          * 
61          * get Metric details
62          * 
63          */
64         @Override
65         
66         public void get(DMaaPContext ctx) throws IOException {
67                 LOG.info("Inside  : MetricsServiceImpl : get()");
68                 final MetricsSet metrics = ctx.getConfigReader().getfMetrics();
69                 DMaaPResponseBuilder.setNoCacheHeadings(ctx);
70                 final JSONObject result = metrics.toJson();
71                 DMaaPResponseBuilder.respondOk(ctx, result);
72                 LOG.info("============ Metrics generated : " + result.toString() + "=================");
73
74         }
75
76
77         @Override
78         /**
79          * 
80          * get Metric by name
81          * 
82          * 
83          * @param ctx
84          * @param name
85          * @throws IOException
86          * @throws CambriaApiException
87          * 
88          * 
89          */
90         public void getMetricByName(DMaaPContext ctx, String name) throws IOException, CambriaApiException {
91                 LOG.info("Inside  : MetricsServiceImpl : getMetricByName()");
92                 final MetricsSet metrics = ctx.getConfigReader().getfMetrics();
93
94                 final CdmMeasuredItem item = metrics.getItem(name);
95                 /**
96                  * check if item is null
97                  */
98                 if (item == null) {
99                         throw new CambriaApiException(404, "No metric named [" + name + "].");
100                 }
101
102                 final JSONObject entry = new JSONObject();
103                 entry.put("summary", item.summarize());
104                 entry.put("raw", item.getRawValueString());
105
106                 DMaaPResponseBuilder.setNoCacheHeadings(ctx);
107
108                 final JSONObject result = new JSONObject();
109                 result.put(name, entry);
110
111                 DMaaPResponseBuilder.respondOk(ctx, result);
112                 LOG.info("============ Metrics generated : " + entry.toString() + "=================");
113         }
114
115 }