1101ac681e6270aaa1043a9027cec1709ce84379
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / 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.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.DashboardConstants;
27 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
28 import org.oransc.ric.xappmgr.client.api.DefaultApi;
29 import org.oransc.ric.xappmgr.client.model.AllXapps;
30 import org.oransc.ric.xappmgr.client.model.XAppInfo;
31 import org.oransc.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                 if (logger.isDebugEnabled())
65                         logger.debug("ctor: configured with client type {}", xappMgrClient.getClass().getName());
66                 this.xappMgrClient = xappMgrClient;
67         }
68
69         @ApiOperation(value = "Calls the xApp Manager health check.")
70         @RequestMapping(value = "/health", method = RequestMethod.GET)
71         public void getHealth(HttpServletResponse response) {
72                 logger.debug("getHealth");
73                 xappMgrClient.getHealth();
74                 response.setStatus(xappMgrClient.getApiClient().getStatusCode().value());
75         }
76
77         @ApiOperation(value = "Calls the xApp Manager to get the list of xApps.", response = AllXapps.class)
78         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
79         public AllXapps getAllXapps() {
80                 if (logger.isDebugEnabled())
81                         logger.debug("getAllXapps via {}", xappMgrClient.getApiClient().getBasePath());
82                 return xappMgrClient.getAllXapps();
83         }
84
85         @ApiOperation(value = "Calls the xApp Manager to get the named xApp.", response = Xapp.class)
86         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
87         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
88                 logger.debug("getXapp {}", xAppName);
89                 return xappMgrClient.getXappByName(xAppName);
90         }
91
92         @ApiOperation(value = "Calls the xApp Manager to deploy the specified Xapp.", response = Xapp.class)
93         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
94         public Object deployXapp(@RequestBody XAppInfo xAppInfo, HttpServletResponse response) {
95                 logger.debug("deployXapp {}", xAppInfo);
96                 try {
97                         return xappMgrClient.deployXapp(xAppInfo);
98                 } catch (Exception ex) {
99                         logger.error("deployXapp failed", ex);
100                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
101                         return new ErrorTransport(500, "deployXapp failed", ex);
102                 }
103         }
104
105         @ApiOperation(value = "Calls the xApp Manager to undeploy the named Xapp.")
106         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
107         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
108                 logger.debug("undeployXapp {}", xAppName);
109                 try {
110                         xappMgrClient.undeployXapp(xAppName);
111                 } catch (Exception ex) {
112                         logger.error("deployXapp failed", ex);
113                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
114                 }
115         }
116
117 }