bb3700ed414f301e8ff3329a5f18dfd8f6eba933
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / AdminController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.onap.portalsdk.core.restful.domain.EcompUser;
28 import org.oransc.ric.portal.dashboard.DashboardApplication;
29 import org.oransc.ric.portal.dashboard.DashboardConstants;
30 import org.oransc.ric.portal.dashboard.DashboardUserManager;
31 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
32 import org.oransc.ric.portal.dashboard.model.IDashboardResponse;
33 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.http.MediaType;
39 import org.springframework.security.access.annotation.Secured;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RequestParam;
43 import org.springframework.web.bind.annotation.RestController;
44
45 import io.swagger.annotations.ApiOperation;
46
47 /**
48  * Answers REST requests for admin services like version, health etc.
49  */
50 @RestController
51 @RequestMapping(value = AdminController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
52 public class AdminController {
53
54         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55
56         // Publish paths in constants so tests are easy to write
57         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/admin";
58         public static final String USER_METHOD = "user";
59         public static final String HEALTH_METHOD = "health";
60         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
61         public static final String XAPPMETRICS_METHOD = "metrics";
62
63         @Value("${metrics.url.ac}")
64         private String acAppMetricsUrl;
65
66         @Value("${metrics.url.mc}")
67         private String mcAppMetricsUrl;
68
69         @Autowired
70         private DashboardUserManager dashboardUserManager;
71
72         @ApiOperation(value = "Gets the Dashboard MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
73         @GetMapping(VERSION_METHOD)
74         // No role required
75         public SuccessTransport getVersion() {
76                 // These endpoints are invoked repeatedly by K8S
77                 logger.trace("getVersion");
78                 return new SuccessTransport(200,
79                                 DashboardApplication.getImplementationVersion(MethodHandles.lookup().lookupClass()));
80         }
81
82         @ApiOperation(value = "Checks the health of the application.", response = SuccessTransport.class)
83         @GetMapping(HEALTH_METHOD)
84         // No role required
85         public SuccessTransport getHealth() {
86                 // These endpoints are invoked repeatedly by K8S
87                 logger.trace("getHealth");
88                 return new SuccessTransport(200, "Dashboard is healthy!");
89         }
90
91         @ApiOperation(value = "Gets the list of application users.", response = EcompUser.class, responseContainer = "List")
92         @GetMapping(USER_METHOD)
93         @Secured({ DashboardConstants.ROLE_ADMIN }) // regular users should not see this
94         public List<EcompUser> getUsers() {
95                 logger.debug("getUsers");
96                 return dashboardUserManager.getUsers();
97         }
98
99         @ApiOperation(value = "Gets the kibana metrics URL for the specified app.", response = SuccessTransport.class)
100         @GetMapping(XAPPMETRICS_METHOD)
101         // No role required
102         public IDashboardResponse getAppMetricsUrl(@RequestParam String app, HttpServletResponse response) {
103                 String metricsUrl = null;
104                 if (DashboardConstants.APP_NAME_AC.equals(app))
105                         metricsUrl = acAppMetricsUrl;
106                 else if (DashboardConstants.APP_NAME_MC.equals(app))
107                         metricsUrl = mcAppMetricsUrl;
108                 logger.debug("getAppMetricsUrl: app {} metricsurl {}", app, metricsUrl);
109                 if (metricsUrl != null)
110                         return new SuccessTransport(200, metricsUrl);
111                 else {
112                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
113                         return new ErrorTransport(400, "Client provided app name is invalid as: " + app);
114                 }
115         }
116
117 }