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