Require RIC instance key in controller methods
[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.RicInstanceList;
34 import org.oransc.ric.portal.dashboard.model.RicInstanceKeyName;
35 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.http.MediaType;
41 import org.springframework.security.access.annotation.Secured;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestParam;
45 import org.springframework.web.bind.annotation.RestController;
46
47 import io.swagger.annotations.ApiOperation;
48
49 /**
50  * Answers REST requests for admin services like version, health etc.
51  */
52 @RestController
53 @RequestMapping(value = AdminController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
54 public class AdminController {
55
56         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58         // Publish paths in constants so tests are easy to write
59         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/admin";
60         public static final String HEALTH_METHOD = "health";
61         public static final String INSTANCE_METHOD = "instance";
62         public static final String USER_METHOD = "user";
63         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
64         public static final String XAPPMETRICS_METHOD = "metrics";
65
66         @Value("${metrics.url.ac}")
67         private String acAppMetricsUrl;
68
69         @Value("${metrics.url.mc}")
70         private String mcAppMetricsUrl;
71
72         @Autowired
73         private DashboardUserManager dashboardUserManager;
74
75         @Autowired
76         private RicInstanceList instanceConfig;
77
78         @ApiOperation(value = "Gets the Dashboard MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
79         @GetMapping(VERSION_METHOD)
80         // No role required
81         public SuccessTransport getVersion() {
82                 // These endpoints are invoked repeatedly by K8S
83                 logger.trace("getVersion");
84                 return new SuccessTransport(200,
85                                 DashboardApplication.getImplementationVersion(MethodHandles.lookup().lookupClass()));
86         }
87
88         @ApiOperation(value = "Checks the health of the application.", response = SuccessTransport.class)
89         @GetMapping(HEALTH_METHOD)
90         // No role required
91         public SuccessTransport getHealth() {
92                 // These endpoints are invoked repeatedly by K8S
93                 logger.trace("getHealth");
94                 return new SuccessTransport(200, "Dashboard is healthy!");
95         }
96
97         @ApiOperation(value = "Gets the list of application users.", response = EcompUser.class, responseContainer = "List")
98         @GetMapping(USER_METHOD)
99         @Secured({ DashboardConstants.ROLE_ADMIN }) // regular users should not see this
100         public List<EcompUser> getUsers() {
101                 logger.debug("getUsers");
102                 return dashboardUserManager.getUsers();
103         }
104
105         @ApiOperation(value = "Gets the list of RIC instances.", response = RicInstanceKeyName.class, responseContainer = "List")
106         @GetMapping(INSTANCE_METHOD)
107         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
108         public List<RicInstanceKeyName> getInstances() {
109                 logger.debug("getInstances");
110                 return instanceConfig.getKeyNameList();
111         }
112
113         @ApiOperation(value = "Gets the kibana metrics URL for the specified app.", response = SuccessTransport.class)
114         @GetMapping(XAPPMETRICS_METHOD)
115         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
116         public IDashboardResponse getAppMetricsUrl(@RequestParam String app, HttpServletResponse response) {
117                 String metricsUrl = null;
118                 if (DashboardConstants.APP_NAME_AC.equals(app))
119                         metricsUrl = acAppMetricsUrl;
120                 else if (DashboardConstants.APP_NAME_MC.equals(app))
121                         metricsUrl = mcAppMetricsUrl;
122                 logger.debug("getAppMetricsUrl: app {} metricsurl {}", app, metricsUrl);
123                 if (metricsUrl != null)
124                         return new SuccessTransport(200, metricsUrl);
125                 else {
126                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
127                         return new ErrorTransport(400, "Client provided app name is invalid as: " + app);
128                 }
129         }
130
131 }