DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.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 com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.att.nsa.metrics.CdmMeasuredItem;
27 import org.json.JSONObject;
28 import org.onap.dmaap.dmf.mr.CambriaApiException;
29 import org.onap.dmaap.dmf.mr.backends.MetricsSet;
30 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
31 import org.onap.dmaap.dmf.mr.service.MetricsService;
32 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
33 import org.springframework.stereotype.Component;
34
35 import java.io.IOException;
36
37 /**
38  * 
39  * 
40  * This will provide all the generated metrics details also it can provide the
41  * get metrics details
42  * 
43  * 
44  * @author nilanjana.maity
45  *
46  *
47  */
48 @Component
49 public class MetricsServiceImpl implements MetricsService {
50
51         
52         private static final EELFLogger LOG = EELFManager.getInstance().getLogger(MetricsService.class);
53         /**
54          * 
55          * 
56          * @param ctx
57          * @throws IOException
58          * 
59          * 
60          * get Metric details
61          * 
62          */
63         @Override
64         
65         public void get(DMaaPContext ctx) throws IOException {
66                 LOG.info("Inside  : MetricsServiceImpl : get()");
67                 final MetricsSet metrics = ctx.getConfigReader().getfMetrics();
68                 DMaaPResponseBuilder.setNoCacheHeadings(ctx);
69                 final JSONObject result = metrics.toJson();
70                 DMaaPResponseBuilder.respondOk(ctx, result);
71                 LOG.info("============ Metrics generated : " + result.toString() + "=================");
72
73         }
74
75
76         @Override
77         /**
78          * 
79          * get Metric by name
80          * 
81          * 
82          * @param ctx
83          * @param name
84          * @throws IOException
85          * @throws CambriaApiException
86          * 
87          * 
88          */
89         public void getMetricByName(DMaaPContext ctx, String name) throws IOException, CambriaApiException {
90                 LOG.info("Inside  : MetricsServiceImpl : getMetricByName()");
91                 final MetricsSet metrics = ctx.getConfigReader().getfMetrics();
92
93                 final CdmMeasuredItem item = metrics.getItem(name);
94                 /**
95                  * check if item is null
96                  */
97                 if (item == null) {
98                         throw new CambriaApiException(404, "No metric named [" + name + "].");
99                 }
100
101                 final JSONObject entry = new JSONObject();
102                 entry.put("summary", item.summarize());
103                 entry.put("raw", item.getRawValueString());
104
105                 DMaaPResponseBuilder.setNoCacheHeadings(ctx);
106
107                 final JSONObject result = new JSONObject();
108                 result.put(name, entry);
109
110                 DMaaPResponseBuilder.respondOk(ctx, result);
111                 LOG.info("============ Metrics generated : " + entry.toString() + "=================");
112         }
113
114 }