2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
24 import javax.servlet.http.HttpServletResponse;
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.PathVariable;
38 import org.springframework.web.bind.annotation.PutMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RestController;
43 import io.swagger.annotations.ApiOperation;
44 import io.swagger.annotations.ApiParam;
47 * Proxies calls from the front end to the A1 Mediator API to get and put
48 * policies. The first application managed via this path is Admission Control.
50 * If a method throws RestClientResponseException, it is handled by
51 * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
52 * which returns status 502. All other exceptions are handled by Spring which
56 @RequestMapping(value = A1MediatorController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
57 public class A1MediatorController {
59 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61 // Publish paths in constants so tests are easy to write
62 public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/a1-p";
64 public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
66 public static final String PP_POLICIES = "policies";
68 // Populated by the autowired constructor
69 private final A1MediatorApi a1MediatorApi;
72 public A1MediatorController(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());
79 @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
80 @GetMapping(VERSION_METHOD)
82 public SuccessTransport getA1MediatorClientVersion() {
83 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
87 * This method is deliberately kept ignorant of the data passing thru.
89 @ApiOperation(value = "Gets the specified policy from the A1 Mediator")
90 @GetMapping(PP_POLICIES + "/{" + PP_POLICIES + "}")
91 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
92 public Object getPolicy(@PathVariable(PP_POLICIES) String policyName) {
93 logger.debug("getPolicy {}", policyName);
94 return a1MediatorApi.a1ControllerGetHandler(policyName);
98 * This method is deliberately kept ignorant of the data passing thru.
100 @ApiOperation(value = "Puts the specified policy to the A1 Mediator")
101 @PutMapping(PP_POLICIES + "/{" + PP_POLICIES + "}")
102 @Secured({ DashboardConstants.ROLE_ADMIN })
103 public void putPolicy(@PathVariable(PP_POLICIES) String policyName,
104 @ApiParam(value = "Policy body") @RequestBody String policy, //
105 HttpServletResponse response) {
106 logger.debug("putPolicy name {} value {}", policyName, policy);
107 a1MediatorApi.a1ControllerPutHandler(policyName, policy);
108 response.setStatus(a1MediatorApi.getApiClient().getStatusCode().value());