Drop Nokia from file header copyright line
[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
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.oransc.ric.portal.dashboard.DashboardApplication;
27 import org.oransc.ric.portal.dashboard.DashboardConstants;
28 import org.oransc.ric.portal.dashboard.model.DashboardUser;
29 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
30 import org.oransc.ric.portal.dashboard.model.IDashboardResponse;
31 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.MediaType;
35 import org.springframework.security.access.annotation.Secured;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RestController;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.beans.factory.annotation.Value;
41
42 import io.swagger.annotations.ApiOperation;
43
44 /**
45  * Answers REST requests for admin services like version, health etc.
46  */
47 @RestController
48 @RequestMapping(value = AdminController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
49 public class AdminController {
50
51         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52
53         // Publish paths in constants so tests are easy to write
54         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/admin";
55         public static final String USER_METHOD = "user";
56         public static final String HEALTH_METHOD = "health";
57         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
58         public static final String XAPPMETRICS_METHOD = "metrics";
59
60         private final DashboardUser[] users;
61
62         private static final String ACTIVE = "Active";
63         private static final String INACTIVE = "Inactive";
64
65         @Value("${metrics.url.ac}")
66         private String acAppMetricsUrl;
67
68         @Value("${metrics.url.mc}")
69         private String mcAppMetricsUrl;
70
71         public AdminController() {
72                 // Mock data
73                 users = new DashboardUser[] { //
74                                 new DashboardUser(1, "John", "Doe", ACTIVE), //
75                                 new DashboardUser(2, "Alice", "Nolan", ACTIVE), //
76                                 new DashboardUser(3, "Pierce", "King", INACTIVE), //
77                                 new DashboardUser(4, "Paul", "Smith", INACTIVE), //
78                                 new DashboardUser(5, "Jack", "Reacher", ACTIVE) };
79         }
80
81         @ApiOperation(value = "Gets the Dashboard MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
82         @GetMapping(VERSION_METHOD)
83         // No role required
84         public SuccessTransport getVersion() {
85                 // These endpoints are invoked repeatedly by K8S
86                 logger.trace("getVersion");
87                 return new SuccessTransport(200,
88                                 DashboardApplication.getImplementationVersion(MethodHandles.lookup().lookupClass()));
89         }
90
91         @ApiOperation(value = "Checks the health of the application.", response = SuccessTransport.class)
92         @GetMapping(HEALTH_METHOD)
93         // No role required
94         public SuccessTransport getHealth() {
95                 // These endpoints are invoked repeatedly by K8S
96                 logger.trace("getHealth");
97                 return new SuccessTransport(200, "Dashboard is healthy!");
98         }
99
100         @ApiOperation(value = "Gets the list of application users.", response = DashboardUser.class, responseContainer = "List")
101         @GetMapping(USER_METHOD)
102         @Secured({ DashboardConstants.ROLE_ADMIN })
103         public DashboardUser[] getUsers() {
104                 logger.debug("getUsers");
105                 return users;
106         }
107
108         @ApiOperation(value = "Gets the kibana metrics URL for the specified app.", response = SuccessTransport.class)
109         @GetMapping(XAPPMETRICS_METHOD)
110         public IDashboardResponse getAppMetricsUrl(@RequestParam String app, HttpServletResponse response) {
111                 String metricsUrl = null;
112                 if (DashboardConstants.APP_NAME_AC.equals(app))
113                         metricsUrl = acAppMetricsUrl;
114                 else if (DashboardConstants.APP_NAME_MC.equals(app))
115                         metricsUrl = mcAppMetricsUrl;
116                 logger.debug("getAppMetricsUrl: app {} metricsurl {}", app, metricsUrl);
117                 if (metricsUrl != null)
118                         return new SuccessTransport(200, metricsUrl);
119                 else {
120                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
121                         return new ErrorTransport(400, "Client provided app name is invalid as: " + app);
122                 }
123         }
124 }