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