041574629bce77475f0ae298ba6207edc7eaffef
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / AppManagerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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.plt.appmgr.client.api.HealthApi;
27 import org.oransc.ric.plt.appmgr.client.api.XappApi;
28 import org.oransc.ric.plt.appmgr.client.model.AllDeployableXapps;
29 import org.oransc.ric.plt.appmgr.client.model.AllDeployedXapps;
30 import org.oransc.ric.plt.appmgr.client.model.AllXappConfig;
31 import org.oransc.ric.plt.appmgr.client.model.ConfigMetadata;
32 import org.oransc.ric.plt.appmgr.client.model.XAppConfig;
33 import org.oransc.ric.plt.appmgr.client.model.XAppInfo;
34 import org.oransc.ric.plt.appmgr.client.model.Xapp;
35 import org.oransc.ric.portal.dashboard.DashboardApplication;
36 import org.oransc.ric.portal.dashboard.DashboardConstants;
37 import org.oransc.ric.portal.dashboard.model.AppTransport;
38 import org.oransc.ric.portal.dashboard.model.DashboardDeployableXapps;
39 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.Configuration;
44 import org.springframework.http.MediaType;
45 import org.springframework.security.access.annotation.Secured;
46 import org.springframework.util.Assert;
47 import org.springframework.web.bind.annotation.DeleteMapping;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.PostMapping;
51 import org.springframework.web.bind.annotation.PutMapping;
52 import org.springframework.web.bind.annotation.RequestBody;
53 import org.springframework.web.bind.annotation.RequestMapping;
54 import org.springframework.web.bind.annotation.RestController;
55
56 import io.swagger.annotations.ApiOperation;
57
58 /**
59  * Proxies calls from the front end to the App Manager API.
60  * 
61  * If a method throws RestClientResponseException, it is handled by
62  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
63  * which returns status 502. All other exceptions are handled by Spring which
64  * returns status 500.
65  */
66 @Configuration
67 @RestController
68 @RequestMapping(value = AppManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
69 public class AppManagerController {
70
71         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
72
73         // Publish paths in constants so tests are easy to write
74         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/appmgr";
75         // Endpoints
76         public static final String HEALTH_ALIVE_METHOD = "/health/alive";
77         public static final String HEALTH_READY_METHOD = "/health/ready";
78         public static final String CONFIG_METHOD = "/config";
79         public static final String XAPPS_METHOD = "/xapps";
80         public static final String XAPPS_LIST_METHOD = XAPPS_METHOD + "/list";
81         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
82         // Path parameters
83         public static final String PP_XAPP_NAME = "xAppName";
84
85         // Populated by the autowired constructor
86         private final HealthApi healthApi;
87         private final XappApi xappApi;
88
89         @Autowired
90         public AppManagerController(final HealthApi healthApi, final XappApi xappApi) {
91                 Assert.notNull(healthApi, "health API must not be null");
92                 Assert.notNull(xappApi, "xapp API must not be null");
93                 this.healthApi = healthApi;
94                 this.xappApi = xappApi;
95                 if (logger.isDebugEnabled())
96                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
97                                         xappApi.getClass().getName());
98         }
99
100         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
101         @GetMapping(VERSION_METHOD)
102         // No role required
103         public SuccessTransport getClientVersion() {
104                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
105         }
106
107         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
108         @GetMapping(HEALTH_ALIVE_METHOD)
109         // No role required
110         public void getHealth(HttpServletResponse response) {
111                 logger.debug("getHealthAlive");
112                 healthApi.getHealthAlive();
113                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
114         }
115
116         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
117         @GetMapping(HEALTH_READY_METHOD)
118         // No role required
119         public void getHealthReady(HttpServletResponse response) {
120                 logger.debug("getHealthReady");
121                 healthApi.getHealthReady();
122                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
123         }
124
125         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
126         @GetMapping(CONFIG_METHOD)
127         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
128         public AllXappConfig getAllXappConfig() {
129                 logger.debug("getAllXappConfig");
130                 return xappApi.getAllXappConfig();
131         }
132
133         @ApiOperation(value = "Create xApp config.", response = XAppConfig.class)
134         @PostMapping(CONFIG_METHOD)
135         @Secured({ DashboardConstants.ROLE_ADMIN })
136         public XAppConfig createXappConfig(@RequestBody XAppConfig xAppConfig) {
137                 logger.debug("createXappConfig {}", xAppConfig);
138                 return xappApi.createXappConfig(xAppConfig);
139         }
140
141         @ApiOperation(value = "Modify xApp config.", response = XAppConfig.class)
142         @PutMapping(CONFIG_METHOD)
143         @Secured({ DashboardConstants.ROLE_ADMIN })
144         public XAppConfig modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
145                 logger.debug("modifyXappConfig {}", xAppConfig);
146                 return xappApi.modifyXappConfig(xAppConfig);
147         }
148
149         @ApiOperation(value = "Delete xApp configuration.")
150         @DeleteMapping(CONFIG_METHOD + "/{" + PP_XAPP_NAME + "}")
151         @Secured({ DashboardConstants.ROLE_ADMIN })
152         public void deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
153                 logger.debug("deleteXappConfig {}", configMetadata);
154                 xappApi.deleteXappConfig(configMetadata);
155                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
156         }
157
158         @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
159         @GetMapping(XAPPS_LIST_METHOD)
160         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
161         public Object getAvailableXapps() {
162                 logger.debug("getAvailableXapps");
163                 AllDeployableXapps appNames = xappApi.listAllDeployableXapps();
164                 // Answer a collection of structure instead of string
165                 // because I expect the AppMgr to be extended with
166                 // additional properties for each one.
167                 DashboardDeployableXapps apps = new DashboardDeployableXapps();
168                 for (String n : appNames)
169                         apps.add(new AppTransport(n));
170                 return apps;
171         }
172
173         @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
174         @GetMapping(XAPPS_METHOD)
175         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
176         public AllDeployedXapps getDeployedXapps() {
177                 logger.debug("getDeployedXapps");
178                 return xappApi.getAllXapps();
179         }
180
181         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
182         @GetMapping(XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}")
183         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
184         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
185                 logger.debug("getXapp {}", xAppName);
186                 return xappApi.getXappByName(xAppName);
187         }
188
189         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
190         @PostMapping(XAPPS_METHOD)
191         @Secured({ DashboardConstants.ROLE_ADMIN })
192         public Xapp deployXapp(@RequestBody XAppInfo xAppInfo) {
193                 logger.debug("deployXapp {}", xAppInfo);
194                 return xappApi.deployXapp(xAppInfo);
195         }
196
197         @ApiOperation(value = "Undeploy an existing xapp.")
198         @DeleteMapping(XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}")
199         @Secured({ DashboardConstants.ROLE_ADMIN })
200         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
201                 logger.debug("undeployXapp {}", xAppName);
202                 xappApi.undeployXapp(xAppName);
203                 response.setStatus(xappApi.getApiClient().getStatusCode().value());
204         }
205
206 }