add xapp deploy dialog, call backend api
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oranosc / ric / portal / dash / controller / XappManagerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ORAN-OSC
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.oranosc.ric.portal.dash.controller;
21
22 import java.lang.invoke.MethodHandles;
23
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.oranosc.ric.portal.dash.DashboardConstants;
27 import org.oranosc.ric.portal.dash.model.ErrorTransport;
28 import org.oranosc.ric.xappmgr.client.api.DefaultApi;
29 import org.oranosc.ric.xappmgr.client.model.AllXapps;
30 import org.oranosc.ric.xappmgr.client.model.XAppInfo;
31 import org.oranosc.ric.xappmgr.client.model.Xapp;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.http.MediaType;
37 import org.springframework.util.Assert;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.bind.annotation.RestController;
43
44 import io.swagger.annotations.ApiOperation;
45
46 /**
47  * Mimics the xApp Manager API. These controller methods just proxy calls from
48  * the front-end thru to the real back-end.
49  *
50  */
51 @Configuration
52 @RestController
53 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xappmgr", produces = MediaType.APPLICATION_JSON_VALUE)
54 public class XappManagerController {
55
56         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58         // Populated by the autowired constructor
59         private final DefaultApi xappMgrClient;
60
61         @Autowired
62         public XappManagerController(final DefaultApi xappMgrClient) {
63                 Assert.notNull(xappMgrClient, "client must not be null");
64                 this.xappMgrClient = xappMgrClient;
65         }
66
67         @ApiOperation(value = "Calls the xApp Manager health check.")
68         @RequestMapping(value = "/health", method = RequestMethod.GET)
69         public void getHealth(HttpServletResponse response) {
70                 logger.debug("getHealth");
71                 xappMgrClient.getHealth();
72                 response.setStatus(xappMgrClient.getApiClient().getStatusCode().value());
73         }
74
75         @ApiOperation(value = "Calls the xApp Manager to get the list of xApps.", response = AllXapps.class)
76         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
77         public AllXapps getAllXapps() {
78                 logger.debug("getAllXapps via " + xappMgrClient.getApiClient().getBasePath());
79                 return xappMgrClient.getAllXapps();
80         }
81
82         @ApiOperation(value = "Calls the xApp Manager to get the named xApp.", response = Xapp.class)
83         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
84         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
85                 logger.debug("getXapp {}", xAppName);
86                 return xappMgrClient.getXappByName(xAppName);
87         }
88
89         @ApiOperation(value = "Calls the xApp Manager to deploy the specified Xapp.", response = Xapp.class)
90         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
91         public Object deployXapp(@RequestBody XAppInfo xAppInfo, HttpServletResponse response) {
92                 logger.debug("deployXapp {}", xAppInfo);
93                 try {
94                         return xappMgrClient.deployXapp(xAppInfo);
95                 } catch (Exception ex) {
96                         logger.error("deployXapp failed", ex);
97                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
98                         return new ErrorTransport(500, "deployXapp failed", ex);
99                 }
100         }
101
102         @ApiOperation(value = "Calls the xApp Manager to undeploy the named Xapp.")
103         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
104         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
105                 logger.debug("undeployXapp {}", xAppName);
106                 try {
107                         xappMgrClient.undeployXapp(xAppName);
108                 } catch (Exception ex) {
109                         logger.error("deployXapp failed", ex);
110                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
111                 }
112         }
113
114 }