Add Xapp Onboarder client to backend
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / XappOnboarderController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 AT&T Intellectual Property
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 org.oransc.itdev.xapponboarder.client.api.HealthApi;
25 import org.oransc.itdev.xapponboarder.client.model.Descriptor;
26 import org.oransc.itdev.xapponboarder.client.model.DescriptorRemote;
27 import org.oransc.itdev.xapponboarder.client.model.Status;
28 import org.oransc.ric.portal.dashboard.DashboardApplication;
29 import org.oransc.ric.portal.dashboard.DashboardConstants;
30 import org.oransc.ric.portal.dashboard.config.XappOnboarderApiBuilder;
31 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.security.access.annotation.Secured;
39 import org.springframework.util.Assert;
40 import org.springframework.validation.annotation.Validated;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.PostMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RestController;
47
48 import io.swagger.annotations.ApiOperation;
49
50 /**
51  * Proxies calls from the front end to the Xapp Onboarder API.
52  * 
53  * If a method throws RestClientResponseException, it is handled by a method in
54  * {@link CustomResponseEntityExceptionHandler} which returns status 502. All
55  * other exceptions are handled by Spring which returns status 500.
56  */
57 @Configuration
58 @RestController
59 @RequestMapping(value = XappOnboarderController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
60 public class XappOnboarderController {
61
62         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
63
64         // Publish paths in constants for tests
65         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/xappobrd";
66         // Dashboard only
67         public static final String HEALTH_METHOD = "health";
68         public static final String CHARTS_METHOD = "charts";
69         public static final String ONBOARD_METHOD = "onboard";
70         public static final String ONBOARD_DOWNLOAD_METHOD = "onboard/download";
71         // Path component, not a method nor a path parameter
72         public static final String VALUESYAML = "values.yaml";
73         // Path parameters
74         public static final String XAPPNAME_PP = "xapp";
75         public static final String VERSION_PP = "ver";
76
77         // Populated by the autowired constructor
78         private final XappOnboarderApiBuilder xappOnboarderApiBuilder;
79
80         @Autowired
81         public XappOnboarderController(final XappOnboarderApiBuilder xappOnboarderApiBuilder) {
82                 Assert.notNull(xappOnboarderApiBuilder, "builder must not be null");
83                 this.xappOnboarderApiBuilder = xappOnboarderApiBuilder;
84                 if (logger.isDebugEnabled())
85                         logger.debug("ctor: configured with builder type {}", xappOnboarderApiBuilder.getClass().getName());
86         }
87
88         @ApiOperation(value = "Gets the xapp onboarder client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
89         @GetMapping(DashboardConstants.VERSION_METHOD)
90         // No role required
91         public SuccessTransport getClientVersion() {
92                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
93         }
94
95         @ApiOperation(value = "Gets the health from the xapp onboarder, expressed as the response code.")
96         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + HEALTH_METHOD)
97         // No role required
98         public ResponseEntity<Status> getHealthCheck(
99                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
100                 logger.debug("getHealthCheck instance {}", instanceKey);
101                 HealthApi api = xappOnboarderApiBuilder.getHealthApi(instanceKey);
102                 Status status = api.getHealthCheck();
103                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(status);
104         }
105
106         @ApiOperation(value = "Gets the helm charts.", response = String.class)
107         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + CHARTS_METHOD)
108         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
109         public Object getCharts(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
110                 logger.debug("getCharts instance {}", instanceKey);
111                 return xappOnboarderApiBuilder.getChartsApi(instanceKey).getChartsList();
112         }
113
114         @ApiOperation(value = "Gets the helm chart for the specified xApp and version.", response = String.class)
115         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + CHARTS_METHOD
116                         + "/" + XAPPNAME_PP + "/{" + XAPPNAME_PP + "}/" + VERSION_PP + "/{" + VERSION_PP + "}")
117         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
118         public Object getChart(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
119                         @PathVariable(XAPPNAME_PP) String xappName, @PathVariable(VERSION_PP) String version) {
120                 logger.debug("getChart instance {} xapp {} ver {}", instanceKey, xappName, version);
121                 return xappOnboarderApiBuilder.getChartsApi(instanceKey).getChartsFetcher(xappName, version);
122         }
123
124         @ApiOperation(value = "Gets the values yaml for the specified xApp and version.", response = String.class)
125         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + CHARTS_METHOD
126                         + "/" + XAPPNAME_PP + "/{" + XAPPNAME_PP + "}/" + VERSION_PP + "/{" + VERSION_PP + "}/" + VALUESYAML)
127         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
128         public Object getValues(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
129                         @PathVariable(XAPPNAME_PP) String xappName, @PathVariable(VERSION_PP) String version) {
130                 logger.debug("getValues instance {} xapp {} ver {}", instanceKey, xappName, version);
131                 return xappOnboarderApiBuilder.getChartsApi(instanceKey).getValuesYamlFetcher(xappName, version);
132         }
133
134         @ApiOperation(value = "Onboard xApp using the xApp descriptor and schema in the request body.", response = Status.class)
135         @PostMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
136                         + ONBOARD_METHOD)
137         @Secured({ DashboardConstants.ROLE_ADMIN })
138         public Status onboardXapp(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey, //
139                         @Validated @RequestBody Descriptor appDescriptor) {
140                 logger.debug("onboardxApp instance {} descriptor {}", instanceKey, appDescriptor);
141                 return xappOnboarderApiBuilder.getOnboardApi(instanceKey).postOnboardxApps(appDescriptor);
142         }
143
144         @ApiOperation(value = "Onboard xApp after downloading the xApp descriptor and schema from the URLs.", response = Status.class)
145         @PostMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
146                         + ONBOARD_DOWNLOAD_METHOD)
147         @Secured({ DashboardConstants.ROLE_ADMIN })
148         public Status onboardRemoteXapp(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey, //
149                         @Validated @RequestBody DescriptorRemote appDescriptor) {
150                 logger.debug("onboardRemoteXapp instance {} descriptor {}", instanceKey, appDescriptor);
151                 return xappOnboarderApiBuilder.getOnboardApi(instanceKey).postOnboardxAppsDownload(appDescriptor);
152         }
153
154 }