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