Require RIC instance key in controller methods
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / A1MediatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 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.config.A1MediatorApiBuilder;
30 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.MediaType;
35 import org.springframework.security.access.annotation.Secured;
36 import org.springframework.util.Assert;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RestController;
43
44 import io.swagger.annotations.ApiOperation;
45 import io.swagger.annotations.ApiParam;
46
47 /**
48  * Proxies calls from the front end to the A1 Mediator API to get and put
49  * policies. All methods are deliberately kept ignorant of the data format.
50  * 
51  * If a method throws RestClientResponseException, it is handled by
52  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
53  * which returns status 502. All other exceptions are handled by Spring which
54  * returns status 500.
55  */
56 @RestController
57 @RequestMapping(value = A1MediatorController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
58 public class A1MediatorController {
59
60         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62         /** This path lacks the RIC instance pattern */
63         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/a1-p";
64         // Path parameters
65         public static final String PP_POLICIES = "policies";
66         // The get and put methods use the same path
67         private static final String POLICY_METHOD_PATH = /* controller path + */ DashboardConstants.RIC_INSTANCE_KEY + "/{"
68                         + DashboardConstants.RIC_INSTANCE_KEY + "}/" + PP_POLICIES + "/{" + PP_POLICIES + "}";
69
70         // Populated by the autowired constructor
71         private final A1MediatorApiBuilder a1MediatorClientBuilder;
72
73         @Autowired
74         public A1MediatorController(final A1MediatorApiBuilder a1MediatorApiBuilder) {
75                 Assert.notNull(a1MediatorApiBuilder, "builder must not be null");
76                 this.a1MediatorClientBuilder = a1MediatorApiBuilder;
77                 if (logger.isDebugEnabled())
78                         logger.debug("ctor: configured with builder type {}", a1MediatorApiBuilder.getClass().getName());
79         }
80
81         @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
82         @GetMapping(DashboardConstants.VERSION_METHOD)
83         // No role required
84         public SuccessTransport getA1MediatorClientVersion() {
85                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
86         }
87
88         @ApiOperation(value = "Gets the specified policy from the A1 Mediator")
89         @GetMapping(POLICY_METHOD_PATH)
90         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
91         public Object getPolicy(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
92                         @PathVariable(PP_POLICIES) String policyName) {
93                 logger.debug("getPolicy instance {} policy {}", instanceKey, policyName);
94                 return a1MediatorClientBuilder.getA1MediatorApi(instanceKey).a1ControllerGetHandler(policyName);
95         }
96
97         @ApiOperation(value = "Puts the specified policy to the A1 Mediator")
98         @PutMapping(POLICY_METHOD_PATH)
99         @Secured({ DashboardConstants.ROLE_ADMIN })
100         public void putPolicy(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
101                         @PathVariable(PP_POLICIES) String policyName, @ApiParam(value = "Policy body") @RequestBody String policy, //
102                         HttpServletResponse response) {
103                 logger.debug("putPolicy instance {} name {} value {}", instanceKey, policyName, policy);
104                 A1MediatorApi api = a1MediatorClientBuilder.getA1MediatorApi(instanceKey);
105                 api.a1ControllerPutHandler(policyName, policy);
106                 response.setStatus(api.getApiClient().getStatusCode().value());
107         }
108
109 }