f164734e337a960913fcfb35d52f80608dcfe5a3
[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         // Dashboard only
74         public static final String HEALTH_METHOD = "health";
75         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
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 HealthCheckApi e2HealthCheckApi;
90         private final NodebApi e2NodebApi;
91
92         @Autowired
93         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
94                 Assert.notNull(e2HealthCheckApi, "API must not be null");
95                 Assert.notNull(e2NodebApi, "API must not be null");
96                 this.e2HealthCheckApi = e2HealthCheckApi;
97                 this.e2NodebApi = e2NodebApi;
98         }
99
100         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
101         @GetMapping(VERSION_METHOD)
102         // No role required
103         public SuccessTransport getClientVersion() {
104                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
105         }
106
107         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
108         @GetMapping(HEALTH_METHOD)
109         // No role required
110         public void healthGet(HttpServletResponse response) {
111                 logger.debug("healthGet");
112                 e2HealthCheckApi.healthGet();
113                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
114         }
115
116         // This calls other methods to simplify the task of the front-end.
117         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
118         @GetMapping(RAN_METHOD)
119         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
120         public List<RanDetailsTransport> getRanDetails() {
121                 logger.debug("getRanDetails");
122                 List<NodebIdentity> nodebIdList = e2NodebApi.getNodebIdList();
123                 logger.debug("getRanDetails: nodebIdList {}", nodebIdList);
124                 List<RanDetailsTransport> details = new ArrayList<>();
125                 for (NodebIdentity nbid : nodebIdList) {
126                         GetNodebResponse nbResp = null;
127                         try {
128                                 // Catch exceptions to keep looping despite failures
129                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
130                         } catch (HttpStatusCodeException ex) {
131                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
132                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
133                                                 .ranName(nbid.getInventoryName());
134                         }
135                         details.add(new RanDetailsTransport(nbid, nbResp));
136                 }
137                 return details;
138         }
139
140         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
141         @GetMapping(NODEB_LIST_METHOD)
142         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
143         public List<NodebIdentity> getNodebIdList() {
144                 logger.debug("getNodebIdList");
145                 return e2NodebApi.getNodebIdList();
146         }
147
148         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
149         @GetMapping(NODEB_SHUTDOWN_METHOD + "/{" + PP_RANNAME + "}")
150         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
151         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
152                 logger.debug("getNb {}", ranName);
153                 return e2NodebApi.getNb(ranName);
154         }
155
156         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
157         @PostMapping(ENDC_SETUP_METHOD)
158         @Secured({ DashboardConstants.ROLE_ADMIN })
159         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
160                 logger.debug("endcSetup {}", setupRequest);
161                 e2NodebApi.endcSetup(setupRequest);
162                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
163         }
164
165         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
166         @PostMapping(X2_SETUP_METHOD)
167         @Secured({ DashboardConstants.ROLE_ADMIN })
168         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
169                 logger.debug("x2Setup {}", setupRequest);
170                 e2NodebApi.x2Setup(setupRequest);
171                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
172         }
173
174         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
175         @PutMapping(NODEB_SHUTDOWN_METHOD)
176         @Secured({ DashboardConstants.ROLE_ADMIN })
177         public void nodebShutdownPut(HttpServletResponse response) {
178                 logger.debug("nodebShutdownPut");
179                 e2NodebApi.nodebShutdownPut();
180                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
181         }
182
183         @ApiOperation(value = "Abort any other ongoing procedures over X2 between the RIC and the RAN.")
184         @PutMapping(NODEB_PREFIX + "/{" + PP_RANNAME + "}" + RESET_METHOD)
185         @Secured({ DashboardConstants.ROLE_ADMIN })
186         public void reset(@PathVariable(PP_RANNAME) String ranName, @RequestBody ResetRequest resetRequest,
187                         HttpServletResponse response) {
188                 logger.debug("reset");
189                 e2NodebApi.reset(ranName, resetRequest);
190                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
191         }
192
193 }