Add error handling to improve user experience
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / AppManagerController.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.plt.appmgr.client.api.HealthApi;
27 import org.oransc.ric.plt.appmgr.client.api.XappApi;
28 import org.oransc.ric.plt.appmgr.client.model.AllDeployableXapps;
29 import org.oransc.ric.plt.appmgr.client.model.AllDeployedXapps;
30 import org.oransc.ric.plt.appmgr.client.model.AllXappConfig;
31 import org.oransc.ric.plt.appmgr.client.model.ConfigMetadata;
32 import org.oransc.ric.plt.appmgr.client.model.XAppConfig;
33 import org.oransc.ric.plt.appmgr.client.model.XAppInfo;
34 import org.oransc.ric.plt.appmgr.client.model.Xapp;
35 import org.oransc.ric.portal.dashboard.DashboardApplication;
36 import org.oransc.ric.portal.dashboard.DashboardConstants;
37 import org.oransc.ric.portal.dashboard.model.AppTransport;
38 import org.oransc.ric.portal.dashboard.model.DashboardDeployableXapps;
39 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.Configuration;
44 import org.springframework.http.MediaType;
45 import org.springframework.util.Assert;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51
52 import io.swagger.annotations.ApiOperation;
53
54 /**
55  * Proxies calls from the front end to the App Manager API. All methods answer
56  * 502 on failure: <blockquote>HTTP server received an invalid response from a
57  * server it consulted when acting as a proxy or gateway.</blockquote>
58  */
59 @Configuration
60 @RestController
61 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/appmgr", produces = MediaType.APPLICATION_JSON_VALUE)
62 public class AppManagerController {
63
64         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65
66         // Populated by the autowired constructor
67         private final HealthApi healthApi;
68         private final XappApi xappApi;
69
70         @Autowired
71         public AppManagerController(final HealthApi healthApi, final XappApi xappApi) {
72                 Assert.notNull(healthApi, "health API must not be null");
73                 Assert.notNull(xappApi, "xapp API must not be null");
74                 this.healthApi = healthApi;
75                 this.xappApi = xappApi;
76                 if (logger.isDebugEnabled())
77                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
78                                         xappApi.getClass().getName());
79         }
80
81         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
82         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
83         public SuccessTransport getXappManagerClientVersion() {
84                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
85         }
86
87         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
88         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
89         public void getHealth(HttpServletResponse response) {
90                 logger.debug("getHealthAlive");
91                 healthApi.getHealthAlive();
92                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
93         }
94
95         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
96         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
97         public void getHealthReady(HttpServletResponse response) {
98                 logger.debug("getHealthReady");
99                 healthApi.getHealthReady();
100                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
101         }
102
103         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
104         @RequestMapping(value = "/config", method = RequestMethod.GET)
105         public AllXappConfig getAllXappConfig() {
106                 logger.debug("getAllXappConfig");
107                 return xappApi.getAllXappConfig();
108         }
109
110         @ApiOperation(value = "Create xApp config.", response = XAppConfig.class)
111         @RequestMapping(value = "/config", method = RequestMethod.POST)
112         public XAppConfig createXappConfig(@RequestBody XAppConfig xAppConfig) {
113                 logger.debug("createXappConfig {}", xAppConfig);
114                 return xappApi.createXappConfig(xAppConfig);
115         }
116
117         @ApiOperation(value = "Modify xApp config.", response = XAppConfig.class)
118         @RequestMapping(value = "/config", method = RequestMethod.PUT)
119         public XAppConfig modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
120                 logger.debug("modifyXappConfig {}", xAppConfig);
121                 return xappApi.modifyXappConfig(xAppConfig);
122         }
123
124         @ApiOperation(value = "Delete xApp configuration.")
125         @RequestMapping(value = "/config/{xAppName}", method = RequestMethod.DELETE)
126         public void deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
127                 logger.debug("deleteXappConfig {}", configMetadata);
128                 xappApi.deleteXappConfig(configMetadata);
129                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
130         }
131
132         @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
133         @RequestMapping(value = "/xapps/list", method = RequestMethod.GET)
134         public DashboardDeployableXapps getAvailableXapps() {
135                 logger.debug("getAvailableXapps");
136                 AllDeployableXapps appNames = xappApi.listAllXapps();
137                 // Answer a collection of structure instead of string
138                 // because I expect the AppMgr to be extended with
139                 // additional properties for each one.
140                 DashboardDeployableXapps apps = new DashboardDeployableXapps();
141                 for (String n : appNames)
142                         apps.add(new AppTransport(n));
143                 return apps;
144         }
145
146         @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
147         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
148         public AllDeployedXapps getDeployedXapps() {
149                 logger.debug("getDeployedXapps");
150                 return xappApi.getAllXapps();
151         }
152
153         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
154         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
155         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
156                 logger.debug("getXapp {}", xAppName);
157                 return xappApi.getXappByName(xAppName);
158         }
159
160         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
161         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
162         public Xapp deployXapp(@RequestBody XAppInfo xAppInfo) {
163                 logger.debug("deployXapp {}", xAppInfo);
164                 return xappApi.deployXapp(xAppInfo);
165         }
166
167         @ApiOperation(value = "Undeploy an existing xapp.")
168         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
169         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
170                 logger.debug("undeployXapp {}", xAppName);
171                 xappApi.undeployXapp(xAppName);
172                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
173         }
174
175 }