365a5803c6e1b3b95d29282eb6eb65111a5ae679
[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 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.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. All methods answer
60  * 502 on failure: <blockquote>HTTP server received an invalid response from a
61  * server it consulted when acting as a proxy or gateway.</blockquote>
62  */
63 @Configuration
64 @RestController
65 @RequestMapping(value = AppManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
66 public class AppManagerController {
67
68         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
69
70         // Publish paths in constants so tests are easy to write
71         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/appmgr";
72         // Endpoints
73         public static final String HEALTH_ALIVE_METHOD = "/health/alive";
74         public static final String HEALTH_READY_METHOD = "/health/ready";
75         public static final String CONFIG_METHOD = "/config";
76         public static final String XAPPS_METHOD = "/xapps";
77         public static final String XAPPS_LIST_METHOD = XAPPS_METHOD + "/list";
78         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
79         // Path parameters
80         public static final String PP_XAPP_NAME = "xAppName";
81
82         // Populated by the autowired constructor
83         private final HealthApi healthApi;
84         private final XappApi xappApi;
85
86         @Autowired
87         public AppManagerController(final HealthApi healthApi, final XappApi xappApi) {
88                 Assert.notNull(healthApi, "health API must not be null");
89                 Assert.notNull(xappApi, "xapp API must not be null");
90                 this.healthApi = healthApi;
91                 this.xappApi = xappApi;
92                 if (logger.isDebugEnabled())
93                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
94                                         xappApi.getClass().getName());
95         }
96
97         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
98         @GetMapping(VERSION_METHOD)
99         // No role required
100         public SuccessTransport getClientVersion() {
101                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
102         }
103
104         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
105         @GetMapping(HEALTH_ALIVE_METHOD)
106         // No role required
107         public void getHealth(HttpServletResponse response) {
108                 logger.debug("getHealthAlive");
109                 healthApi.getHealthAlive();
110                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
111         }
112
113         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
114         @GetMapping(HEALTH_READY_METHOD)
115         // No role required
116         public void getHealthReady(HttpServletResponse response) {
117                 logger.debug("getHealthReady");
118                 healthApi.getHealthReady();
119                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
120         }
121
122         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
123         @GetMapping(CONFIG_METHOD)
124         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
125         public AllXappConfig getAllXappConfig() {
126                 logger.debug("getAllXappConfig");
127                 return xappApi.getAllXappConfig();
128         }
129
130         @ApiOperation(value = "Create xApp config.", response = XAppConfig.class)
131         @PostMapping(CONFIG_METHOD)
132         @Secured({ DashboardConstants.ROLE_ADMIN })
133         public XAppConfig createXappConfig(@RequestBody XAppConfig xAppConfig) {
134                 logger.debug("createXappConfig {}", xAppConfig);
135                 return xappApi.createXappConfig(xAppConfig);
136         }
137
138         @ApiOperation(value = "Modify xApp config.", response = XAppConfig.class)
139         @PutMapping(CONFIG_METHOD)
140         @Secured({ DashboardConstants.ROLE_ADMIN })
141         public XAppConfig modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
142                 logger.debug("modifyXappConfig {}", xAppConfig);
143                 return xappApi.modifyXappConfig(xAppConfig);
144         }
145
146         @ApiOperation(value = "Delete xApp configuration.")
147         @DeleteMapping(CONFIG_METHOD + "/{" + PP_XAPP_NAME + "}")
148         @Secured({ DashboardConstants.ROLE_ADMIN })
149         public void deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
150                 logger.debug("deleteXappConfig {}", configMetadata);
151                 xappApi.deleteXappConfig(configMetadata);
152                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
153         }
154
155         @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
156         @GetMapping(XAPPS_LIST_METHOD)
157         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
158         public Object getAvailableXapps() {
159                 logger.debug("getAvailableXapps");
160                 AllDeployableXapps appNames = xappApi.listAllDeployableXapps();
161                 // Answer a collection of structure instead of string
162                 // because I expect the AppMgr to be extended with
163                 // additional properties for each one.
164                 DashboardDeployableXapps apps = new DashboardDeployableXapps();
165                 for (String n : appNames)
166                         apps.add(new AppTransport(n));
167                 return apps;
168         }
169
170         @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
171         @GetMapping(XAPPS_METHOD)
172         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
173         public AllDeployedXapps getDeployedXapps() {
174                 logger.debug("getDeployedXapps");
175                 return xappApi.getAllXapps();
176         }
177
178         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
179         @GetMapping(XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}")
180         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
181         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
182                 logger.debug("getXapp {}", xAppName);
183                 return xappApi.getXappByName(xAppName);
184         }
185
186         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
187         @PostMapping(XAPPS_METHOD)
188         @Secured({ DashboardConstants.ROLE_ADMIN })
189         public Xapp deployXapp(@RequestBody XAppInfo xAppInfo) {
190                 logger.debug("deployXapp {}", xAppInfo);
191                 return xappApi.deployXapp(xAppInfo);
192         }
193
194         @ApiOperation(value = "Undeploy an existing xapp.")
195         @DeleteMapping(XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}")
196         @Secured({ DashboardConstants.ROLE_ADMIN })
197         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
198                 logger.debug("undeployXapp {}", xAppName);
199                 xappApi.undeployXapp(xAppName);
200                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
201         }
202
203 }