1054cc82126ed986b67938cf6749b8cb5f6162df
[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.security.access.annotation.Secured;
35 import org.springframework.util.Assert;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PutMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RestController;
41
42 import io.swagger.annotations.ApiOperation;
43 import io.swagger.annotations.ApiParam;
44
45 /**
46  * Proxies calls from the front end to the AC xApp via the A1 Mediator API.
47  * 
48  * If a method throws RestClientResponseException, it is handled by
49  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
50  * which returns status 502. All other exceptions are handled by Spring which
51  * returns status 500.
52  */
53 @RestController
54 @RequestMapping(value = AcXappController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
55 public class AcXappController {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         // Publish paths in constants so tests are easy to write
60         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/xapp/admctl";
61         // Endpoints
62         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
63         public static final String POLICY_METHOD = "policy";
64
65         // A "control" is an element in the XApp descriptor
66         private static final String AC_CONTROL_NAME = "admission_control_policy";
67
68         // Populated by the autowired constructor
69         private final A1MediatorApi a1MediatorApi;
70
71         @Autowired
72         public AcXappController(final A1MediatorApi a1MediatorApi) {
73                 Assert.notNull(a1MediatorApi, "API must not be null");
74                 this.a1MediatorApi = a1MediatorApi;
75                 if (logger.isDebugEnabled())
76                         logger.debug("ctor: configured with client type {}", a1MediatorApi.getClass().getName());
77         }
78
79         @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
80         @GetMapping(VERSION_METHOD)
81         // No role required
82         public SuccessTransport getA1MediatorClientVersion() {
83                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
84         }
85
86         /*
87          * This controller is deliberately kept ignorant of the data expected by AC. The
88          * fields are defined in the ACAdmissionIntervalControl Typescript interface.
89          */
90         @ApiOperation(value = "Gets the admission control policy for AC xApp via the A1 Mediator")
91         @GetMapping(POLICY_METHOD)
92         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
93         public Object getAdmissionControlPolicy(HttpServletResponse response) {
94                 logger.debug("getAdmissionControlPolicy");
95                 response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
96                 return null;
97         }
98
99         /*
100          * This controller is deliberately kept ignorant of the data expected by AC. The
101          * fields are defined in the ACAdmissionIntervalControl Typescript interface. AC
102          * uses snake_case keys but Jackson automatically converts to CamelCase on
103          * parse. To avoid this conversion, specify the request parameter as String.
104          */
105         @ApiOperation(value = "Sets the admission control policy for AC xApp via the A1 Mediator")
106         @PutMapping(POLICY_METHOD)
107         @Secured({ DashboardConstants.ROLE_ADMIN })
108         public void putAdmissionControlPolicy(@ApiParam(value = "Admission control policy") @RequestBody String acPolicy, //
109                         HttpServletResponse response) {
110                 logger.debug("putAdmissionControlPolicy {}", acPolicy);
111                 a1MediatorApi.a1ControllerPutHandler(AC_CONTROL_NAME, acPolicy);
112                 response.setStatus(a1MediatorApi.getApiClient().getStatusCode().value());
113         }
114
115 }