2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
24 import javax.servlet.http.HttpServletResponse;
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.AllXappConfig;
32 import org.oransc.ric.xappmgr.client.model.AllXapps;
33 import org.oransc.ric.xappmgr.client.model.ConfigMetadata;
34 import org.oransc.ric.xappmgr.client.model.XAppConfig;
35 import org.oransc.ric.xappmgr.client.model.XAppInfo;
36 import org.oransc.ric.xappmgr.client.model.Xapp;
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;
51 import io.swagger.annotations.ApiOperation;
54 * Proxies calls from the front end to the xApp 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>
60 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xappmgr", produces = MediaType.APPLICATION_JSON_VALUE)
61 public class XappManagerController {
63 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65 // Populated by the autowired constructor
66 private final HealthApi healthApi;
67 private final XappApi xappApi;
70 public XappManagerController(final HealthApi healthApi, final XappApi xappApi) {
71 Assert.notNull(healthApi, "health API must not be null");
72 Assert.notNull(xappApi, "xapp API must not be null");
73 this.healthApi = healthApi;
74 this.xappApi = xappApi;
75 if (logger.isDebugEnabled())
76 logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
77 xappApi.getClass().getName());
80 @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
81 @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
82 public SuccessTransport getXappManagerClientVersion() {
83 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
86 @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
87 @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
88 public Object getHealth(HttpServletResponse response) {
89 logger.debug("getHealthAlive");
91 healthApi.getHealthAlive();
92 response.setStatus(healthApi.getApiClient().getStatusCode().value());
94 } catch (HttpStatusCodeException ex) {
95 logger.error("getHealthAlive failed: {}", ex.toString());
96 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
100 @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
101 @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
102 public Object getHealthReady(HttpServletResponse response) {
103 logger.debug("getHealthReady");
105 healthApi.getHealthReady();
106 response.setStatus(healthApi.getApiClient().getStatusCode().value());
108 } catch (HttpStatusCodeException ex) {
109 logger.error("getHealthReady failed: {}", ex.toString());
110 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
114 @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
115 @RequestMapping(value = "/config", method = RequestMethod.GET)
116 public Object getAllXappConfig() {
117 logger.debug("getAllXappConfig");
119 return xappApi.getAllXappConfig();
120 } catch (HttpStatusCodeException ex) {
121 logger.error("getAllXappConfig failed: {}", ex.toString());
122 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
126 @ApiOperation(value = "Create xApp config.")
127 @RequestMapping(value = "/config", method = RequestMethod.POST)
128 public Object createXappConfig(@RequestBody XAppConfig xAppConfig) {
129 logger.debug("createXappConfig {}", xAppConfig);
131 return xappApi.createXappConfig(xAppConfig);
132 } catch (HttpStatusCodeException ex) {
133 logger.error("undeployXapp failed: {}", ex.toString());
134 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
138 @ApiOperation(value = "Modify xApp config.")
139 @RequestMapping(value = "/config", method = RequestMethod.PUT)
140 public Object modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
141 logger.debug("modifyXappConfig {}", xAppConfig);
143 return xappApi.modifyXappConfig(xAppConfig);
144 } catch (HttpStatusCodeException ex) {
145 logger.error("modifyXappConfig failed: {}", ex.toString());
146 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
150 @ApiOperation(value = "Delete xApp configuration.")
151 @RequestMapping(value = "/config/{xAppName}", method = RequestMethod.DELETE)
152 public Object deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
153 logger.debug("deleteXappConfig {}", configMetadata);
155 xappApi.deleteXappConfig(configMetadata);
156 response.setStatus(healthApi.getApiClient().getStatusCode().value());
158 } catch (HttpStatusCodeException ex) {
159 logger.error("deleteXappConfig failed: {}", ex.toString());
160 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
164 @ApiOperation(value = "Returns the status of all xapps.", response = AllXapps.class)
165 @RequestMapping(value = "/xapps", method = RequestMethod.GET)
166 public Object getAllXapps() {
167 logger.debug("getAllXapps");
169 return xappApi.getAllXapps();
170 } catch (HttpStatusCodeException ex) {
171 logger.error("getAllXapps failed: {}", ex.toString());
172 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
176 @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
177 @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
178 public Object getXapp(@PathVariable("xAppName") String xAppName) {
179 logger.debug("getXapp {}", xAppName);
181 return xappApi.getXappByName(xAppName);
182 } catch (HttpStatusCodeException ex) {
183 logger.error("getXapp failed: {}", ex.toString());
184 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
188 @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
189 @RequestMapping(value = "/xapps", method = RequestMethod.POST)
190 public Object deployXapp(@RequestBody XAppInfo xAppInfo) {
191 logger.debug("deployXapp {}", xAppInfo);
193 return xappApi.deployXapp(xAppInfo);
194 } catch (HttpStatusCodeException ex) {
195 logger.error("deployXapp failed: {}", ex.toString());
196 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
200 @ApiOperation(value = "Undeploy an existing xapp.")
201 @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
202 public Object undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
203 logger.debug("undeployXapp {}", xAppName);
205 xappApi.undeployXapp(xAppName);
206 response.setStatus(healthApi.getApiClient().getStatusCode().value());
208 } catch (HttpStatusCodeException ex) {
209 logger.error("undeployXapp failed: {}", ex.toString());
210 return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());