Pass thru error details from remote APIs
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / XappManagerController.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
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.oransc.ric.portal.dashboard.DashboardApplication;
27 import org.oransc.ric.portal.dashboard.DashboardConstants;
28 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
29 import org.oransc.ric.xappmgr.client.api.HealthApi;
30 import org.oransc.ric.xappmgr.client.api.XappApi;
31 import org.oransc.ric.xappmgr.client.model.AllXapps;
32 import org.oransc.ric.xappmgr.client.model.XAppInfo;
33 import org.oransc.ric.xappmgr.client.model.Xapp;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.util.Assert;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestMethod;
45 import org.springframework.web.bind.annotation.RestController;
46 import org.springframework.web.client.HttpStatusCodeException;
47
48 import io.swagger.annotations.ApiOperation;
49
50 /**
51  * Proxies calls from the front end to the xApp Manager API. All methods answer
52  * 502 on failure: <blockquote>HTTP server received an invalid response from a
53  * server it consulted when acting as a proxy or gateway.</blockquote>
54  */
55 @Configuration
56 @RestController
57 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xappmgr", produces = MediaType.APPLICATION_JSON_VALUE)
58 public class XappManagerController {
59
60         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62         // Populated by the autowired constructor
63         private final HealthApi healthApi;
64         private final XappApi xappApi;
65
66         @Autowired
67         public XappManagerController(final HealthApi healthApi, final XappApi xappApi) {
68                 Assert.notNull(healthApi, "health API must not be null");
69                 Assert.notNull(xappApi, "xapp API must not be null");
70                 this.healthApi = healthApi;
71                 this.xappApi = xappApi;
72                 if (logger.isDebugEnabled())
73                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
74                                         xappApi.getClass().getName());
75         }
76
77         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
78         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
79         public SuccessTransport getXappManagerClientVersion() {
80                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
81         }
82
83         @ApiOperation(value = "Calls the xApp Manager liveness health check.")
84         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
85         public Object getHealth(HttpServletResponse response) {
86                 logger.debug("getHealthAlive");
87                 try {
88                         healthApi.getHealthAlive();
89                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
90                         return null;
91                 } catch (HttpStatusCodeException ex) {
92                         logger.warn("getHealthAlive failed: {}", ex.toString());
93                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
94                 }
95         }
96
97         @ApiOperation(value = "Calls the xApp Manager readiness health check.")
98         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
99         public Object getHealthReady(HttpServletResponse response) {
100                 logger.debug("getHealthReady");
101                 try {
102                         healthApi.getHealthReady();
103                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
104                         return null;
105                 } catch (HttpStatusCodeException ex) {
106                         logger.warn("getHealthReady failed: {}", ex.toString());
107                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
108                 }
109         }
110
111         @ApiOperation(value = "Calls the xApp Manager to get the list of xApps.", response = AllXapps.class)
112         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
113         public Object getAllXapps() {
114                 logger.debug("getAllXapps");
115                 try {
116                         return xappApi.getAllXapps();
117                 } catch (HttpStatusCodeException ex) {
118                         logger.warn("getAllXapps failed: {}", ex.toString());
119                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
120                 }
121         }
122
123         @ApiOperation(value = "Calls the xApp Manager to get the named xApp.", response = Xapp.class)
124         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
125         public Object getXapp(@PathVariable("xAppName") String xAppName) {
126                 logger.debug("getXapp {}", xAppName);
127                 try {
128                         return xappApi.getXappByName(xAppName);
129                 } catch (HttpStatusCodeException ex) {
130                         logger.warn("getXapp failed: {}", ex.toString());
131                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
132                 }
133         }
134
135         @ApiOperation(value = "Calls the xApp Manager to deploy the specified Xapp.", response = Xapp.class)
136         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
137         public Object deployXapp(@RequestBody XAppInfo xAppInfo) {
138                 logger.debug("deployXapp {}", xAppInfo);
139                 try {
140                         return xappApi.deployXapp(xAppInfo);
141                 } catch (HttpStatusCodeException ex) {
142                         logger.warn("deployXapp failed: {}", ex.toString());
143                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
144                 }
145         }
146
147         @ApiOperation(value = "Calls the xApp Manager to undeploy the named Xapp.")
148         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
149         public Object undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
150                 logger.debug("undeployXapp {}", xAppName);
151                 try {
152                         xappApi.undeployXapp(xAppName);
153                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
154                         return null;
155                 } catch (HttpStatusCodeException ex) {
156                         logger.warn("undeployXapp failed: {}", ex.toString());
157                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
158                 }
159         }
160
161 }