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