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