Pass thru error details from remote APIs
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / AcXappController.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.a1med.client.api.A1MediatorApi;
27 import org.oransc.ric.portal.dashboard.DashboardApplication;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.util.Assert;
36 import org.springframework.web.bind.annotation.RequestBody;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.RestController;
40 import org.springframework.web.client.HttpStatusCodeException;
41
42 import com.fasterxml.jackson.databind.JsonNode;
43
44 import io.swagger.annotations.ApiOperation;
45 import io.swagger.annotations.ApiParam;
46
47 /**
48  * * Proxies calls from the front end to the AC xApp via the A1 Mediator API.
49  * All methods answer 502 on failure: <blockquote>HTTP server received an
50  * invalid response from a server it consulted when acting as a proxy or
51  * gateway.</blockquote>
52  */
53 @RestController
54 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xapp/ac", produces = MediaType.APPLICATION_JSON_VALUE)
55 public class AcXappController {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         // A "control" is an element in the XApp descriptor
60         private static final String AC_CONTROL_NAME = "admission_control_policy";
61
62         // Populated by the autowired constructor
63         private final A1MediatorApi a1MediatorApi;
64
65         @Autowired
66         public AcXappController(final A1MediatorApi a1MediatorApi) {
67                 Assert.notNull(a1MediatorApi, "API must not be null");
68                 this.a1MediatorApi = a1MediatorApi;
69                 if (logger.isDebugEnabled())
70                         logger.debug("ctor: configured with client type {}", a1MediatorApi.getClass().getName());
71         }
72
73         @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
74         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
75         public SuccessTransport getA1MediatorClientVersion() {
76                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
77         }
78
79         /*
80          * GET policy is not supported at present by A1 Mediator! Always returns 501.
81          */
82         @ApiOperation(value = "Gets the admission control policy for AC xApp via the A1 Mediator")
83         @RequestMapping(value = "admctrl", method = RequestMethod.GET)
84         public Object getAdmissionControlPolicy(HttpServletResponse response) {
85                 logger.debug("getAdmissionControlPolicy");
86                 response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
87                 return null;
88         }
89
90         /*
91          * This controller is deliberately kept ignorant of the data expected by AC. The
92          * fields are defined in the ACAdmissionIntervalControl Typescript interface.
93          */
94         @ApiOperation(value = "Sets the admission control policy for AC xApp via the A1 Mediator")
95         @RequestMapping(value = "catime", method = RequestMethod.PUT)
96         public Object setAdmissionControlPolicy(
97                         @ApiParam(value = "Admission control policy") @RequestBody JsonNode acPolicy, //
98                         HttpServletResponse response) {
99                 logger.debug("setAdmissionControlPolicy {}", acPolicy);
100                 try {
101                         a1MediatorApi.a1ControllerPutHandler(AC_CONTROL_NAME, acPolicy);
102                         response.setStatus(a1MediatorApi.getApiClient().getStatusCode().value());
103                         return null;
104                 } catch (HttpStatusCodeException ex) {
105                         logger.warn("setAdmissionControlPolicy failed: {}", ex.toString());
106                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
107                 }
108         }
109
110 }