2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
24 import javax.servlet.http.HttpServletResponse;
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;
56 import io.swagger.annotations.ApiOperation;
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>
65 @RequestMapping(value = AppManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
66 public class AppManagerController {
68 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
70 // Publish paths in constants so tests are easy to write
71 public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/appmgr";
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;
80 public static final String PP_XAPP_NAME = "xAppName";
82 // Populated by the autowired constructor
83 private final HealthApi healthApi;
84 private final XappApi xappApi;
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());
97 @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
98 @GetMapping(VERSION_METHOD)
100 public SuccessTransport getClientVersion() {
101 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
104 @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
105 @GetMapping(HEALTH_ALIVE_METHOD)
107 public void getHealth(HttpServletResponse response) {
108 logger.debug("getHealthAlive");
109 healthApi.getHealthAlive();
110 response.setStatus(healthApi.getApiClient().getStatusCode().value());
113 @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
114 @GetMapping(HEALTH_READY_METHOD)
116 public void getHealthReady(HttpServletResponse response) {
117 logger.debug("getHealthReady");
118 healthApi.getHealthReady();
119 response.setStatus(healthApi.getApiClient().getStatusCode().value());
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();
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);
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);
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());
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));
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();
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);
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);
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());