Upgrade App Manager to version 0.1.5
[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.http.ResponseEntity;
46 import org.springframework.util.Assert;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.bind.annotation.RestController;
52 import org.springframework.web.client.HttpStatusCodeException;
53
54 import io.swagger.annotations.ApiOperation;
55
56 /**
57  * Proxies calls from the front end to the App Manager API. All methods answer
58  * 502 on failure: <blockquote>HTTP server received an invalid response from a
59  * server it consulted when acting as a proxy or gateway.</blockquote>
60  */
61 @Configuration
62 @RestController
63 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/appmgr", produces = MediaType.APPLICATION_JSON_VALUE)
64 public class AppManagerController {
65
66         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67
68         // Populated by the autowired constructor
69         private final HealthApi healthApi;
70         private final XappApi xappApi;
71
72         @Autowired
73         public AppManagerController(final HealthApi healthApi, final XappApi xappApi) {
74                 Assert.notNull(healthApi, "health API must not be null");
75                 Assert.notNull(xappApi, "xapp API must not be null");
76                 this.healthApi = healthApi;
77                 this.xappApi = xappApi;
78                 if (logger.isDebugEnabled())
79                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
80                                         xappApi.getClass().getName());
81         }
82
83         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
84         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
85         public SuccessTransport getXappManagerClientVersion() {
86                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
87         }
88
89         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
90         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
91         public Object getHealth(HttpServletResponse response) {
92                 logger.debug("getHealthAlive");
93                 try {
94                         healthApi.getHealthAlive();
95                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
96                         return null;
97                 } catch (HttpStatusCodeException ex) {
98                         logger.error("getHealthAlive failed: {}", ex.toString());
99                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
100                 }
101         }
102
103         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
104         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
105         public Object getHealthReady(HttpServletResponse response) {
106                 logger.debug("getHealthReady");
107                 try {
108                         healthApi.getHealthReady();
109                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
110                         return null;
111                 } catch (HttpStatusCodeException ex) {
112                         logger.error("getHealthReady failed: {}", ex.toString());
113                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
114                 }
115         }
116
117         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
118         @RequestMapping(value = "/config", method = RequestMethod.GET)
119         public Object getAllXappConfig() {
120                 logger.debug("getAllXappConfig");
121                 try {
122                         return xappApi.getAllXappConfig();
123                 } catch (HttpStatusCodeException ex) {
124                         logger.error("getAllXappConfig failed: {}", ex.toString());
125                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
126                 }
127         }
128
129         @ApiOperation(value = "Create xApp config.")
130         @RequestMapping(value = "/config", method = RequestMethod.POST)
131         public Object createXappConfig(@RequestBody XAppConfig xAppConfig) {
132                 logger.debug("createXappConfig {}", xAppConfig);
133                 try {
134                         return xappApi.createXappConfig(xAppConfig);
135                 } catch (HttpStatusCodeException ex) {
136                         logger.error("undeployXapp failed: {}", ex.toString());
137                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
138                 }
139         }
140
141         @ApiOperation(value = "Modify xApp config.")
142         @RequestMapping(value = "/config", method = RequestMethod.PUT)
143         public Object modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
144                 logger.debug("modifyXappConfig {}", xAppConfig);
145                 try {
146                         return xappApi.modifyXappConfig(xAppConfig);
147                 } catch (HttpStatusCodeException ex) {
148                         logger.error("modifyXappConfig failed: {}", ex.toString());
149                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
150                 }
151         }
152
153         @ApiOperation(value = "Delete xApp configuration.")
154         @RequestMapping(value = "/config/{xAppName}", method = RequestMethod.DELETE)
155         public Object deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
156                 logger.debug("deleteXappConfig {}", configMetadata);
157                 try {
158                         xappApi.deleteXappConfig(configMetadata);
159                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
160                         return null;
161                 } catch (HttpStatusCodeException ex) {
162                         logger.error("deleteXappConfig failed: {}", ex.toString());
163                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
164                 }
165         }
166
167         @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
168         @RequestMapping(value = "/xapps/list", method = RequestMethod.GET)
169         public Object getAvailableXapps() {
170                 logger.debug("getAvailableXapps");
171                 try {
172                         AllDeployableXapps appNames = xappApi.listAllXapps();
173                         // Answer a collection of structure instead of string
174                         DashboardDeployableXapps apps = new DashboardDeployableXapps();
175                         for (String n : appNames)
176                                 apps.add(new AppTransport(n));
177                         return apps;
178                 } catch (HttpStatusCodeException ex) {
179                         logger.error("getAvailableXapps failed: {}", ex.toString());
180                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
181                 }
182         }
183
184         @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
185         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
186         public Object getDeployedXapps() {
187                 logger.debug("getDeployedXapps");
188                 try {
189                         return xappApi.getAllXapps();
190                 } catch (HttpStatusCodeException ex) {
191                         logger.error("getDeployedXapps failed: {}", ex.toString());
192                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
193                 }
194         }
195
196         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
197         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
198         public Object getXapp(@PathVariable("xAppName") String xAppName) {
199                 logger.debug("getXapp {}", xAppName);
200                 try {
201                         return xappApi.getXappByName(xAppName);
202                 } catch (HttpStatusCodeException ex) {
203                         logger.error("getXapp failed: {}", ex.toString());
204                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
205                 }
206         }
207
208         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
209         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
210         public Object deployXapp(@RequestBody XAppInfo xAppInfo) {
211                 logger.debug("deployXapp {}", xAppInfo);
212                 try {
213                         return xappApi.deployXapp(xAppInfo);
214                 } catch (HttpStatusCodeException ex) {
215                         logger.error("deployXapp failed: {}", ex.toString());
216                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
217                 }
218         }
219
220         @ApiOperation(value = "Undeploy an existing xapp.")
221         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
222         public Object undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
223                 logger.debug("undeployXapp {}", xAppName);
224                 try {
225                         xappApi.undeployXapp(xAppName);
226                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
227                         return null;
228                 } catch (HttpStatusCodeException ex) {
229                         logger.error("undeployXapp failed: {}", ex.toString());
230                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
231                 }
232         }
233
234 }