Pass thru error details from remote APIs
[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.HashSet;
24 import java.util.Set;
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.SetupRequest;
32 import org.oransc.ric.portal.dashboard.DashboardApplication;
33 import org.oransc.ric.portal.dashboard.DashboardConstants;
34 import org.oransc.ric.portal.dashboard.model.E2SetupRequestType;
35 import org.oransc.ric.portal.dashboard.model.E2SetupResponse;
36 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.util.Assert;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.client.HttpStatusCodeException;
50
51 import io.swagger.annotations.ApiOperation;
52
53 /**
54  * Proxies calls from the front end to the E2 Manager API. All methods answer
55  * 502 on failure: <blockquote>HTTP server received an invalid response from a
56  * server it consulted when acting as a proxy or gateway.</blockquote>
57  * 
58  * As of this writing the E2 interface does not support get-all, so this class
59  * mocks up some of that functionality.
60  */
61 @Configuration
62 @RestController
63 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
64 public class E2ManagerController {
65
66         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67
68         // Path parameters
69         private static final String PP_RANNAME = "ranName";
70
71         // Populated by the autowired constructor
72         private final HealthCheckApi e2HealthCheckApi;
73         private final NodebApi e2NodebApi;
74
75         // Stores the requests and results.
76         // TODO remove when the E2 manager is extended.
77         private Set<E2SetupResponse> responses = new HashSet<>();
78
79         @Autowired
80         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
81                 Assert.notNull(e2HealthCheckApi, "API must not be null");
82                 Assert.notNull(e2NodebApi, "API must not be null");
83                 this.e2HealthCheckApi = e2HealthCheckApi;
84                 this.e2NodebApi = e2NodebApi;
85         }
86
87         private void assertNotNull(Object o) {
88                 if (o == null)
89                         throw new IllegalArgumentException("Null not permitted");
90         }
91
92         private void assertNotEmpty(String s) {
93                 assertNotNull(s);
94                 if (s.isEmpty())
95                         throw new IllegalArgumentException("Empty not permitted");
96         }
97
98         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
99         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
100         public SuccessTransport getE2ManagerClientVersion() {
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         @RequestMapping(value = "/health", method = RequestMethod.GET)
106         public Object healthGet(HttpServletResponse response) {
107                 logger.debug("healthGet");
108                 try {
109                         e2HealthCheckApi.healthGet();
110                         response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
111                         return null;
112                 } catch (HttpStatusCodeException ex) {
113                         logger.warn("healthGet failed: {}", ex.toString());
114                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
115                 }
116         }
117
118         // TODO replace with actual functionality
119         @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = E2SetupResponse.class, responseContainer = "List")
120         @RequestMapping(value = "/setup", method = RequestMethod.GET)
121         public Iterable<E2SetupResponse> getRequests() {
122                 logger.debug("getRequests");
123                 return responses;
124         }
125
126         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
127         @RequestMapping(value = "/nodeb/{" + PP_RANNAME + "}", method = RequestMethod.GET)
128         public Object getNb(@PathVariable(PP_RANNAME) String ranName) {
129                 logger.debug("getNb {}", ranName);
130                 try {
131                         return e2NodebApi.getNb(ranName);
132                 } catch (HttpStatusCodeException ex) {
133                         logger.warn("getNb failed: {}", ex.toString());
134                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
135                 }
136         }
137
138         // TODO replace with actual functionality
139         @ApiOperation(value = "Disconnect all RAN Connections.")
140         @RequestMapping(value = "/disconnectAllRAN", method = RequestMethod.DELETE)
141         public void disconnectAllRANConnections() {
142                 logger.debug("disconnectAllRANConnections");
143                 responses.clear();
144         }
145
146         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.", response = E2SetupResponse.class)
147         @RequestMapping(value = "/endcSetup", method = RequestMethod.POST)
148         public Object endcSetup(@RequestBody SetupRequest setupRequest) {
149                 logger.debug("endcSetup {}", setupRequest);
150                 try {
151                         assertNotEmpty(setupRequest.getRanIp());
152                         assertNotEmpty(setupRequest.getRanName());
153                         assertNotNull(setupRequest.getRanPort());
154                 } catch (Exception ex) {
155                         return new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, HttpServletResponse.SC_BAD_REQUEST);
156                 }
157                 try {
158                         e2NodebApi.endcSetup(setupRequest);
159                         E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest,
160                                         e2NodebApi.getApiClient().getStatusCode().value());
161                         responses.add(r);
162                         return r;
163                 } catch (HttpStatusCodeException ex) {
164                         logger.warn("endcSetup failed: {}", ex.toString());
165                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
166                 }
167         }
168
169         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.", response = E2SetupResponse.class)
170         @RequestMapping(value = "/x2Setup", method = RequestMethod.POST)
171         public Object x2Setup(@RequestBody SetupRequest setupRequest) {
172                 logger.debug("x2Setup {}", setupRequest);
173                 try {
174                         assertNotEmpty(setupRequest.getRanIp());
175                         assertNotEmpty(setupRequest.getRanName());
176                         assertNotNull(setupRequest.getRanPort());
177                 } catch (Exception ex) {
178                         return new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, HttpServletResponse.SC_BAD_REQUEST);
179                 }
180                 try {
181                         e2NodebApi.x2Setup(setupRequest);
182                         E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.X2, setupRequest,
183                                         e2NodebApi.getApiClient().getStatusCode().value());
184                         responses.add(r);
185                         return r;
186                 } catch (HttpStatusCodeException ex) {
187                         logger.warn("x2Setup failed: {}", ex.toString());
188                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
189                 }
190         }
191
192 }