Upgrade xapp manager to spec 0.1.3
[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 getVersion() {
79                 logger.debug("getVersion enter");
80                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
81         }
82
83         @ApiOperation(value = "Calls the xApp Manager liveness health check.")
84         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
85         public void getHealth(HttpServletResponse response) {
86                 logger.debug("getHealth");
87                 healthApi.getHealthAlive();
88                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
89         }
90
91         @ApiOperation(value = "Calls the xApp Manager readiness health check.")
92         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
93         public void getHealthReady(HttpServletResponse response) {
94                 logger.debug("getHealthReady");
95                 healthApi.getHealthReady();
96                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
97         }
98
99         @ApiOperation(value = "Calls the xApp Manager to get the list of xApps.", response = AllXapps.class)
100         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
101         public AllXapps getAllXapps() {
102                 if (logger.isDebugEnabled())
103                         logger.debug("getAllXapps via {}", xappApi.getApiClient().getBasePath());
104                 return xappApi.getAllXapps();
105         }
106
107         @ApiOperation(value = "Calls the xApp Manager to get the named xApp.", response = Xapp.class)
108         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
109         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
110                 logger.debug("getXapp {}", xAppName);
111                 return xappApi.getXappByName(xAppName);
112         }
113
114         @ApiOperation(value = "Calls the xApp Manager to deploy the specified Xapp.", response = Xapp.class)
115         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
116         public Object deployXapp(@RequestBody XAppInfo xAppInfo, HttpServletResponse response) {
117                 logger.debug("deployXapp {}", xAppInfo);
118                 try {
119                         return xappApi.deployXapp(xAppInfo);
120                 } catch (Exception ex) {
121                         logger.error("deployXapp failed", ex);
122                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
123                         return new ErrorTransport(500, "deployXapp failed", ex);
124                 }
125         }
126
127         @ApiOperation(value = "Calls the xApp Manager to undeploy the named Xapp.")
128         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
129         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
130                 logger.debug("undeployXapp {}", xAppName);
131                 try {
132                         xappApi.undeployXapp(xAppName);
133                 } catch (Exception ex) {
134                         logger.error("deployXapp failed", ex);
135                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
136                 }
137         }
138
139 }