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