ddef5b42efb1f1121d219cafa4cccee71d1cb9c7
[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         // Path parameters
75         public static final String PP_XAPP_NAME = "xAppName";
76
77         // Populated by the autowired constructor
78         private final HealthApi healthApi;
79         private final XappApi xappApi;
80
81         @Autowired
82         public AppManagerController(final HealthApi healthApi, final XappApi xappApi) {
83                 Assert.notNull(healthApi, "health API must not be null");
84                 Assert.notNull(xappApi, "xapp API must not be null");
85                 this.healthApi = healthApi;
86                 this.xappApi = xappApi;
87                 if (logger.isDebugEnabled())
88                         logger.debug("ctor: configured with client types {} and {}", healthApi.getClass().getName(),
89                                         xappApi.getClass().getName());
90         }
91
92         @ApiOperation(value = "Gets the XApp manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
93         @RequestMapping(value = DashboardConstants.VERSION_METHOD, method = RequestMethod.GET)
94         public SuccessTransport getXappManagerClientVersion() {
95                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
96         }
97
98         @ApiOperation(value = "Health check of xApp Manager - Liveness probe.")
99         @RequestMapping(value = HEALTH_ALIVE_METHOD, method = RequestMethod.GET)
100         public void getHealth(HttpServletResponse response) {
101                 logger.debug("getHealthAlive");
102                 healthApi.getHealthAlive();
103                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
104         }
105
106         @ApiOperation(value = "Readiness check of xApp Manager - Readiness probe.")
107         @RequestMapping(value = HEALTH_READY_METHOD, method = RequestMethod.GET)
108         public void getHealthReady(HttpServletResponse response) {
109                 logger.debug("getHealthReady");
110                 healthApi.getHealthReady();
111                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
112         }
113
114         @ApiOperation(value = "Returns the configuration of all xapps.", response = AllXappConfig.class)
115         @RequestMapping(value = CONFIG_METHOD, method = RequestMethod.GET)
116         public AllXappConfig getAllXappConfig() {
117                 logger.debug("getAllXappConfig");
118                 return xappApi.getAllXappConfig();
119         }
120
121         @ApiOperation(value = "Create xApp config.", response = XAppConfig.class)
122         @RequestMapping(value = CONFIG_METHOD, method = RequestMethod.POST)
123         public XAppConfig createXappConfig(@RequestBody XAppConfig xAppConfig) {
124                 logger.debug("createXappConfig {}", xAppConfig);
125                 return xappApi.createXappConfig(xAppConfig);
126         }
127
128         @ApiOperation(value = "Modify xApp config.", response = XAppConfig.class)
129         @RequestMapping(value = CONFIG_METHOD, method = RequestMethod.PUT)
130         public XAppConfig modifyXappConfig(@RequestBody XAppConfig xAppConfig) {
131                 logger.debug("modifyXappConfig {}", xAppConfig);
132                 return xappApi.modifyXappConfig(xAppConfig);
133         }
134
135         @ApiOperation(value = "Delete xApp configuration.")
136         @RequestMapping(value = CONFIG_METHOD + "/{" + PP_XAPP_NAME + "}", method = RequestMethod.DELETE)
137         public void deleteXappConfig(@RequestBody ConfigMetadata configMetadata, HttpServletResponse response) {
138                 logger.debug("deleteXappConfig {}", configMetadata);
139                 xappApi.deleteXappConfig(configMetadata);
140                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
141         }
142
143         @ApiOperation(value = "Returns a list of deployable xapps.", response = DashboardDeployableXapps.class)
144         @RequestMapping(value = XAPPS_LIST_METHOD, method = RequestMethod.GET)
145         public Object getAvailableXapps() {
146                 logger.debug("getAvailableXapps");
147                 AllDeployableXapps appNames = xappApi.listAllXapps();
148                 // Answer a collection of structure instead of string
149                 // because I expect the AppMgr to be extended with
150                 // additional properties for each one.
151                 DashboardDeployableXapps apps = new DashboardDeployableXapps();
152                 for (String n : appNames)
153                         apps.add(new AppTransport(n));
154                 return apps;
155         }
156
157         @ApiOperation(value = "Returns the status of all deployed xapps.", response = AllDeployedXapps.class)
158         @RequestMapping(value = XAPPS_METHOD, method = RequestMethod.GET)
159         public AllDeployedXapps getDeployedXapps() {
160                 logger.debug("getDeployedXapps");
161                 return xappApi.getAllXapps();
162         }
163
164         @ApiOperation(value = "Returns the status of a given xapp.", response = Xapp.class)
165         @RequestMapping(value = XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}", method = RequestMethod.GET)
166         public Xapp getXapp(@PathVariable("xAppName") String xAppName) {
167                 logger.debug("getXapp {}", xAppName);
168                 return xappApi.getXappByName(xAppName);
169         }
170
171         @ApiOperation(value = "Deploy a xapp.", response = Xapp.class)
172         @RequestMapping(value = XAPPS_METHOD, method = RequestMethod.POST)
173         public Xapp deployXapp(@RequestBody XAppInfo xAppInfo) {
174                 logger.debug("deployXapp {}", xAppInfo);
175                 return xappApi.deployXapp(xAppInfo);
176         }
177
178         @ApiOperation(value = "Undeploy an existing xapp.")
179         @RequestMapping(value = XAPPS_METHOD + "/{" + PP_XAPP_NAME + "}", method = RequestMethod.DELETE)
180         public void undeployXapp(@PathVariable("xAppName") String xAppName, HttpServletResponse response) {
181                 logger.debug("undeployXapp {}", xAppName);
182                 xappApi.undeployXapp(xAppName);
183                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
184         }
185
186 }