Require RIC instance key in controller methods
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / E2ManagerController.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.ArrayList;
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.oransc.ric.e2mgr.client.api.HealthCheckApi;
29 import org.oransc.ric.e2mgr.client.api.NodebApi;
30 import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
31 import org.oransc.ric.e2mgr.client.model.NodebIdentity;
32 import org.oransc.ric.e2mgr.client.model.ResetRequest;
33 import org.oransc.ric.e2mgr.client.model.SetupRequest;
34 import org.oransc.ric.portal.dashboard.DashboardApplication;
35 import org.oransc.ric.portal.dashboard.DashboardConstants;
36 import org.oransc.ric.portal.dashboard.config.E2ManagerApiBuilder;
37 import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
38 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.http.MediaType;
44 import org.springframework.security.access.annotation.Secured;
45 import org.springframework.util.Assert;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.PostMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RestController;
53 import org.springframework.web.client.HttpStatusCodeException;
54
55 import io.swagger.annotations.ApiOperation;
56
57 /**
58  * Proxies calls from the front end to the E2 Manager API.
59  * 
60  * If a method throws RestClientResponseException, it is handled by
61  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
62  * which returns status 502. All other exceptions are handled by Spring which
63  * returns status 500.
64  */
65 @Configuration
66 @RestController
67 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
68 public class E2ManagerController {
69
70         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
71
72         // Publish paths in constants for tests
73         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
74         // Dashboard only
75         public static final String HEALTH_METHOD = "health";
76         // Keep these consistent with the E2M implementation
77         /* package */ static final String NODEB_PREFIX = "nodeb";
78         public static final String RAN_METHOD = NODEB_PREFIX + "/ran";
79         public static final String NODEB_SHUTDOWN_METHOD = NODEB_PREFIX + "/shutdown";
80         public static final String NODEB_LIST_METHOD = NODEB_PREFIX + "/ids";
81         public static final String ENDC_SETUP_METHOD = NODEB_PREFIX + "/endc-setup";
82         public static final String X2_SETUP_METHOD = NODEB_PREFIX + "/x2-setup";
83         // Reset uses prefix, adds a path parameter below
84         public static final String RESET_METHOD = "reset";
85         // Path parameters
86         private static final String PP_RANNAME = "ranName";
87
88         // Populated by the autowired constructor
89         private final E2ManagerApiBuilder e2ManagerApiBuilder;
90
91         @Autowired
92         public E2ManagerController(final E2ManagerApiBuilder e2ManagerApiBuilder) {
93                 Assert.notNull(e2ManagerApiBuilder, "builder must not be null");
94                 this.e2ManagerApiBuilder = e2ManagerApiBuilder;
95                 if (logger.isDebugEnabled())
96                         logger.debug("ctor: configured with builder type {}", e2ManagerApiBuilder.getClass().getName());
97         }
98
99         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
100         @GetMapping(DashboardConstants.VERSION_METHOD)
101         // No role required
102         public SuccessTransport getClientVersion() {
103                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
104         }
105
106         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
107         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + HEALTH_METHOD)
108         // No role required
109         public void healthGet(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
110                         HttpServletResponse response) {
111                 logger.debug("healthGet instance {}", instanceKey);
112                 HealthCheckApi api = e2ManagerApiBuilder.getHealthCheckApi(instanceKey);
113                 api.healthGet();
114                 response.setStatus(api.getApiClient().getStatusCode().value());
115         }
116
117         // This calls other methods to simplify the task of the front-end.
118         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
119         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + RAN_METHOD)
120         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
121         public List<RanDetailsTransport> getRanDetails(
122                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
123                 logger.debug("getRanDetails instance {}", instanceKey);
124                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
125                 List<NodebIdentity> nodebIdList = api.getNodebIdList();
126                 logger.debug("getRanDetails: nodebIdList {}", nodebIdList);
127                 List<RanDetailsTransport> details = new ArrayList<>();
128                 for (NodebIdentity nbid : nodebIdList) {
129                         GetNodebResponse nbResp = null;
130                         try {
131                                 // Catch exceptions to keep looping despite failures
132                                 nbResp = api.getNb(nbid.getInventoryName());
133                         } catch (HttpStatusCodeException ex) {
134                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
135                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
136                                                 .ranName(nbid.getInventoryName());
137                         }
138                         details.add(new RanDetailsTransport(nbid, nbResp));
139                 }
140                 return details;
141         }
142
143         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
144         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
145                         + NODEB_LIST_METHOD)
146         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
147         public List<NodebIdentity> getNodebIdList(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
148                 logger.debug("getNodebIdList instance {}", instanceKey);
149                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNodebIdList();
150         }
151
152         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
153         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
154                         + "/{" + PP_RANNAME + "}")
155         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
156         public GetNodebResponse getNb(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
157                         @PathVariable(PP_RANNAME) String ranName) {
158                 logger.debug("getNb instance {} name {}", instanceKey, ranName);
159                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNb(ranName);
160         }
161
162         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
163         @PostMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
164                         + ENDC_SETUP_METHOD)
165         @Secured({ DashboardConstants.ROLE_ADMIN })
166         public void endcSetup(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
167                         @RequestBody SetupRequest setupRequest, HttpServletResponse response) {
168                 logger.debug("endcSetup instance {} request {}", instanceKey, setupRequest);
169                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
170                 api.endcSetup(setupRequest);
171                 response.setStatus(api.getApiClient().getStatusCode().value());
172         }
173
174         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
175         @PostMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
176                         + X2_SETUP_METHOD)
177         @Secured({ DashboardConstants.ROLE_ADMIN })
178         public void x2Setup(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
179                         @RequestBody SetupRequest setupRequest, HttpServletResponse response) {
180                 logger.debug("x2Setup instance {} request {}", instanceKey, setupRequest);
181                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
182                 api.x2Setup(setupRequest);
183                 response.setStatus(api.getApiClient().getStatusCode().value());
184         }
185
186         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
187         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
188                         + NODEB_SHUTDOWN_METHOD)
189         @Secured({ DashboardConstants.ROLE_ADMIN })
190         public void nodebShutdownPut(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
191                         HttpServletResponse response) {
192                 logger.debug("nodebShutdownPut instance {}", instanceKey);
193                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
194                 api.nodebShutdownPut();
195                 response.setStatus(api.getApiClient().getStatusCode().value());
196         }
197
198         @ApiOperation(value = "Abort any other ongoing procedures over X2 between the RIC and the RAN.")
199         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
200                         + "/{" + PP_RANNAME + "}/" + RESET_METHOD)
201         @Secured({ DashboardConstants.ROLE_ADMIN })
202         public void reset(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
203                         @PathVariable(PP_RANNAME) String ranName, @RequestBody ResetRequest resetRequest,
204                         HttpServletResponse response) {
205                 logger.debug("reset instance {} name {}", instanceKey, ranName);
206                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
207                 api.reset(ranName, resetRequest);
208                 response.setStatus(api.getApiClient().getStatusCode().value());
209         }
210
211 }