8ff58f36cee0bde68c4fe9faeafbfb08099203bc
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / service / MetricsRestService.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 com.att.nsa.dmaap.service;
23
24 import java.io.IOException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33
34 import org.apache.http.HttpStatus;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Qualifier;
39 import org.springframework.stereotype.Component;
40
41 import org.onap.dmaap.dmf.mr.CambriaApiException;
42 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
43 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
44 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
45 import org.onap.dmaap.dmf.mr.service.MetricsService;
46 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
47
48 /**
49  * This class is a CXF REST service which acts 
50  * as gateway for MR Metrics Service.
51  * @author rajashree.khare
52  *
53  */
54 @Component
55 @Path("/")
56 public class MetricsRestService {
57
58         /**
59          * Logger obj
60          */
61         //private Logger log = Logger.getLogger(MetricsRestService.class.toString());
62         private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigurationReader.class);
63         /**
64          * HttpServletRequest obj
65          */
66         @Context
67         private HttpServletRequest request;
68
69         /**
70          * HttpServletResponse obj
71          */
72         @Context
73         private HttpServletResponse response;
74
75         /**
76          * Config Reader
77          */
78         @Autowired
79         @Qualifier("configurationReader")
80         private ConfigurationReader configReader;
81
82         /**
83          * MetricsService obj
84          */
85         @Autowired
86         private MetricsService metricsService;
87
88         /**
89          * Get Metrics method
90          * @throws CambriaApiException ex
91          */
92         @GET
93         @Produces("text/plain")
94         public void getMetrics() throws CambriaApiException {
95                 try {
96                         log.info("MetricsRestService: getMetrics : START");
97                         metricsService.get(getDmaapContext());
98                         log.info("MetricsRestService: getMetrics : Completed");
99                 } catch (IOException e) {
100                         log.error("Error while fetching metrics data : ", e);
101                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_FORBIDDEN, 
102                                         DMaaPResponseCode.GET_METRICS_ERROR.getResponseCode(), 
103                                         "Error while fetching metrics data"+ e.getMessage());
104                         log.info(errRes.toString());
105                         throw new CambriaApiException(errRes);
106                 }
107         }
108
109         /**
110          * This method is for get the metrics details by the metrics name
111          * 
112          * @param metricName
113          * @throws CambriaApiException 
114          */
115         @GET
116         @Path("/{metricName}")
117         @Produces("text/plain")
118         public void getMetricsByName(@PathParam("metricName") String metricName) 
119                         throws CambriaApiException {
120
121                 try {
122                         log.info("MetricsProducer: getMetricsByName : START");
123                         metricsService.getMetricByName(getDmaapContext(), metricName);
124                         log.info("MetricsRestService: getMetricsByName : Completed");
125                 } catch (IOException | CambriaApiException e) {
126                         log.error("Error while fetching metrics data : ", e);
127                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
128                                         DMaaPResponseCode.GET_METRICS_ERROR.getResponseCode(), 
129                                         "Error while fetching metrics data"+ e.getMessage());
130                         log.info(errRes.toString());
131                         throw new CambriaApiException(errRes);
132                 }
133         }
134
135         /**
136          * This method is used for taking Configuration Object,HttpServletRequest
137          * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
138          * Object.
139          * 
140          * @return DMaaPContext object from where user can get Configuration
141          *         Object,HttpServlet Object
142          * 
143          */
144         private DMaaPContext getDmaapContext() {
145                 DMaaPContext dmaapContext = new DMaaPContext();
146                 dmaapContext.setConfigReader(configReader);
147                 dmaapContext.setRequest(request);
148                 dmaapContext.setResponse(response);
149                 return dmaapContext;
150         }
151
152 }