Add error handling to improve user experience
[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.NodebIdentityGlobalNbId;
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.beans.factory.annotation.Value;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.http.MediaType;
44 import org.springframework.util.Assert;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestMethod;
49 import org.springframework.web.bind.annotation.RestController;
50 import org.springframework.web.client.HttpStatusCodeException;
51
52 import io.swagger.annotations.ApiOperation;
53
54 /**
55  * Proxies calls from the front end to the E2 Manager API. All methods answer
56  * 502 on failure and wrap the remote details: <blockquote>HTTP server received
57  * an invalid response from a server it consulted when acting as a proxy or
58  * gateway.</blockquote>
59  * 
60  * In R1 the E2 interface does not yet implement the get-ID-list method, so this
61  * class mocks up some functionality.
62  */
63 @Configuration
64 @RestController
65 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
66 public class E2ManagerController {
67
68         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
69
70         private final List<NodebIdentity> mockNodebIdList;
71
72         // Path parameters
73         private static final String PP_RANNAME = "ranName";
74
75         // Populated by the autowired constructor
76         private final HealthCheckApi e2HealthCheckApi;
77         private final NodebApi e2NodebApi;
78
79         @Autowired
80         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi,
81                         @Value("${e2mgr.mock.rannames:#{null}}") final String mockRanNames) {
82                 Assert.notNull(e2HealthCheckApi, "API must not be null");
83                 Assert.notNull(e2NodebApi, "API must not be null");
84                 this.e2HealthCheckApi = e2HealthCheckApi;
85                 this.e2NodebApi = e2NodebApi;
86                 mockNodebIdList = new ArrayList<>();
87                 if (mockRanNames != null) {
88                         logger.debug("ctor: Mocking RAN names: {}", mockRanNames);
89                         for (String id : mockRanNames.split(",")) {
90                                 NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId").plmnId("mockPlmId");
91                                 mockNodebIdList.add(new NodebIdentity().globalNbId(globalNbId).inventoryName(id.trim()));
92                         }
93                 }
94         }
95
96         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
97         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
98         public SuccessTransport getE2ManagerClientVersion() {
99                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
100         }
101
102         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
103         @RequestMapping(value = "/health", method = RequestMethod.GET)
104         public void healthGet(HttpServletResponse response) {
105                 logger.debug("healthGet");
106                 e2HealthCheckApi.healthGet();
107                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
108         }
109
110         // This calls other methods to simplify the task of the front-end.
111         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
112         @RequestMapping(value = "/ran", method = RequestMethod.GET)
113         public List<RanDetailsTransport> getRanDetails() {
114                 logger.debug("getRanDetails");
115                 // TODO: remove mock when e2mgr delivers the getNodebIdList() method
116                 List<NodebIdentity> nodebIdList = mockNodebIdList.isEmpty() ? e2NodebApi.getNodebIdList() : mockNodebIdList;
117                 List<RanDetailsTransport> details = new ArrayList<>();
118                 for (NodebIdentity nbid : nodebIdList) {
119                         GetNodebResponse nbResp = null;
120                         try {
121                                 // Catch exceptions to keep looping despite failures
122                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
123                         } catch (HttpStatusCodeException ex) {
124                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
125                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
126                                                 .ranName(nbid.getInventoryName());
127                         }
128                         details.add(new RanDetailsTransport(nbid, nbResp));
129                 }
130                 return details;
131         }
132
133         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
134         @RequestMapping(value = "/nodeb-ids", method = RequestMethod.GET)
135         public List<NodebIdentity> getNodebIdList() {
136                 logger.debug("getNodebIdList");
137                 return e2NodebApi.getNodebIdList();
138         }
139
140         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
141         @RequestMapping(value = "/nodeb/{" + PP_RANNAME + "}", method = RequestMethod.GET)
142         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
143                 logger.debug("getNb {}", ranName);
144                 return e2NodebApi.getNb(ranName);
145         }
146
147         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
148         @RequestMapping(value = "/nodeb", method = RequestMethod.DELETE)
149         public void nodebDelete(HttpServletResponse response) {
150                 logger.debug("nodebDelete");
151                 e2NodebApi.nodebDelete();
152                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
153         }
154
155         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
156         @RequestMapping(value = "/endcSetup", method = RequestMethod.POST)
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         @RequestMapping(value = "/x2Setup", method = RequestMethod.POST)
165         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
166                 logger.debug("x2Setup {}", setupRequest);
167                 e2NodebApi.x2Setup(setupRequest);
168                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
169         }
170
171 }