2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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 org.oransc.ric.portal.dashboard.DashboardApplication;
25 import org.oransc.ric.portal.dashboard.DashboardConstants;
26 import org.oransc.ric.portal.dashboard.config.AppManagerApiBuilder;
27 import org.oransc.ric.portal.dashboard.model.AppTransport;
28 import org.oransc.ric.portal.dashboard.model.DashboardDeployableXapps;
29 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
30 import org.oransc.ricplt.appmgr.client.api.HealthApi;
31 import org.oransc.ricplt.appmgr.client.api.XappApi;
32 import org.oransc.ricplt.appmgr.client.model.AllDeployableXapps;
33 import org.oransc.ricplt.appmgr.client.model.AllDeployedXapps;
34 import org.oransc.ricplt.appmgr.client.model.AllXappConfig;
35 import org.oransc.ricplt.appmgr.client.model.ConfigValidationErrors;
36 import org.oransc.ricplt.appmgr.client.model.XAppConfig;
37 import org.oransc.ricplt.appmgr.client.model.Xapp;
38 import org.oransc.ricplt.appmgr.client.model.XappDescriptor;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.security.access.annotation.Secured;
46 import org.springframework.util.Assert;
47 import org.springframework.validation.annotation.Validated;
48 import org.springframework.web.bind.annotation.DeleteMapping;
49 import org.springframework.web.bind.annotation.GetMapping;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.PostMapping;
52 import org.springframework.web.bind.annotation.PutMapping;
53 import org.springframework.web.bind.annotation.RequestBody;
54 import org.springframework.web.bind.annotation.RequestMapping;
55 import org.springframework.web.bind.annotation.RestController;
57 import io.swagger.annotations.ApiOperation;
60 * Proxies calls from the front end to the App Manager API.
62 * If a method throws RestClientResponseException, it is handled by a method in
63 * {@link CustomResponseEntityExceptionHandler} which returns status 502. All
64 * other exceptions are handled by Spring which returns status 500.
68 @RequestMapping(value = AppManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
69 public class AppManagerController {
71 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
73 // Publish paths in constants so tests are easy to write
74 public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/appmgr";
75 public static final String HEALTH_ALIVE_METHOD = "health/alive";
76 public static final String HEALTH_READY_METHOD = "health/ready";
77 public static final String CONFIG_METHOD = "config";
78 public static final String XAPPS_METHOD = "xapps";
79 public static final String XAPPS_LIST_METHOD = XAPPS_METHOD + "/list";
81 public static final String PP_XAPP_NAME = "xAppName";
83 private static final String CONFIG_METHOD_PATH = DashboardConstants.RIC_INSTANCE_KEY + "/{"
84 + DashboardConstants.RIC_INSTANCE_KEY + "}/" + CONFIG_METHOD;
85 private static final String XAPPS_METHOD_PATH = DashboardConstants.RIC_INSTANCE_KEY + "/{"
86 + DashboardConstants.RIC_INSTANCE_KEY + "}/" + XAPPS_METHOD;
88 // Populated by the autowired constructor
89 private final AppManagerApiBuilder appManagerApiBuilder;
92 public AppManagerController(final AppManagerApiBuilder appManagerApiBuilder) {
93 Assert.notNull(appManagerApiBuilder, "builder must not be null");
94 this.appManagerApiBuilder = appManagerApiBuilder;
95 if (logger.isDebugEnabled())
96 logger.debug("ctor: configured with builder type {}", appManagerApiBuilder.getClass().getName());
99 @ApiOperation(value = "Gets the App manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
100 @GetMapping(DashboardConstants.VERSION_METHOD)
102 public SuccessTransport getClientVersion() {
103 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
106 @ApiOperation(value = "Health check of App Manager - Liveness probe.")
107 @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
108 + HEALTH_ALIVE_METHOD)
110 public ResponseEntity<String> getHealth(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
111 logger.debug("getHealthAlive instance {}", instanceKey);
112 HealthApi api = appManagerApiBuilder.getHealthApi(instanceKey);
113 api.getHealthAlive();
114 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
117 @ApiOperation(value = "Readiness check of App Manager - Readiness probe.")
118 @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
119 + HEALTH_READY_METHOD)
121 public ResponseEntity<String> getHealthReady(
122 @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
123 logger.debug("getHealthReady instance {}", instanceKey);
124 HealthApi api = appManagerApiBuilder.getHealthApi(instanceKey);
125 api.getHealthReady();
126 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
129 @ApiOperation(value = "Returns the configuration of all Xapps.", response = AllXappConfig.class)
130 @GetMapping(CONFIG_METHOD_PATH)
131 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
132 public AllXappConfig getAllXappConfig(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
133 logger.debug("getAllXappConfig instance {}", instanceKey);
134 return appManagerApiBuilder.getXappApi(instanceKey).getAllXappConfig();
137 @ApiOperation(value = "Modify XApp config.", response = ConfigValidationErrors.class)
138 @PutMapping(CONFIG_METHOD_PATH)
139 @Secured({ DashboardConstants.ROLE_ADMIN })
140 public ConfigValidationErrors modifyXappConfig(
141 @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey, //
142 @Validated @RequestBody XAppConfig xAppConfig) {
143 logger.debug("modifyXappConfig instance {} config {}", instanceKey, xAppConfig);
144 return appManagerApiBuilder.getXappApi(instanceKey).modifyXappConfig(xAppConfig);
147 @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
148 @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
150 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
151 public DashboardDeployableXapps getAvailableXapps(
152 @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
153 logger.debug("getAvailableXapps instance {}", instanceKey);
154 AllDeployableXapps deployableXapps = appManagerApiBuilder.getXappApi(instanceKey).listAllXapps();
155 // Answer a collection of structure instead of string
156 // because I expect the AppMgr to be extended with
157 // additional properties for each one.
158 DashboardDeployableXapps apps = new DashboardDeployableXapps();
159 for (String n : deployableXapps)
160 apps.add(new AppTransport(n));
164 @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
165 @GetMapping(XAPPS_METHOD_PATH)
166 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
167 public AllDeployedXapps getDeployedXapps(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
168 logger.debug("getDeployedXapps instance {}", instanceKey);
169 return appManagerApiBuilder.getXappApi(instanceKey).getAllXapps();
172 @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
173 @GetMapping(XAPPS_METHOD_PATH + "/{" + PP_XAPP_NAME + "}")
174 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
175 public Xapp getXapp(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
176 @PathVariable(PP_XAPP_NAME) String appName) {
177 logger.debug("getXapp instance {} name {}", instanceKey, appName);
178 return appManagerApiBuilder.getXappApi(instanceKey).getXappByName(appName);
181 @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
182 @PostMapping(XAPPS_METHOD_PATH)
183 @Secured({ DashboardConstants.ROLE_ADMIN })
184 public Xapp deployXapp(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey, //
185 @Validated @RequestBody XappDescriptor appDescriptor) {
186 logger.debug("deployXapp instance {} descriptor {}", instanceKey, appDescriptor);
187 return appManagerApiBuilder.getXappApi(instanceKey).deployXapp(appDescriptor);
190 @ApiOperation(value = "Undeploy an existing xapp.")
191 @DeleteMapping(XAPPS_METHOD_PATH + "/{" + PP_XAPP_NAME + "}")
192 @Secured({ DashboardConstants.ROLE_ADMIN })
193 public ResponseEntity<String> undeployXapp(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
194 @PathVariable(PP_XAPP_NAME) String appName) {
195 logger.debug("undeployXapp instance {} name {}", instanceKey, appName);
196 XappApi api = appManagerApiBuilder.getXappApi(instanceKey);
197 api.undeployXapp(appName);
198 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);