Add Xapp Manager config endpoints to backend
[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.SuccessTransport;
29 import org.oransc.ric.xappmgr.client.api.HealthApi;
30 import org.oransc.ric.xappmgr.client.api.XappApi;
31 import org.oransc.ric.xappmgr.client.model.AllXappConfig;
32 import org.oransc.ric.xappmgr.client.model.AllXapps;
33 import org.oransc.ric.xappmgr.client.model.ConfigMetadata;
34 import org.oransc.ric.xappmgr.client.model.XAppConfig;
35 import org.oransc.ric.xappmgr.client.model.XAppInfo;
36 import org.oransc.ric.xappmgr.client.model.Xapp;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.util.Assert;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.client.HttpStatusCodeException;
50
51 import io.swagger.annotations.ApiOperation;
52
53 /**
54  * Proxies calls from the front end to the xApp Manager API. All methods answer
55  * 502 on failure: <blockquote>HTTP server received an invalid response from a
56  * server it consulted when acting as a proxy or gateway.</blockquote>
57  */
58 @Configuration
59 @RestController
60 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xappmgr", produces = MediaType.APPLICATION_JSON_VALUE)
61 public class XappManagerController {
62
63         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64
65         // Populated by the autowired constructor
66         private final HealthApi healthApi;
67         private final XappApi xappApi;
68
69         @Autowired
70         public XappManagerController(final HealthApi healthApi, final XappApi xappApi) {
71                 Assert.notNull(healthApi, "health API must not be null");
72                 Assert.notNull(xappApi, "xapp API must not be null");
73                 this.healthApi = healthApi;
74                 this.xappApi = xappApi;
75                 if (logger.isDebugEnabled())
76                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
77                                         xappApi.getClass().getName());
78         }
79
80         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
81         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
82         public SuccessTransport getXappManagerClientVersion() {
83                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
84         }
85
86         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
87         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
88         public Object getHealth(HttpServletResponse response) {
89                 logger.debug("getHealthAlive");
90                 try {
91                         healthApi.getHealthAlive();
92                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
93                         return null;
94                 } catch (HttpStatusCodeException ex) {
95                         logger.error("getHealthAlive failed: {}", ex.toString());
96                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
97                 }
98         }
99
100         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
101         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
102         public Object getHealthReady(HttpServletResponse response) {
103                 logger.debug("getHealthReady");
104                 try {
105                         healthApi.getHealthReady();
106                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
107                         return null;
108                 } catch (HttpStatusCodeException ex) {
109                         logger.error("getHealthReady failed: {}", ex.toString());
110                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
111                 }
112         }
113
114         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
115         @RequestMapping(value = "/config", method = RequestMethod.GET)
116         public Object getAllXappConfig() {
117                 logger.debug("getAllXappConfig");
118                 try {
119                         return xappApi.getAllXappConfig();
120                 } catch (HttpStatusCodeException ex) {
121                         logger.error("getAllXappConfig failed: {}", ex.toString());
122                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
123                 }
124         }
125
126         @ApiOperation(value = "Create xApp config.")
127         @RequestMapping(value = "/config", method = RequestMethod.POST)
128         public Object createXappConfig(@RequestBody XAppConfig xAppConfig) {
129                 logger.debug("createXappConfig {}", xAppConfig);
130                 try {
131                         return xappApi.createXappConfig(xAppConfig);
132                 } catch (HttpStatusCodeException ex) {
133                         logger.error("undeployXapp failed: {}", ex.toString());
134                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
135                 }
136         }
137
138         @ApiOperation(value = "Modify xApp config.")
139         @RequestMapping(value = "/config", method = RequestMethod.PUT)
140         public Object modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
141                 logger.debug("modifyXappConfig {}", xAppConfig);
142                 try {
143                         return xappApi.modifyXappConfig(xAppConfig);
144                 } catch (HttpStatusCodeException ex) {
145                         logger.error("modifyXappConfig failed: {}", ex.toString());
146                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
147                 }
148         }
149
150         @ApiOperation(value = "Delete xApp configuration.")
151         @RequestMapping(value = "/config/{xAppName}", method = RequestMethod.DELETE)
152         public Object deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
153                 logger.debug("deleteXappConfig {}", configMetadata);
154                 try {
155                         xappApi.deleteXappConfig(configMetadata);
156                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
157                         return null;
158                 } catch (HttpStatusCodeException ex) {
159                         logger.error("deleteXappConfig failed: {}", ex.toString());
160                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
161                 }
162         }
163
164         @ApiOperation(value = "Returns the status of all xapps.", response = AllXapps.class)
165         @RequestMapping(value = "/xapps", method = RequestMethod.GET)
166         public Object getAllXapps() {
167                 logger.debug("getAllXapps");
168                 try {
169                         return xappApi.getAllXapps();
170                 } catch (HttpStatusCodeException ex) {
171                         logger.error("getAllXapps failed: {}", ex.toString());
172                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
173                 }
174         }
175
176         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
177         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.GET)
178         public Object getXapp(@PathVariable("xAppName") String xAppName) {
179                 logger.debug("getXapp {}", xAppName);
180                 try {
181                         return xappApi.getXappByName(xAppName);
182                 } catch (HttpStatusCodeException ex) {
183                         logger.error("getXapp failed: {}", ex.toString());
184                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
185                 }
186         }
187
188         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
189         @RequestMapping(value = "/xapps", method = RequestMethod.POST)
190         public Object deployXapp(@RequestBody XAppInfo xAppInfo) {
191                 logger.debug("deployXapp {}", xAppInfo);
192                 try {
193                         return xappApi.deployXapp(xAppInfo);
194                 } catch (HttpStatusCodeException ex) {
195                         logger.error("deployXapp failed: {}", ex.toString());
196                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
197                 }
198         }
199
200         @ApiOperation(value = "Undeploy an existing xapp.")
201         @RequestMapping(value = "/xapps/{xAppName}", method = RequestMethod.DELETE)
202         public Object undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
203                 logger.debug("undeployXapp {}", xAppName);
204                 try {
205                         xappApi.undeployXapp(xAppName);
206                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
207                         return null;
208                 } catch (HttpStatusCodeException ex) {
209                         logger.error("undeployXapp failed: {}", ex.toString());
210                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
211                 }
212         }
213
214 }