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