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